|
|
@@ -2,15 +2,14 @@ package cn.reghao.tnb.file.app.controller;
|
|
|
|
|
|
import cn.reghao.tnb.file.app.db.mapper.FileUrlMapper;
|
|
|
import cn.reghao.tnb.file.app.model.dto.FileUrlDto;
|
|
|
-import org.springframework.core.io.InputStreamResource;
|
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
-import org.springframework.http.HttpStatus;
|
|
|
-import org.springframework.http.MediaType;
|
|
|
-import org.springframework.http.ResponseEntity;
|
|
|
+import cn.reghao.tnb.file.app.service.FileAccessService;
|
|
|
+import cn.reghao.tnb.file.app.util.LocalStores;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
import java.io.RandomAccessFile;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Arrays;
|
|
|
@@ -20,27 +19,28 @@ import java.util.Arrays;
|
|
|
* @date 2022-04-24 16:13:10
|
|
|
*/
|
|
|
@RestController
|
|
|
-@RequestMapping
|
|
|
+@RequestMapping("/video")
|
|
|
public class VideoFileController {
|
|
|
+ private final FileAccessService fileAccessService;
|
|
|
private final FileUrlMapper fileUrlMapper;
|
|
|
|
|
|
- public VideoFileController(FileUrlMapper fileUrlMapper) {
|
|
|
+ public VideoFileController(FileAccessService fileAccessService, FileUrlMapper fileUrlMapper) {
|
|
|
+ this.fileAccessService = fileAccessService;
|
|
|
this.fileUrlMapper = fileUrlMapper;
|
|
|
}
|
|
|
|
|
|
- @GetMapping("/video/{uploadId}")
|
|
|
- public void videoFile(@PathVariable("uploadId") String uploadId,
|
|
|
- @RequestHeader(required = false) String range,
|
|
|
- HttpServletResponse response) throws Exception {
|
|
|
- }
|
|
|
-
|
|
|
- @GetMapping("/video/playback")
|
|
|
- public void videoPlayer(@RequestParam("uploadId") String uploadId,
|
|
|
- @RequestHeader(required = false) String range,
|
|
|
+ @GetMapping("/playback/{filename}")
|
|
|
+ public void videoPlayer(@PathVariable("filename") String filename, @RequestHeader(required = false) String range,
|
|
|
HttpServletResponse response) throws Exception {
|
|
|
- fileUrlMapper.findByUploadId(uploadId);
|
|
|
+ String uploadId = filename.split("\\.")[0];
|
|
|
+ FileUrlDto fileUrlDto = fileUrlMapper.findByUploadId(uploadId);
|
|
|
+ if (fileUrlDto == null) {
|
|
|
+ response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
|
|
+ response.getOutputStream().write("".getBytes(StandardCharsets.UTF_8));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String filePath = LocalStores.getMountedOn(fileUrlDto.getBlockId()) + "/" + fileUrlDto.getFilePath();
|
|
|
|
|
|
- String filePath = "/home/reghao/Downloads/mp4/test.mp4";
|
|
|
RandomAccessFile raf = new RandomAccessFile(filePath, "r");
|
|
|
long fileLength = raf.length();
|
|
|
long partLength = 1024*1024*2;
|
|
|
@@ -70,17 +70,65 @@ public class VideoFileController {
|
|
|
response.getOutputStream().write(Arrays.copyOf(bytes, readBytes));
|
|
|
}
|
|
|
|
|
|
- @GetMapping("/image/{uploadId}")
|
|
|
- public ResponseEntity<InputStreamResource> videoPlayer(@PathVariable("uploadId") String uploadId) throws Exception {
|
|
|
- FileUrlDto fileUrlDto = fileUrlMapper.findByUploadId(uploadId);
|
|
|
- String filePath = fileUrlDto.getPath();
|
|
|
- FileInputStream fis = new FileInputStream(filePath);
|
|
|
- InputStreamResource inputStreamResource = new InputStreamResource(fis);
|
|
|
-
|
|
|
- HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
- httpHeaders.set("Pragma", "No-cache");
|
|
|
- httpHeaders.set("Cache-Control", "no-cache");
|
|
|
- return ResponseEntity.status(HttpStatus.OK).headers(httpHeaders).contentType(MediaType.IMAGE_JPEG)
|
|
|
- .body(inputStreamResource);
|
|
|
+ @GetMapping("/hls/{uploadId}/{filename:.+}")
|
|
|
+ public void hlsVideoPlayer(@PathVariable("uploadId") String uploadId,
|
|
|
+ @PathVariable("filename") String filename,
|
|
|
+ HttpServletResponse response) throws IOException {
|
|
|
+ if (filename.contains("m3u8")) {
|
|
|
+ String localFilePath = "/opt/file/disk/spider/twitter/vid/hls/0/" + filename;
|
|
|
+ FileInputStream fis = new FileInputStream(localFilePath);
|
|
|
+ response.setStatus(HttpServletResponse.SC_OK);
|
|
|
+ response.setContentType("application/x-mpegURL");
|
|
|
+ response.getOutputStream().write(fis.readAllBytes());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String localFilePath = "/opt/file/disk/spider/twitter/vid/hls/0/" + filename;
|
|
|
+ FileInputStream fis = new FileInputStream(localFilePath);
|
|
|
+
|
|
|
+ response.setContentType("video/mp4");
|
|
|
+ response.setHeader("Content-Length", ""+fis.available());
|
|
|
+ response.setStatus(HttpServletResponse.SC_OK);
|
|
|
+ response.getOutputStream().write(fis.readAllBytes());
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/dash/{uploadId}/{filename:.+}")
|
|
|
+ public void dashVideoPlayer(@PathVariable("uploadId") String uploadId,
|
|
|
+ @PathVariable("filename") String filename,
|
|
|
+ @RequestHeader(required = false) String range,
|
|
|
+ HttpServletResponse response) throws IOException {
|
|
|
+ if (filename.contains("mpd")) {
|
|
|
+ String localFilePath = fileAccessService.getLocalFilePath(uploadId);
|
|
|
+ FileInputStream fis = new FileInputStream(localFilePath);
|
|
|
+ response.setStatus(HttpServletResponse.SC_OK);
|
|
|
+ response.setContentType("application/dash+xml; charset=utf-8");
|
|
|
+ response.getOutputStream().write(fis.readAllBytes());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String localFilePath = fileAccessService.getLocalFilePath(uploadId);
|
|
|
+ String parentDir = new File(localFilePath).getParent();
|
|
|
+ String localFilePath1 = parentDir + File.separator + filename;
|
|
|
+ RandomAccessFile raf = new RandomAccessFile(localFilePath1, "r");
|
|
|
+ long fileLength = raf.length();
|
|
|
+
|
|
|
+ String[] ranges = range.replace("bytes=", "").split(",");
|
|
|
+ String[] firstRange = ranges[0].split("-");
|
|
|
+ long start = Long.parseLong(firstRange[0]);
|
|
|
+ long end = Long.parseLong(firstRange[1]);
|
|
|
+ long expectRead = end-start+1;
|
|
|
+ byte[] bytes = new byte[(int) (expectRead)];
|
|
|
+
|
|
|
+ raf.seek(start);
|
|
|
+ int actualRead = raf.read(bytes);
|
|
|
+ //long end1 = start + actualRead-1;
|
|
|
+ long end1 = Math.min(start+end-1, start+actualRead-1);
|
|
|
+
|
|
|
+ response.setContentType("video/mp4");
|
|
|
+ response.setHeader("Content-Length", ""+actualRead);
|
|
|
+ response.setHeader("Content-Range", "bytes "+start+"-"+end1+"/"+fileLength);
|
|
|
+ response.setHeader("Accept-Ranges", "bytes");
|
|
|
+ response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
|
|
|
+ response.getOutputStream().write(Arrays.copyOf(bytes, actualRead));
|
|
|
}
|
|
|
}
|