|
|
@@ -1,19 +1,14 @@
|
|
|
package cn.reghao.dfs.store.controller;
|
|
|
|
|
|
import cn.reghao.dfs.store.config.DfsProperties;
|
|
|
-import cn.reghao.dfs.store.db.mapper.FileUserMapper;
|
|
|
-import cn.reghao.dfs.store.model.dto.PathUrl;
|
|
|
-import cn.reghao.dfs.api.dto.FileAccess;
|
|
|
-import cn.reghao.dfs.store.service.FileUrlService;
|
|
|
+import cn.reghao.dfs.store.db.mapper.VideoFileMapper;
|
|
|
+import cn.reghao.dfs.store.model.vo.VideoFileInfo;
|
|
|
import cn.reghao.jutil.jdk.security.crypto.AesEncrypt;
|
|
|
import cn.reghao.jutil.web.ServletUtil;
|
|
|
import io.swagger.annotations.Api;
|
|
|
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;
|
|
|
@@ -26,15 +21,16 @@ import java.util.Arrays;
|
|
|
@RestController
|
|
|
@RequestMapping("/video")
|
|
|
public class VideoFileController {
|
|
|
- private final FileUrlService fileUrlService;
|
|
|
- private final FileUserMapper fileUserMapper;
|
|
|
+ private final VideoFileMapper videoFileMapper;
|
|
|
private final String encryptKey;
|
|
|
+ private final int group;
|
|
|
+ private final int node;
|
|
|
|
|
|
- public VideoFileController(FileUrlService fileUrlService, FileUserMapper fileUserMapper,
|
|
|
- DfsProperties dfsProperties) {
|
|
|
- this.fileUrlService = fileUrlService;
|
|
|
- this.fileUserMapper = fileUserMapper;
|
|
|
+ public VideoFileController(VideoFileMapper videoFileMapper, DfsProperties dfsProperties) {
|
|
|
+ this.videoFileMapper = videoFileMapper;
|
|
|
this.encryptKey = dfsProperties.getEncryptKey();
|
|
|
+ this.group = dfsProperties.getGroup();
|
|
|
+ this.node = dfsProperties.getNode();
|
|
|
}
|
|
|
|
|
|
@GetMapping("/playback/{filename}")
|
|
|
@@ -43,56 +39,52 @@ public class VideoFileController {
|
|
|
@RequestParam(value = "sign", required = false) String sign,
|
|
|
HttpServletResponse response) throws Exception {
|
|
|
String uploadId = filename.split("\\.")[0];
|
|
|
- FileAccess fileAccess = fileUserMapper.findFileAccessByUploadId(uploadId);
|
|
|
- if (fileAccess == null) {
|
|
|
+ VideoFileInfo videoFileInfo = videoFileMapper.findVideoFileInfoByUploadId(uploadId, group, node);
|
|
|
+ if (videoFileInfo == null) {
|
|
|
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
|
|
response.getOutputStream().write("".getBytes(StandardCharsets.UTF_8));
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- if (!fileAccess.isActivate()) {
|
|
|
- response.setStatus(HttpServletResponse.SC_FORBIDDEN);
|
|
|
- response.getOutputStream().write("".getBytes(StandardCharsets.UTF_8));
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (fileAccess.isAuth()) {
|
|
|
- if (sign == null) {
|
|
|
- response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
+ long accessUserId = Long.parseLong(ServletUtil.getUserId());
|
|
|
+ long uploadBy = Long.parseLong(videoFileInfo.getUploadBy());
|
|
|
+ if (accessUserId != -1) {
|
|
|
+ if (!videoFileInfo.isActivate()) {
|
|
|
+ response.setStatus(HttpServletResponse.SC_FORBIDDEN);
|
|
|
response.getOutputStream().write("".getBytes(StandardCharsets.UTF_8));
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- String decryptSign = AesEncrypt.decrypt(sign, encryptKey);
|
|
|
- String[] arr = decryptSign.split("_");
|
|
|
- String key = arr[0];
|
|
|
- long userId = Long.parseLong(arr[1]);
|
|
|
- long timestamp = Long.parseLong(arr[2]);
|
|
|
-
|
|
|
- long accessUserId = Long.parseLong(ServletUtil.getUserId());
|
|
|
- if (userId != accessUserId) {
|
|
|
- response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
- String errMsg = "url 不是为当前用户生成";
|
|
|
- response.getOutputStream().write(errMsg.getBytes(StandardCharsets.UTF_8));
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (timestamp < System.currentTimeMillis()) {
|
|
|
- response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
- String errMsg = "url 过期";
|
|
|
- response.getOutputStream().write(errMsg.getBytes(StandardCharsets.UTF_8));
|
|
|
- return;
|
|
|
+ if (videoFileInfo.isAuth()) {
|
|
|
+ if (sign == null) {
|
|
|
+ response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
+ response.getOutputStream().write("".getBytes(StandardCharsets.UTF_8));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String decryptSign = AesEncrypt.decrypt(sign, encryptKey);
|
|
|
+ String[] arr = decryptSign.split("_");
|
|
|
+ String key = arr[0];
|
|
|
+ long userId = Long.parseLong(arr[1]);
|
|
|
+ long timestamp = Long.parseLong(arr[2]);
|
|
|
+
|
|
|
+ if (userId != accessUserId) {
|
|
|
+ response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
+ String errMsg = "url 不是为当前用户生成";
|
|
|
+ response.getOutputStream().write(errMsg.getBytes(StandardCharsets.UTF_8));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (timestamp < System.currentTimeMillis()) {
|
|
|
+ response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
+ String errMsg = "url 过期";
|
|
|
+ response.getOutputStream().write(errMsg.getBytes(StandardCharsets.UTF_8));
|
|
|
+ return;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- PathUrl pathUrl = fileUrlService.getPathUrl(uploadId);
|
|
|
- if (pathUrl == null) {
|
|
|
- response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
|
|
- response.getOutputStream().write("".getBytes(StandardCharsets.UTF_8));
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- String absolutePath = pathUrl.getAbsolutePath();
|
|
|
+ String absolutePath = videoFileInfo.getAbsolutePath();
|
|
|
RandomAccessFile raf = new RandomAccessFile(absolutePath, "r");
|
|
|
long fileLength = raf.length();
|
|
|
long partLength = 1024*1024*2;
|
|
|
@@ -121,88 +113,4 @@ public class VideoFileController {
|
|
|
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
|
|
|
response.getOutputStream().write(Arrays.copyOf(bytes, readBytes));
|
|
|
}
|
|
|
-
|
|
|
- @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;
|
|
|
- }
|
|
|
-
|
|
|
- PathUrl pathUrl = fileUrlService.getPathUrl(uploadId);
|
|
|
- if (pathUrl == null) {
|
|
|
- response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
|
|
- response.getOutputStream().write("".getBytes(StandardCharsets.UTF_8));
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- String absolutePath = pathUrl.getAbsolutePath();
|
|
|
- File file = new File(absolutePath);
|
|
|
- long len = file.length();
|
|
|
- FileInputStream fis = new FileInputStream(absolutePath);
|
|
|
- response.setContentType("video/mp4");
|
|
|
- response.setHeader("Content-Length", ""+len);
|
|
|
- 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")) {
|
|
|
- PathUrl pathUrl = fileUrlService.getPathUrl(uploadId);
|
|
|
- if (pathUrl == null) {
|
|
|
- response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
|
|
- response.getOutputStream().write("".getBytes(StandardCharsets.UTF_8));
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- String absolutePath = pathUrl.getAbsolutePath();
|
|
|
- FileInputStream fis = new FileInputStream(absolutePath);
|
|
|
- response.setStatus(HttpServletResponse.SC_OK);
|
|
|
- response.setContentType("application/dash+xml; charset=utf-8");
|
|
|
- response.getOutputStream().write(fis.readAllBytes());
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- PathUrl pathUrl = fileUrlService.getPathUrl(uploadId);
|
|
|
- if (pathUrl == null) {
|
|
|
- response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
|
|
- response.getOutputStream().write("".getBytes(StandardCharsets.UTF_8));
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- String absolutePath = pathUrl.getAbsolutePath();
|
|
|
- String parentDir = new File(absolutePath).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));
|
|
|
- }
|
|
|
}
|