Переглянути джерело

修正分片文件上传 bug

reghao 3 роки тому
батько
коміт
3898e6063a

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

@@ -36,6 +36,7 @@ public class FileUploadService {
     private final FileTypeService fileTypeService;
     private final FileRepository fileRepository;
     private Map<String, Set<Integer>> map = new HashMap<>();
+    private Map<String, PathUrl> pathUrlMap = new HashMap<>();
 
     public FileUploadService(FileUrlService fileUrlService, FileStoreService fileStoreService,
                              FileTypeService fileTypeService, FileRepository fileRepository) {
@@ -132,9 +133,11 @@ public class FileUploadService {
      */
     @Transactional(rollbackFor = Exception.class)
     public synchronized FilePartRet putFilePart(UploadFilePart uploadFilePart) throws Exception {
+        int totalParts = uploadFilePart.getTotalChunks();
         int partIndex = uploadFilePart.getChunkNumber();
-        int partNum = uploadFilePart.getTotalChunks();
+        int currentPartSize = uploadFilePart.getCurrentChunkSize();
         MultipartFile multipartFile = uploadFilePart.getFile();
+
         String uploadId = uploadFilePart.getIdentifier();
         FileInfo fileInfo = fileRepository.getFileInfoByFileId(uploadId);
         // 没有检查文件的 sha256sum, 存在安全隐患
@@ -149,33 +152,35 @@ public class FileUploadService {
         String suffix = fileInfo.getSuffix();
         String contentType = fileInfo.getContentType();
         long size = fileInfo.getSize();
-        PathUrl pathUrl = fileUrlService.genPathUrl(contentType, sha256sum, size, fileId, suffix, uploadId);
         Set<Integer> set = map.computeIfAbsent(fileId, k -> new HashSet<>());
         if (set.isEmpty()) {
+            PathUrl pathUrl = fileUrlService.genPathUrl(contentType, sha256sum, size, fileId, suffix, uploadId);
             fileStoreService.createSparseFile(pathUrl.getAbsolutePath(), size);
+            pathUrlMap.put(fileId, pathUrl);
         }
 
         if (!set.contains(partIndex)) {
             set.add(partIndex);
             long pos = (partIndex-1) * PART_SIZE;
-            String absolutePath = pathUrl.getAbsolutePath();
+            String absolutePath = pathUrlMap.get(fileId).getAbsolutePath();
             fileStoreService.writeToFile(multipartFile.getInputStream(), absolutePath, pos);
         }
 
-        long len = set.size();
-        if (len != partNum) {
+        if (set.size() != totalParts) {
             return new FilePartRet(fileId);
         } else {
-            String absolutePath = pathUrl.getAbsolutePath();
+            String absolutePath = pathUrlMap.get(fileId).getAbsolutePath();
             FileInputStream fis = new FileInputStream(absolutePath);
             String sha256sumMerged = DigestUtil.sha256sum(fis);
             if (!sha256sum.equals(sha256sumMerged)) {
-                throw new Exception("分片合并文件的 sha256sum 与原文件一致!");
+                throw new Exception("分片合并文件的 sha256sum 与原文件一致!");
             }
 
+            PathUrl pathUrl = pathUrlMap.get(fileId);
             FileContentType fileType = fileTypeService.getFileType(fileInfo.getContentType(), absolutePath);
             fileRepository.saveFile(fileId, fileType, pathUrl);
             map.remove(fileId);
+            pathUrlMap.remove(fileId);
             return new FilePartRet(uploadId, pathUrl, fileId);
         }
     }