reghao 3 лет назад
Родитель
Сommit
446541d7ae

+ 0 - 4
src/main/java/cn/reghao/dfs/store/config/DfsProperties.java

@@ -1,8 +1,6 @@
 package cn.reghao.dfs.store.config;
 
-import lombok.AllArgsConstructor;
 import lombok.Getter;
-import lombok.NoArgsConstructor;
 import lombok.Setter;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
@@ -13,8 +11,6 @@ import java.util.List;
  * @author reghao
  * @date 2021-12-30 11:01:46
  */
-@AllArgsConstructor
-@NoArgsConstructor
 @Getter
 @Setter
 @Component

+ 17 - 11
src/main/java/cn/reghao/dfs/store/controller/ImageFileController.java

@@ -1,5 +1,6 @@
 package cn.reghao.dfs.store.controller;
 
+import cn.reghao.dfs.store.model.dto.PathUrl;
 import cn.reghao.dfs.store.service.FileUrlService;
 import cn.reghao.dfs.store.service.media.ImageFileService;
 import org.springframework.core.io.InputStreamResource;
@@ -9,7 +10,9 @@ import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletResponse;
 import java.io.*;
+import java.nio.charset.StandardCharsets;
 
 /**
  * @author reghao
@@ -26,15 +29,17 @@ public class ImageFileController {
         this.imageFileService = imageFileService;
     }
 
-    @GetMapping("/image/{filename}")
-    public ResponseEntity<InputStreamResource> image(@PathVariable("filename") String filename) throws Exception {
+    @GetMapping("/image2/{filename}")
+    @Deprecated
+    public ResponseEntity<InputStreamResource> image2(@PathVariable("filename") String filename) throws Exception {
         String uploadId = filename.split("\\.")[0];
-        String filePath = fileUrlService.getFilePath(uploadId);
-        if (filePath == null) {
+        PathUrl pathUrl = fileUrlService.getPathUrl(uploadId);
+        if (pathUrl == null) {
             return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
         }
 
-        FileInputStream fis = new FileInputStream(filePath);
+        String absolutePath = pathUrl.getAbsolutePath();
+        FileInputStream fis = new FileInputStream(absolutePath);
         InputStreamResource inputStreamResource = new InputStreamResource(fis);
 
         HttpHeaders httpHeaders = new HttpHeaders();
@@ -44,23 +49,24 @@ public class ImageFileController {
                 .body(inputStreamResource);
     }
 
-    @GetMapping("/image1/{filename:.+}")
-    public ResponseEntity<InputStreamResource> image1(@PathVariable("filename") String filename) throws Exception {
+    @GetMapping("/image/{filename:.+}")
+    public ResponseEntity<InputStreamResource> image(@PathVariable("filename") String filename) throws Exception {
         String[] strs = filename.split("@");
         String uploadId = strs[0].split("\\.")[0];
-        String filePath = fileUrlService.getFilePath(uploadId);
-        if (filePath == null) {
+        PathUrl pathUrl = fileUrlService.getPathUrl(uploadId);
+        if (pathUrl == null) {
             return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
         }
 
+        String absolutePath = pathUrl.getAbsolutePath();
         InputStream inputStream;
         if (strs.length == 1) {
-            inputStream = new FileInputStream(filePath);
+            inputStream = new FileInputStream(absolutePath);
         } else {
             String[] params = strs[1].split("_");
             int width = Integer.parseInt(params[0].replace("w", ""));
             int height = Integer.parseInt(params[1].replace("h", ""));
-            inputStream = imageFileService.getImageStream(filePath, width, height);
+            inputStream = imageFileService.getImageStream(absolutePath, width, height);
         }
 
         InputStreamResource inputStreamResource = new InputStreamResource(inputStream);

+ 33 - 10
src/main/java/cn/reghao/dfs/store/controller/VideoFileController.java

@@ -1,5 +1,6 @@
 package cn.reghao.dfs.store.controller;
 
+import cn.reghao.dfs.store.model.dto.PathUrl;
 import cn.reghao.dfs.store.service.FileUrlService;
 import org.springframework.web.bind.annotation.*;
 
@@ -28,14 +29,15 @@ public class VideoFileController {
     public void videoPlayer(@PathVariable("filename") String filename, @RequestHeader(required = false) String range,
                             HttpServletResponse response) throws Exception {
         String uploadId = filename.split("\\.")[0];
-        String filePath = fileUrlService.getFilePath(uploadId);
-        if (filePath == null) {
+        PathUrl pathUrl = fileUrlService.getPathUrl(uploadId);
+        if (pathUrl == null) {
             response.setStatus(HttpServletResponse.SC_NOT_FOUND);
             response.getOutputStream().write("".getBytes(StandardCharsets.UTF_8));
             return;
         }
 
-        RandomAccessFile raf = new RandomAccessFile(filePath, "r");
+        String absolutePath = pathUrl.getAbsolutePath();
+        RandomAccessFile raf = new RandomAccessFile(absolutePath, "r");
         long fileLength = raf.length();
         long partLength = 1024*1024*2;
 
@@ -77,10 +79,17 @@ public class VideoFileController {
             return;
         }
 
-        String filePath = fileUrlService.getFilePath(uploadId);
-        File file = new File(filePath);
+        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(filePath);
+        FileInputStream fis = new FileInputStream(absolutePath);
         response.setContentType("video/mp4");
         response.setHeader("Content-Length", ""+len);
         response.setStatus(HttpServletResponse.SC_OK);
@@ -93,16 +102,30 @@ public class VideoFileController {
                                 @RequestHeader(required = false) String range,
                                 HttpServletResponse response) throws IOException {
         if (filename.contains("mpd")) {
-            String localFilePath = fileUrlService.getFilePath(uploadId);
-            FileInputStream fis = new FileInputStream(localFilePath);
+            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;
         }
 
-        String filePath = fileUrlService.getFilePath(uploadId);
-        String parentDir = new File(filePath).getParent();
+        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();

+ 1 - 9
src/main/java/cn/reghao/dfs/store/db/mapper/FileUrlMapper.java

@@ -1,14 +1,11 @@
 package cn.reghao.dfs.store.db.mapper;
 
-import cn.reghao.dfs.store.model.dto.FileUrlDto;
 import cn.reghao.dfs.store.model.dto.PathUrl;
 import cn.reghao.jutil.jdk.db.BaseMapper;
 import cn.reghao.dfs.store.model.po.FileUrl;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
-import java.util.List;
-
 /**
  * @author reghao
  * @date 2021-12-08 14:20:12
@@ -20,10 +17,5 @@ public interface FileUrlMapper extends BaseMapper<FileUrl> {
                           @Param("blockId") String blockId,
                           @Param("filePath") String filePath);
 
-    FileUrlDto findByUploadId(String uploadId);
-    List<FileUrl> findByFileId(String fileId);
-    PathUrl findPathUrl(@Param("fileId") String fileId, @Param("group") int group, @Param("node") int node);
-
-    List<FileUrl> findAllVideoUrl();
-    List<FileUrl> findAllImageUrl();
+    PathUrl findPathUrl(@Param("uploadId") String uploadId, @Param("group") int group, @Param("node") int node);
 }

+ 1 - 10
src/main/java/cn/reghao/dfs/store/db/repository/FileRepository.java

@@ -31,7 +31,7 @@ public class FileRepository {
     private final IdGenerator idGenerator;
     private final FileInfoMapper fileInfoMapper;
     private final FileUrlMapper fileUrlMapper;
-    private FilePathMapper filePathMapper;
+    private final FilePathMapper filePathMapper;
     private final FileUserMapper fileUserMapper;
 
     public FileRepository(FileInfoMapper fileInfoMapper, FileUrlMapper fileUrlMapper, FilePathMapper filePathMapper,
@@ -103,19 +103,10 @@ public class FileRepository {
         return fileInfoMapper.findFileInfoBySha256sum(sha256sum);
     }
 
-    public FileInfo getFileInfoByUploadId(String uploadId) {
-        return fileUserMapper.findFileInfoByUploadId(uploadId);
-    }
-
     public FileInfo getFileInfoByFileId(String uploadId) {
         return fileUserMapper.findFileInfoByUploadId(uploadId);
     }
 
-    public String getPathUrlByFileId(String fileId) {
-        List<FileUrl> list = fileUrlMapper.findByFileId(fileId);
-        return list.get(0).getUrl();
-    }
-
     @Transactional(rollbackFor = Exception.class)
     public void deleteFile(String uploadId) {
         String currentUserId = String.valueOf(ServletUtil.getUserId());

+ 0 - 21
src/main/java/cn/reghao/dfs/store/model/dto/FileUrlDto.java

@@ -1,21 +0,0 @@
-package cn.reghao.dfs.store.model.dto;
-
-import lombok.Getter;
-
-import java.io.Serializable;
-
-/**
- * @author reghao
- * @date 2022-04-26 10:59:16
- */
-@Getter
-@Deprecated
-public class FileUrlDto implements Serializable {
-    private static final long serialVersionUID = 1L;
-
-    private String fileId;
-    private String absolutePath;
-    private String filename;
-    private String url;
-    private String path;
-}

+ 2 - 0
src/main/java/cn/reghao/dfs/store/model/dto/PathUrl.java

@@ -2,6 +2,7 @@ package cn.reghao.dfs.store.model.dto;
 
 import lombok.AllArgsConstructor;
 import lombok.Getter;
+import lombok.NoArgsConstructor;
 
 /**
  * 文件的本地路径和 URL
@@ -10,6 +11,7 @@ import lombok.Getter;
  * @date 2022-04-26 15:10:04
  */
 @AllArgsConstructor
+@NoArgsConstructor
 @Getter
 public class PathUrl {
     // 本地文件路径

+ 2 - 3
src/main/java/cn/reghao/dfs/store/service/FileDownloadService.java

@@ -1,7 +1,6 @@
 package cn.reghao.dfs.store.service;
 
 import cn.reghao.dfs.store.model.dto.DownloadFile;
-import cn.reghao.dfs.store.model.dto.FileUrlDto;
 import cn.reghao.jutil.web.ServletUtil;
 import org.springframework.stereotype.Service;
 
@@ -29,14 +28,14 @@ public class FileDownloadService {
         }
 
         String uploadId = downloadFile.getUploadId();
-        FileUrlDto fileUrlDto = fileUrlService.getFileUrl(uploadId);
+        /*FileUrlDto fileUrlDto = fileUrlService.getFileUrl(uploadId);
         if (fileUrlDto == null) {
             return;
         }
 
         String filename = fileUrlDto.getFilename();
         String path = fileUrlDto.getPath();
-        download(filename, new File(path));
+        download(filename, new File(path));*/
     }
 
     public void download(String filename, File file) throws IOException {

+ 3 - 3
src/main/java/cn/reghao/dfs/store/service/FileUploadService.java

@@ -16,6 +16,7 @@ import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.*;
+import java.nio.file.Path;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
@@ -142,9 +143,8 @@ public class FileUploadService {
         FileInfo fileInfo = fileRepository.getFileInfoByFileId(uploadId);
         // 没有检查文件的 sha256sum, 存在安全隐患
         if (fileInfo.getUploaded()) {
-            FileUrlDto fileUrlDto = fileUrlService.getFileUrl(uploadId);
-            PathUrl pathUrl = new PathUrl(fileUrlDto.getAbsolutePath(), fileUrlDto.getUrl(), fileUrlDto.getPath());
-            return new FilePartRet(uploadId, pathUrl, fileUrlDto.getFileId());
+            PathUrl pathUrl = fileUrlService.getPathUrl(uploadId);
+            return new FilePartRet(uploadId, pathUrl, "");
         }
 
         String fileId = fileInfo.getFileId();

+ 8 - 20
src/main/java/cn/reghao/dfs/store/service/FileUrlService.java

@@ -1,8 +1,6 @@
 package cn.reghao.dfs.store.service;
 
 import cn.reghao.dfs.store.config.DfsProperties;
-import cn.reghao.dfs.store.model.dto.FileUrlDto;
-import cn.reghao.dfs.store.model.po.FileUrl;
 import cn.reghao.dfs.store.util.store.LoadBalancer;
 import cn.reghao.dfs.store.util.store.StoreDir;
 import cn.reghao.dfs.store.model.dto.PathUrl;
@@ -19,12 +17,16 @@ import java.security.NoSuchAlgorithmException;
 @Service
 public class FileUrlService {
     private final String domain;
+    private final int group;
+    private final int node;
     private final LoadBalancer loadBalancer;
     private final FileUrlMapper fileUrlMapper;
     
-    public FileUrlService(LoadBalancer loadBalancer, DfsProperties dfsProperties, FileUrlMapper fileUrlMapper) {
-        this.loadBalancer = loadBalancer;
+    public FileUrlService(DfsProperties dfsProperties, LoadBalancer loadBalancer, FileUrlMapper fileUrlMapper) {
         this.domain = dfsProperties.getDomain();
+        this.group = dfsProperties.getGroup();
+        this.node = dfsProperties.getNode();
+        this.loadBalancer = loadBalancer;
         this.fileUrlMapper = fileUrlMapper;
     }
 
@@ -55,21 +57,7 @@ public class FileUrlService {
         return new PathUrl(filePath, url, path);
     }
 
-    public PathUrl getPathUrl(String fileId) {
-        return fileUrlMapper.findPathUrl(fileId, 0, 0);
-    }
-
-    @Deprecated
-    public String getFilePath(String uploadId) {
-        FileUrlDto fileUrlDto = fileUrlMapper.findByUploadId(uploadId);
-        if (fileUrlDto != null) {
-            return fileUrlDto.getAbsolutePath();
-        }
-        return null;
-    }
-
-    @Deprecated
-    public FileUrlDto getFileUrl(String uploadId) {
-        return fileUrlMapper.findByUploadId(uploadId);
+    public PathUrl getPathUrl(String uploadId) {
+        return fileUrlMapper.findPathUrl(uploadId, group, node);
     }
 }

+ 0 - 4
src/main/resources/mapper/FilePathMapper.xml

@@ -8,9 +8,5 @@
         values
         (#{id},#{deleted},#{createTime},#{updateTime},#{fileId},#{blockId},#{absolutePath},#{group},#{node})
     </insert>
-
-    <select id="findByUploadId" resultType="cn.reghao.dfs.store.model.dto.FileUrlDto">
-        select * from file_path
-    </select>
 </mapper>
 

+ 4 - 11
src/main/resources/mapper/FileUrlMapper.xml

@@ -9,21 +9,14 @@
         (#{fileId},#{url},#{path},#{group},#{node})
     </insert>
 
-    <select id="findAllVideoUrl" resultType="cn.reghao.dfs.store.model.po.FileUrl">
-        select * from file_url
-        where path like '%video/playback%';
-    </select>
-    <select id="findAllImageUrl" resultType="cn.reghao.dfs.store.model.po.FileUrl">
-        select * from file_url
-        where path like '%image%';
-    </select>
-
-    <select id="findByFileId" resultType="cn.reghao.dfs.store.model.po.FileUrl">
+    <select id="findByUploadId" resultType="cn.reghao.dfs.store.model.po.FileUrl">
         select * from file_url where file_id=#{fileId}
     </select>
     <select id="findPathUrl" resultType="cn.reghao.dfs.store.model.dto.PathUrl">
         select * from file_url url
         inner join file_path path
-        where url.file_id=path.file_id and path.`group`=#{group} and path.node=#{node} and url.file_id=#{fileId}
+        inner join file_user fileUser
+        where url.file_id=path.file_id and fileUser.file_id=url.file_id and path.`group`=#{group} and path.node=#{node}
+        and fileUser.upload_id=#{uploadId}
     </select>
 </mapper>