浏览代码

fix bug:使用旧接口请求合并文件时无法正常合并并且bucketName不合法的问题

lijie 3 年之前
父节点
当前提交
5e5366b123

+ 19 - 5
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/controller/CompatibleOldFileController.java

@@ -13,6 +13,7 @@ import com.persagy.dmp.common.utils.ResourceUtil;
 import com.persagy.dmp.file.constant.FileCommonConst;
 import com.persagy.dmp.file.context.OldFileAppContext;
 import com.persagy.dmp.file.model.FileInfo;
+import com.persagy.dmp.file.model.FileInfoCreator;
 import com.persagy.dmp.file.model.FileMd5;
 import com.persagy.dmp.file.model.FileMd5Creator;
 import com.persagy.dmp.file.service.*;
@@ -29,10 +30,8 @@ import javax.servlet.http.HttpServletResponse;
 import javax.ws.rs.core.MediaType;
 import java.io.IOException;
 import java.nio.charset.Charset;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /***
  * Description: 兼容旧的文件服务接口
@@ -258,6 +257,7 @@ public class CompatibleOldFileController {
      */
     @PostMapping(value = "/common/register_multipart_upload")
     @ResponseBody
+    @CrossOrigin(origins = "*",allowedHeaders = "*",allowCredentials = "true",methods = {RequestMethod.POST,RequestMethod.GET})
     public String registerMultipartUpload() {
         if (StrUtil.isBlank(OldFileAppContext.getContext().getSystemId())
             || StrUtil.isBlank(OldFileAppContext.getContext().getKey())){
@@ -288,6 +288,7 @@ public class CompatibleOldFileController {
     @SneakyThrows
     @RequestMapping(value = "/common/multipart_upload",consumes = {MediaType.MULTIPART_FORM_DATA})
     @ResponseBody
+    @CrossOrigin(value = "*", origins = "*",allowedHeaders = "*",allowCredentials = "true",methods = {RequestMethod.POST,RequestMethod.GET})
     public String multipartUpload(@RequestParam(value = "file",required = false) MultipartFile file,
                                   @RequestParam(value = "uploadId",required = false) String uploadId,
                                   @RequestParam(value = "chunkNumber",required = false) Integer chunkNumber,
@@ -336,7 +337,7 @@ public class CompatibleOldFileController {
         if (ObjectUtil.isEmpty(fileMd5)){
             FileMd5 fileMd5Create = FileMd5Creator.of(fileInfo.getCreator(), md5, FileCommonConst.UPLOAD_PART);
             fileMd5Create.setFileSize(totalSize);
-            fileMd5Create.setCreator(fileInfo.getCreator());
+            fileMd5Create.setCreator(FileInfoCreator.formatBucket(fileInfo.getCreator()));
             fileMd5Service.save(fileMd5Create);
         }
         Map<Integer,String> okChunkMap = service.mapChunkObjectNames(service.getChuckBucketName(null), md5);
@@ -351,6 +352,7 @@ public class CompatibleOldFileController {
     @SneakyThrows
     @RequestMapping(value = "/common/merge_multipart")
     @ResponseBody
+    @CrossOrigin(origins = "*",allowedHeaders = "*",allowCredentials = "true",methods = {RequestMethod.POST,RequestMethod.GET})
     public String mergeMultipart() {
         if (StrUtil.isBlank(OldFileAppContext.getContext().getUploadId())){
             return INCORRECT_PARAMETERS;
@@ -366,11 +368,23 @@ public class CompatibleOldFileController {
             return INCORRECT_PARAMETERS;
         }
         IFileStorageService service = FileStorageFactory.getService();
+        if (FileCommonConst.UPLOAD_SUCCESS.equals(fileMd5.getUploadStatus())
+            && StrUtil.isNotBlank(fileMd5.getFileBucket())
+            && StrUtil.isNotBlank(fileMd5.getFilePath())
+            && service.exists(fileMd5.getFileBucket(),fileMd5.getFilePath())){
+            JSONObject jsonObj = new JSONObject();
+            jsonObj.put("Result", "success");
+            jsonObj.put("ResultMsg", "");
+            return jsonObj.toJSONString();
+        }
         List<String> chunks = service
                 .listObjectNames(service.getChuckBucketName(null),fileInfo.getFileMd5());
         if (CollUtil.isEmpty(chunks)){
             return INCORRECT_PARAMETERS;
         }
+        chunks = chunks.stream()
+                .sorted(Comparator.comparingInt(n -> Integer.parseInt(StrUtil.subBetween(n, StrUtil.SLASH, StrUtil.DOT))))
+                .collect(Collectors.toList());
         fileMd5Service.ensureFilePath(fileMd5,fileInfo.getFileName());
         boolean composeObject = service.composeObject(fileInfo.getCreator(), chunks, fileMd5.getFilePath());
         JSONObject jsonObj = new JSONObject();

+ 10 - 4
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/service/impl/CompatibleOldFileServiceImpl.java

@@ -22,6 +22,7 @@ import com.persagy.dmp.file.service.*;
 import com.persagy.dmp.file.utils.FileStorageHelper;
 import lombok.RequiredArgsConstructor;
 import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -37,6 +38,7 @@ import java.util.stream.Collectors;
  */
 @Service
 @RequiredArgsConstructor
+@Slf4j
 public class CompatibleOldFileServiceImpl implements CompatibleOldFileService {
 
     private static final String PREFIX = "prefix";
@@ -253,10 +255,14 @@ public class CompatibleOldFileServiceImpl implements CompatibleOldFileService {
         FileInfo fileInfo = FileInfoCreator
                 .of(CommonConstant.DEFAULT_ID, OldFileAppContext.getContext().getKey(),IdWorker.getIdStr(),
                         null,  null);
-        fileInfo.setCreator(OldFileAppContext.getContext().getSystemId());
-        fileInfo.setId(OldFileAppContext.getContext().getKey());
-        fileService.save(fileInfo);
-        return fileInfo.getBusinessId();
+        try {
+            fileInfo.setCreator(FileInfoCreator.formatBucket(OldFileAppContext.getContext().getSystemId()));
+            fileInfo.setId(OldFileAppContext.getContext().getKey());
+            fileService.save(fileInfo);
+            return fileInfo.getBusinessId();
+        }catch (Exception e){
+            return null;
+        }
     }
 
     /***

+ 5 - 4
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/service/impl/FileServiceImpl.java

@@ -23,10 +23,8 @@ import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * 文件服务实现类
@@ -158,6 +156,9 @@ public class FileServiceImpl extends ServiceImpl<FileMapper, FileInfo> implement
         if (CollUtil.isEmpty(chunks)){
             throw new BusinessException(ResponseCode.A0400.getCode(), "分片文件数量为空");
         }
+        chunks = chunks.stream()
+                .sorted(Comparator.comparingInt(n -> Integer.parseInt(StrUtil.subBetween(n, StrUtil.SLASH, StrUtil.DOT))))
+                .collect(Collectors.toList());
         // 2.获取数据库里记录的文件信息,修改数据并返回文件信息
         FileMd5 fileMd5 = fileMd5Service.queryFileMd5ByFileMd5(requestData.getFileMd5());
         if (null==fileMd5){

+ 1 - 12
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/service/impl/MinioStorageServiceImpl.java

@@ -67,17 +67,6 @@ public class MinioStorageServiceImpl implements IFileStorageService, Initializin
             if (exists(bucketName,fileName)){
                 return;
             }
-            if (chunkBucKet.equals(bucketName)){
-                // 检查一下分片内容大小
-                // 合并文件之前先检查一下文件是否正确
-                byte[] bytes = IoUtil.readBytes(input);
-                if (bytes.length<CHUCK_MIN_FILE_SIZE){
-                    // 进行合并分片操作
-                    mergeMinFile(bucketName,fileName,bytes);
-                    return;
-                }
-                input = IoUtil.toStream(bytes);
-            }
             minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).
                             stream(input, input.available(), -1).build());
         } catch (Exception e) {
@@ -528,7 +517,7 @@ public class MinioStorageServiceImpl implements IFileStorageService, Initializin
         }
         minioClient.composeObject(
                 ComposeObjectArgs.builder()
-                        .bucket(composeBucketName)
+                        .bucket(ensureBucket(composeBucketName))
                         .object(objectName)
                         .sources(sourceObjectList)
                         .build()