테스트 영상은 여기서 다운받았다.
업로드 한 영상을 재생 해보자.
VodController
@Controller
public class VodController {
@Value("${tus.save.path}")
private String savedPath;
@ResponseBody
@GetMapping("/vod/{date}/{filename}")
public ResponseEntity<Resource> resource(
@PathVariable String date,
@PathVariable String filename
) throws IOException {
String path = savedPath + "/" + date + "/" + filename;
Resource resource = new FileSystemResource(path);
return ResponseEntity.ok()
.contentType(MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM))
.contentLength(resource.contentLength())
.body(resource);
}
}
GET http://localhost:8080/vod/{date}/{filename}
@Controller
public class VodController {
@Value("${tus.save.path}")
private String savedPath;
@ResponseBody
@GetMapping("/vod/chunk/{date}/{filename}")
public ResponseEntity<ResourceRegion> chunkResource(
@RequestHeader HttpHeaders headers,
@PathVariable String date,
@PathVariable String filename
) throws IOException {
String path = savedPath + "/" + date + "/" + filename;
Resource resource = new FileSystemResource(path);
long chunkSize = 1024 * 1024;
long contentLength = resource.contentLength();
HttpRange httpRange = headers.getRange().stream().findFirst()
.orElse(HttpRange.createByteRange(0, contentLength - 1));
long rangeLength = calculateRangeLength(httpRange, contentLength, chunkSize);
ResourceRegion region = new ResourceRegion(resource, httpRange.getRangeStart(contentLength), rangeLength);
return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT)
.cacheControl(CacheControl.maxAge(10, TimeUnit.MINUTES))
.contentType(MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM))
.header("Accept-Ranges", "bytes")
.eTag(path)
.body(region);
}
private long calculateRangeLength(HttpRange httpRange, long contentLength, long chunkSize) {
long start = httpRange.getRangeStart(contentLength);
long end = httpRange.getRangeEnd(contentLength);
return Long.min(chunkSize, end - start + 1);
}
}
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Uploaded vod</title>
</head>
<body>
<div style="display: flex; align-items: start">
<div>
<h3>normal</h3>
<video src="http://localhost:8080/vod/2023-03-12/f0704db526574661885e1ed87f3fa745.mp4"
width="360px"
height="240px"
autoplay
muted
controls>
</video>
</div>
<div>
<h3>chunk</h3>
<video src="http://localhost:8080/vod/chunk/2023-03-12/f0704db526574661885e1ed87f3fa745.mp4"
width="360px"
height="240px"
autoplay
muted
controls>
</video>
</div>
</div>
</body>
</html>