perf(api): add HTTP range requests on media sources (#9)
Reviewed-on: https://gitea.vodsystem.ddns.net/dylan/vod-system/pulls/9
This commit was merged in pull request #9.
This commit is contained in:
@@ -12,6 +12,7 @@ import org.springframework.http.MediaTypeFactory;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@@ -77,8 +78,8 @@ public class ClipController {
|
||||
);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/media")
|
||||
public ResponseEntity<Resource> downloadClip(@PathVariable Long id) {
|
||||
@GetMapping(value = "/{id}/media", produces = MediaType.ALL_VALUE)
|
||||
public ResponseEntity<Resource> downloadClip(@PathVariable Long id) throws IOException {
|
||||
Resource resource = clipService.downloadClip(id);
|
||||
|
||||
if (resource == null || !resource.exists()) {
|
||||
@@ -86,8 +87,11 @@ public class ClipController {
|
||||
}
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, String.format(FILENAME_HEADER, resource.getFilename()))
|
||||
.contentType(MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM))
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||
String.format(FILENAME_HEADER, resource.getFilename()))
|
||||
.contentType(MediaTypeFactory.getMediaType(resource)
|
||||
.orElse(MediaType.APPLICATION_OCTET_STREAM))
|
||||
.contentLength(resource.contentLength())
|
||||
.body(resource);
|
||||
}
|
||||
|
||||
|
||||
@@ -102,6 +102,13 @@ public class GlobalExceptionHandler {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler(ClipMediaUnavailable.class)
|
||||
public ResponseEntity<APIResponse<Void>> handleClipMediaUnavailable(ClipMediaUnavailable ex) {
|
||||
logger.error("ClipMediaUnavailable (clip {}): {}", ex.getClipId(), ex.getMessage());
|
||||
APIResponse<Void> response = new APIResponse<>(ERROR, "Clip media is currently unavailable", null);
|
||||
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler(VodNotFound.class)
|
||||
public ResponseEntity<APIResponse<Void>> handleVodNotFound(VodNotFound ex) {
|
||||
logger.error("VodNotFound: {}", ex.getMessage());
|
||||
@@ -109,6 +116,13 @@ public class GlobalExceptionHandler {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler(VodMediaUnavailable.class)
|
||||
public ResponseEntity<APIResponse<Void>> handleVodMediaUnavailable(VodMediaUnavailable ex) {
|
||||
logger.error("VodMediaUnavailable (vod {}): {}", ex.getVodId(), ex.getMessage());
|
||||
APIResponse<Void> response = new APIResponse<>(ERROR, "Vod media is currently unavailable", null);
|
||||
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler(NotStreaming.class)
|
||||
public ResponseEntity<APIResponse<Void>> handleNotStreaming(NotStreaming ex) {
|
||||
logger.error("NotStreaming: {}", ex.getMessage());
|
||||
|
||||
@@ -30,7 +30,7 @@ public class MediaController {
|
||||
public ResponseEntity<APIResponse<UUIDResponse>> compress(
|
||||
@RequestParam("file")MultipartFile file,
|
||||
@Valid @ModelAttribute ClipOptions options
|
||||
) throws IOException, InterruptedException {
|
||||
) throws IOException {
|
||||
Job job = mediaService.compress(file, options);
|
||||
return ResponseEntity.ok(new APIResponse<>(
|
||||
SUCCESS,
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.springframework.http.MediaTypeFactory;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@@ -54,8 +55,8 @@ public class VodController {
|
||||
"Vod with ID " + id + " has been deleted"));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/media")
|
||||
public ResponseEntity<Resource> downloadVod(@PathVariable Long id) {
|
||||
@GetMapping(value = "/{id}/media", produces = MediaType.ALL_VALUE)
|
||||
public ResponseEntity<Resource> downloadVod(@PathVariable Long id) throws IOException {
|
||||
Resource resource = vodService.downloadVod(id);
|
||||
|
||||
if (resource == null || !resource.exists()) {
|
||||
@@ -63,8 +64,11 @@ public class VodController {
|
||||
}
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, String.format(FILENAME_HEADER, resource.getFilename()))
|
||||
.contentType(MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM))
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||
String.format(FILENAME_HEADER, resource.getFilename()))
|
||||
.contentType(MediaTypeFactory.getMediaType(resource)
|
||||
.orElse(MediaType.APPLICATION_OCTET_STREAM))
|
||||
.contentLength(resource.contentLength())
|
||||
.body(resource);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.ddf.vodsystem.exceptions;
|
||||
|
||||
public class ClipMediaUnavailable extends RuntimeException {
|
||||
private final Long clipId;
|
||||
|
||||
public ClipMediaUnavailable(Long clipId, String message) {
|
||||
this(clipId, message, null);
|
||||
}
|
||||
|
||||
public ClipMediaUnavailable(Long clipId, String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.clipId = clipId;
|
||||
}
|
||||
|
||||
public Long getClipId() {
|
||||
return clipId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.ddf.vodsystem.exceptions;
|
||||
|
||||
public class VodMediaUnavailable extends RuntimeException {
|
||||
private final Long vodId;
|
||||
|
||||
public VodMediaUnavailable(Long vodId, String message) {
|
||||
this(vodId, message, null);
|
||||
}
|
||||
|
||||
public VodMediaUnavailable(Long vodId, String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.vodId = vodId;
|
||||
}
|
||||
|
||||
public Long getVodId() {
|
||||
return vodId;
|
||||
}
|
||||
}
|
||||
@@ -156,6 +156,10 @@ public class ClipService {
|
||||
throw new ClipNotFound("Clip file not found");
|
||||
}
|
||||
|
||||
if (!Files.isReadable(file)) {
|
||||
throw new ClipMediaUnavailable(id, "Clip file is not readable");
|
||||
}
|
||||
|
||||
return new FileSystemResource(file);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
@@ -129,11 +130,29 @@ public class DirectoryService {
|
||||
}
|
||||
}
|
||||
|
||||
private void checkDirPermissions() {
|
||||
List<Path> dirs = List.of(
|
||||
Path.of(tempInputsDir),
|
||||
Path.of(tempOutputsDir),
|
||||
Path.of(usersDir)
|
||||
);
|
||||
|
||||
List<Path> bad = dirs.stream()
|
||||
.filter(p -> !Files.isDirectory(p) || !Files.isReadable(p) || !Files.isWritable(p))
|
||||
.toList();
|
||||
|
||||
if (!bad.isEmpty()) {
|
||||
throw new IllegalStateException("Unusable directories: " + bad);
|
||||
}
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void createDirectoriesIfNotExist() throws IOException {
|
||||
Files.createDirectories(Path.of(tempInputsDir));
|
||||
Files.createDirectories(Path.of(tempOutputsDir));
|
||||
Files.createDirectories(Path.of(usersDir));
|
||||
|
||||
checkDirPermissions();
|
||||
}
|
||||
|
||||
@Scheduled(fixedRate = TEMP_DIR_CLEANUP_RATE)
|
||||
|
||||
@@ -93,7 +93,7 @@ public class MediaService {
|
||||
);
|
||||
}).exceptionally(ex -> {
|
||||
job.setState(JobState.FAILED);
|
||||
job.setErrorOutput(ex.getMessage());
|
||||
job.setErrorOutput("Compression job failed");
|
||||
logger.error("Compression job with UUID {} failed due to: {}", job.getUuid(), ex.getMessage());
|
||||
return null;
|
||||
});
|
||||
|
||||
@@ -139,6 +139,10 @@ public class VodService {
|
||||
throw new VodNotFound("Vod file not found for ID: " + id);
|
||||
}
|
||||
|
||||
if (!Files.isReadable(file)) {
|
||||
throw new VodMediaUnavailable(id, "Vod file is not readable");
|
||||
}
|
||||
|
||||
return new FileSystemResource(file);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user