Bläddra i källkod

需求:实现旧文件服务的所有接口

lijie 3 år sedan
förälder
incheckning
2930115d47

+ 4 - 0
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/context/OldFileContext.java

@@ -20,6 +20,8 @@ public class OldFileContext {
     public static final String SECRET = "secret";
     /** 文件key */
     public static final String KEY = "key";
+    /** 上传文件id */
+    public static final String UPLOAD_ID = "uploadId";
     /** 项目id */
     public static final String PROJECT_ID = "projectId";
     /** 工单id */
@@ -43,5 +45,7 @@ public class OldFileContext {
     private String projectId;
     /** 是否覆盖,true-覆盖,false-非覆盖 */
     private String workOrderId;
+    /** 上传文件id */
+    private String uploadId;
 
 }

+ 245 - 18
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/controller/CompatibleOldFileController.java

@@ -1,23 +1,36 @@
 package com.persagy.dmp.file.controller;
 
+import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.persagy.dmp.common.utils.ResourceUtil;
-import com.persagy.dmp.file.service.CompatibleOldFileService;
+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.FileMd5;
+import com.persagy.dmp.file.model.FileMd5Creator;
+import com.persagy.dmp.file.service.*;
+import com.persagy.dmp.file.utils.FileStorageHelper;
 import lombok.RequiredArgsConstructor;
+import lombok.SneakyThrows;
 import org.springframework.core.io.Resource;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletRequest;
 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;
 
 /***
@@ -34,7 +47,13 @@ public class CompatibleOldFileController {
     private static final String ERROR_HTML="<html><head>" +
             "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>" +
             "<title>image-service</title></head></html><body>{}</body>";
+    private static final String INCORRECT_PARAMETERS="Incorrect parameters inputted.";
+    private static final String FILE_EXISTED="File existed.";
+    private static final String CAMEL_HTTP_RESPONSE_CODE="CamelHttpResponseCode";
+    private static final Integer MAX_CHUCK_NUMBER=10000;
     private final CompatibleOldFileService compatibleOldFileService;
+    private final IFileService fileService;
+    private final FileMd5Service fileMd5Service;
 
     /***
      * Description: 获取接口文档
@@ -70,7 +89,19 @@ public class CompatibleOldFileController {
     @PostMapping(value = "/work_order/upload",consumes = {"image/jpg"})
     @ResponseBody
     public String workOrderFileUpload(HttpServletRequest request) throws IOException {
-        return compatibleOldFileService.uploadWorkOrderFile(request.getInputStream());
+        if(StrUtil.isBlank(OldFileAppContext.getContext().getKey())
+                || StrUtil.isBlank(OldFileAppContext.getContext().getProjectId())
+                || StrUtil.isBlank(OldFileAppContext.getContext().getWorkOrderId())
+                || OldFileAppContext.getContext().getProjectId().length()<=2){
+            return INCORRECT_PARAMETERS;
+        }
+        // 1.如果非覆盖且文件信息已存在则直接返回文件已存在
+        if (!OldFileAppContext.getContext().getOverwrite() &&
+                null!=fileService.load(OldFileAppContext.getContext().getKey())){
+            return FILE_EXISTED;
+        }
+        compatibleOldFileService.uploadWorkOrderFile(request.getInputStream());
+        return FileStorageHelper.oldSuccessResultCreate("");
     }
     /***
      * Description: 文件上传
@@ -83,17 +114,6 @@ public class CompatibleOldFileController {
     }
 
     /***
-     * Description: 文件上传
-     * @return : void
-     * @author : lijie
-     * Update By lijie 2021/11/19 9:58
-     */
-    @PostMapping(value = "/common/file_upload")
-    @ResponseBody
-    public String fileUpload(HttpServletRequest request) throws IOException {
-        return compatibleOldFileService.uploadFile(request.getInputStream());
-    }
-    /***
      * Description: 图片上传
      * @return : void
      * @author : lijie
@@ -102,7 +122,11 @@ public class CompatibleOldFileController {
     @PostMapping(value = "/common/image_upload",consumes = {"image/jpg"})
     @ResponseBody
     public String imageUpload(HttpServletRequest request) throws IOException {
-        return compatibleOldFileService.uploadFile(request.getInputStream());
+        if(StrUtil.isBlank(OldFileAppContext.getContext().getKey())){
+            return INCORRECT_PARAMETERS;
+        }
+        compatibleOldFileService.uploadFile(request.getInputStream());
+        return FileStorageHelper.oldSuccessResultCreate("");
     }
     /***
      * Description: 图片下载
@@ -130,7 +154,7 @@ public class CompatibleOldFileController {
      * @author : lijie
      * Update By lijie 2021/11/19 9:58
      */
-    @PostMapping(value = "/common/images_list")
+    @PostMapping(value = {"/common/images_list","/common/files_list"})
     @ResponseBody
     public JSONObject imagesList(@RequestBody(required = false) JSONObject param) {
         List<String> fileIds = compatibleOldFileService.listFiles(param);
@@ -142,14 +166,14 @@ public class CompatibleOldFileController {
         return result;
     }
     /***
-     * Description: 图片删除
+     * Description: 图片/文件删除
      * @return : void
      * @author : lijie
      * Update By lijie 2021/11/19 9:58
      */
-    @PostMapping(value = "/common/images_delete")
+    @PostMapping(value = {"/common/images_delete","/common/files_delete"})
     @ResponseBody
-    public JSONObject imageDelete(@RequestBody(required = false) JSONObject param){
+    public JSONObject filesDelete(@RequestBody(required = false) JSONObject param){
         JSONObject result = new JSONObject();
         result.put("result", "success");
         result.put("Count", 0L);
@@ -165,7 +189,210 @@ public class CompatibleOldFileController {
         result.put("Content",fileIds);
         return result;
     }
+    /***
+     * Description: 更改图片/文件key
+     * @author : lijie
+     * Update By lijie 2021/11/19 9:58
+     */
+    @PostMapping(value = {"/common/image_key_change","/common/files_key_change"})
+    @ResponseBody
+    public JSONObject keyChange(@RequestBody(required = false) JSONObject param){
+        JSONObject result = new JSONObject();
+        result.put("result", "success");
+        result.put("ResultMsg", "");
+        if (StrUtil.isBlank(OldFileAppContext.getContext().getSystemId())
+            || StrUtil.isBlank(param.getString("from"))
+            || StrUtil.isBlank(param.getString("to"))){
+            result.put("result", "failed");
+            result.put("ResultMsg","Incorrect parameters inputted.");
+            return result;
+        }
+        compatibleOldFileService.fileKeyChange(param.getString("from"),param.getString("to"));
+        return result;
+    }
 
+    /***
+     * Description: 文件上传
+     * @return : void
+     * @author : lijie
+     * Update By lijie 2021/11/19 9:58
+     */
+    @PostMapping(value = "/common/file_upload")
+    @ResponseBody
+    public String fileUpload(HttpServletRequest request) throws IOException {
+        if(StrUtil.isBlank(OldFileAppContext.getContext().getKey())){
+            return INCORRECT_PARAMETERS;
+        }
+        compatibleOldFileService.uploadFile(request.getInputStream());
+        return FileStorageHelper.oldSuccessResultCreate("");
+    }
+
+    /***
+     * Description: 文件下载
+     * @author : lijie
+     * Update By lijie 2021/11/19 9:58
+     */
+    @GetMapping(value = "/common/file_get/{key}")
+    public void fileGetByKey(@PathVariable(value = "key",required = false) String key,HttpServletResponse response) {
+        if (StrUtil.isNotBlank(key)){
+            OldFileAppContext.getContext().setKey(key);
+        }
+        compatibleOldFileService.getFile(response);
+        response.setContentType("application/octet-stream");
+    }
+    /***
+     * Description: 文件下载
+     * @author : lijie
+     * Update By lijie 2021/11/19 9:58
+     */
+    @GetMapping(value = "/common/file_get")
+    public void fileGet(HttpServletResponse response) {
+        compatibleOldFileService.getFile(response);
+        response.setContentType("application/octet-stream");
+    }
+    /***
+     * Description: 断点续传的注册
+     * @author : lijie
+     * Update By lijie 2021/11/19 9:58
+     */
+    @PostMapping(value = "/common/register_multipart_upload")
+    @ResponseBody
+    public String registerMultipartUpload() {
+        if (StrUtil.isBlank(OldFileAppContext.getContext().getSystemId())
+            || StrUtil.isBlank(OldFileAppContext.getContext().getKey())){
+            return INCORRECT_PARAMETERS;
+        }
+        // 1.如果非覆盖且文件信息已存在则直接返回文件已存在
+        if (!OldFileAppContext.getContext().getOverwrite() &&
+                null!=fileService.load(OldFileAppContext.getContext().getKey())){
+            return FILE_EXISTED;
+        }
+        String uploadId = compatibleOldFileService.registerMultipartUpload();
+        JSONObject jsonObj = new JSONObject();
+        if (StrUtil.isBlank(uploadId)){
+            jsonObj.put("Result", "failure");
+            jsonObj.put("ResultMsg", "获取UploadId失败");
+            return jsonObj.toJSONString();
+        }
+        jsonObj.put("Result", "success");
+        jsonObj.put("ResultMsg", "");
+        jsonObj.put("UploadId", uploadId);
+        return jsonObj.toJSONString();
+    }
+    /***
+     * Description: 断点续传的注册
+     * @author : lijie
+     * Update By lijie 2021/11/19 9:58
+     */
+    @SneakyThrows
+    @RequestMapping(value = "/common/multipart_upload",consumes = {MediaType.MULTIPART_FORM_DATA})
+    @ResponseBody
+    public String multipartUpload(@RequestParam(value = "file",required = false) MultipartFile file,
+                                  @RequestParam(value = "uploadId",required = false) String uploadId,
+                                  @RequestParam(value = "chunkNumber",required = false) Integer chunkNumber,
+                                  @RequestParam(value = "chunkSize",required = false) Integer chunkSize,
+                                  @RequestParam(value = "totalSize",required = false) Long totalSize,
+                                  @RequestParam(value = "filename",required = false) String fileName,
+                                  @RequestParam(value = "totalChunks",required = false) Integer totalChunks,
+                                  @RequestParam(value = "md5",required = false) String md5,
+                                  HttpServletResponse response) {
+        if(null==file || null==uploadId || null==chunkNumber || null==chunkSize || null == md5 || null==totalSize){
+            response.setIntHeader(CAMEL_HTTP_RESPONSE_CODE, 500);
+            return INCORRECT_PARAMETERS;
+        }
+        if(chunkNumber < 1 || chunkNumber > MAX_CHUCK_NUMBER){
+            response.setIntHeader(CAMEL_HTTP_RESPONSE_CODE, 500);
+            return "Invalid chunkSize.";
+        }
+        FileInfo fileInfo = getFileInfoByUploadId(uploadId);
+        if (null==fileInfo){
+            response.setIntHeader(CAMEL_HTTP_RESPONSE_CODE, 500);
+            return "UploadId has expired.";
+        }
+        JSONObject jsonObj = new JSONObject();
+        jsonObj.put("Result", "success");
+        jsonObj.put("ResultMsg", "");
+        jsonObj.put("TotalCount", 0);
+        // 1.根据文件md5判断是否已经存在
+        FileMd5 fileMd5 = fileMd5Service.queryFileMd5ByFileMd5(md5);
+        if (ObjectUtil.isNotEmpty(fileMd5) && FileCommonConst.UPLOAD_SUCCESS.equals(fileMd5.getUploadStatus())){
+            // MD5文件已存在
+            jsonObj.put("TotalCount", totalChunks);
+            return jsonObj.toJSONString();
+        }
+        // 1.先上传分片信息
+        IFileStorageService service = FileStorageFactory.getService();
+        service.upload(service.getChuckBucketName(null),md5 + "/" + chunkNumber + ".chunk",file.getInputStream());
+        // 2.更新fileInfo
+        if (StrUtil.isBlank(fileInfo.getFileMd5())){
+            fileInfo.setFileMd5(md5);
+            if (StrUtil.isNotBlank(fileName)){
+                fileInfo.setFileName(fileName);
+            }
+            fileService.updateById(fileInfo);
+        }
+        // 3.如果MD5信息没有的话则创建一个
+        if (ObjectUtil.isEmpty(fileMd5)){
+            FileMd5 fileMd5Create = FileMd5Creator.of(fileInfo.getCreator(), md5, FileCommonConst.UPLOAD_PART);
+            fileMd5Create.setFileSize(totalSize);
+            fileMd5Create.setCreator(fileInfo.getCreator());
+            fileMd5Service.save(fileMd5);
+        }
+        Map<Integer,String> okChunkMap = service.mapChunkObjectNames(service.getChuckBucketName(null), md5);
+        jsonObj.put("TotalCount", okChunkMap.size());
+        return jsonObj.toJSONString();
+    }
+    /***
+     * Description: 断点续传的合并
+     * @author : lijie
+     * Update By lijie 2021/11/19 9:58
+     */
+    @SneakyThrows
+    @RequestMapping(value = "/common/merge_multipart")
+    @ResponseBody
+    public String multipartUpload() {
+        if (StrUtil.isBlank(OldFileAppContext.getContext().getUploadId())){
+            return INCORRECT_PARAMETERS;
+        }
+        FileInfo fileInfo = getFileInfoByUploadId(OldFileAppContext.getContext().getUploadId());
+        if (null==fileInfo
+                || StrUtil.isBlank(fileInfo.getCreator())
+                || StrUtil.isBlank(fileInfo.getFileMd5())){
+            return INCORRECT_PARAMETERS;
+        }
+        FileMd5 fileMd5 = fileMd5Service.queryFileMd5ByFileMd5(fileInfo.getFileMd5());
+        if (ObjectUtil.isEmpty(fileMd5)){
+            return INCORRECT_PARAMETERS;
+        }
+        IFileStorageService service = FileStorageFactory.getService();
+        List<String> chunks = service
+                .listObjectNames(service.getChuckBucketName(null),fileInfo.getFileMd5());
+        if (CollUtil.isEmpty(chunks)){
+            return INCORRECT_PARAMETERS;
+        }
+        fileMd5Service.ensureFilePath(fileMd5,fileInfo.getFileName());
+        service.composeObject(fileInfo.getCreator(),chunks,fileMd5.getFilePath());
+        // 更新Md5值
+        fileMd5.setUploadStatus(FileCommonConst.UPLOAD_SUCCESS);
+        fileMd5Service.updateById(fileMd5);
+        JSONObject jsonObj = new JSONObject();
+        jsonObj.put("Result", "success");
+        jsonObj.put("ResultMsg", "");
+        return jsonObj.toJSONString();
+    }
+    /***
+     * Description: 根据上传id查询文件信息
+     * @param uploadId : 上传id
+     * @return : com.persagy.dmp.file.model.FileInfo
+     * @author : lijie
+     * Update By 2021/12/9 20:17
+     */
+    private FileInfo getFileInfoByUploadId(String uploadId) {
+        LambdaQueryWrapper<FileInfo> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(FileInfo::getBusinessId,uploadId);
+        queryWrapper.eq(FileInfo::getValid,Boolean.TRUE);
+        return fileService.getOne(queryWrapper,false);
+    }
 
 
 }

+ 41 - 1
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/controller/TestController.java

@@ -1,5 +1,7 @@
 package com.persagy.dmp.file.controller;
 
+import cn.hutool.crypto.digest.DigestUtil;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.persagy.dmp.common.context.AppContext;
 import com.persagy.dmp.common.lang.PsDateTime;
@@ -10,11 +12,15 @@ import com.persagy.dmp.file.model.FileInfo;
 import com.persagy.dmp.file.model.FileInfoCreator;
 import com.persagy.dmp.file.model.FileRequestData;
 import com.persagy.dmp.file.model.UploadMes;
-import io.minio.MinioClient;
+import com.persagy.dmp.file.utils.FileStorageHelper;
+import io.minio.*;
+import lombok.SneakyThrows;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 
 /**
@@ -89,4 +95,38 @@ public class TestController {
         return "success";
     }
 
+    @SneakyThrows
+    public static void main(String[] args) {
+        //byte[] streamBytes = FileStorageHelper.getStreamBytes(new FileInputStream(new File("D:\\test\\temp\\20211209_Eq440305000274029fde4b7f459a872d5e15646c285c.png")));
+        byte[] streamBytes = FileStorageHelper.getStreamBytes(new FileInputStream(new File("D:\\test\\temp\\上海国际航空服务中心龙华二期_塔楼_F18_20211206.rvt")));
+        System.out.println(DigestUtil.md5Hex(streamBytes));
+        // 4ee40f88485bcbe9282592d6d97ce6ff
+        //EFDDF79F60624752814D8F2A36CD3B9D
+//        MinioClient minioClient=MinioClient.builder()
+//                .endpoint("http://192.168.100.102",31335,false)
+//                .credentials("persagy", "persagy@2021")
+//                .region("GM+8")
+//                .build();
+        // Get information of an object.
+//        StatObjectResponse statObject = minioClient
+//                .statObject(StatObjectArgs.builder()
+//                        .bucket("dmp-rwd")
+//                        .object("20211209/Eq440305000274029fde4b7f459a872d5e15646c285c.png")
+//                        .build());
+//        StatObjectResponse statObject = minioClient
+//                .statObject(StatObjectArgs.builder()
+//                        .bucket("dmp-rwd")
+//                        .object("20211209/Eq440305000274029fde4b7f459a872d5e15646c285c.png")
+//                        .region("GM+8")
+//                        .build());
+//        GetObjectResponse getObjectResponse = minioClient.getObject(GetObjectArgs.builder()
+//                .bucket("dmp-rwd")
+//                .object("20211209/Eq440305000274029fde4b7f459a872d5e15646c285c.png")
+//                .region("GM+8")
+//                .build());
+//        System.out.println(getObjectResponse.read());
+        // System.out.println(JSONObject.toJSONString(statObject));
+
+    }
+
 }

+ 18 - 4
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/service/CompatibleOldFileService.java

@@ -18,11 +18,10 @@ public interface CompatibleOldFileService {
     /***
      * Description: 上传文件
      * @param inputStream : 文件流
-     * @return : void
      * @author : lijie
      * Update By lijie 2021/12/8 14:29
      */
-    String uploadFile(InputStream inputStream);
+    void uploadFile(InputStream inputStream);
     /***
      * Description: 下载文件
      * @param response : 响应参数
@@ -41,11 +40,10 @@ public interface CompatibleOldFileService {
     /***
      * Description: 上传工单的文件
      * @param inputStream : 文件流
-     * @return : java.lang.String
      * @author : lijie
      * Update By lijie 2021/12/9 10:07
      */
-    String uploadWorkOrderFile(InputStream inputStream);
+    void uploadWorkOrderFile(InputStream inputStream);
     /***
      * Description: 下载工单文件
      * @param response : 响应体
@@ -61,4 +59,20 @@ public interface CompatibleOldFileService {
      * Update By lijie 2021/12/9 11:31
      */
     Set<String> deleteFileByIds(List<String> deleteKeys);
+    /***
+     * Description: 更改文件key
+     * @param from : 旧文件key
+     * @param to : 新文件key
+     * @return : java.lang.Boolean
+     * @author : lijie
+     * Update By 2021/12/9 17:05
+     */
+    Boolean fileKeyChange(String from, String to);
+    /***
+     * Description: 断点续传之注册上传id
+     * @return : java.lang.String
+     * @author : lijie
+     * Update By 2021/12/9 19:39
+     */
+    String registerMultipartUpload();
 }

+ 3 - 1
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/service/IFileService.java

@@ -1,7 +1,9 @@
 package com.persagy.dmp.file.service;
 
+import com.baomidou.mybatisplus.extension.service.IService;
 import com.persagy.dmp.common.lang.PsDate;
 import com.persagy.dmp.file.model.FileInfo;
+import com.persagy.dmp.file.model.FileMd5;
 import com.persagy.dmp.file.model.FileRequestData;
 import com.persagy.dmp.file.model.UploadMes;
 
@@ -12,7 +14,7 @@ import java.util.Set;
  * 文件管理接口
  * @author Charlie Yu
  */
-public interface IFileService {
+public interface IFileService extends IService<FileInfo> {
 
     /**
      * 保存

+ 51 - 28
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/service/impl/CompatibleOldFileServiceImpl.java

@@ -1,19 +1,19 @@
 package com.persagy.dmp.file.service.impl;
 
+import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.io.IoUtil;
-import cn.hutool.core.util.NumberUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.crypto.digest.DigestUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.persagy.dmp.common.constant.CommonConstant;
+import com.persagy.dmp.common.exception.BusinessException;
 import com.persagy.dmp.common.helper.SpringHelper;
 import com.persagy.dmp.common.lang.PsDateTime;
-import com.persagy.dmp.file.client.FileClientFacade;
-import com.persagy.dmp.file.client.FileMd5ClientFacade;
 import com.persagy.dmp.file.constant.FileCommonConst;
 import com.persagy.dmp.file.context.OldFileAppContext;
 import com.persagy.dmp.file.dao.FileMapper;
@@ -23,9 +23,8 @@ import com.persagy.dmp.file.utils.FileStorageHelper;
 import lombok.RequiredArgsConstructor;
 import lombok.SneakyThrows;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
-import javax.servlet.ServletInputStream;
-import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.InputStream;
 import java.util.*;
@@ -56,11 +55,8 @@ public class CompatibleOldFileServiceImpl implements CompatibleOldFileService {
      * Update By lijie 2021/12/8 15:20
      */
     @Override
-    public String uploadFile(InputStream inputStream) {
-        if(StrUtil.isBlank(OldFileAppContext.getContext().getKey())){
-            return "Incorrect parameters inputted.";
-        }
-        return commonUploadFile(inputStream,OldFileAppContext.getContext().getSystemId(),null);
+    public void uploadFile(InputStream inputStream) {
+        commonUploadFile(inputStream,OldFileAppContext.getContext().getSystemId(),null);
     }
 
     /***
@@ -126,14 +122,8 @@ public class CompatibleOldFileServiceImpl implements CompatibleOldFileService {
      * Update By lijie 2021/12/9 10:07
      */
     @Override
-    public String uploadWorkOrderFile(InputStream inputStream) {
-        if(StrUtil.isBlank(OldFileAppContext.getContext().getKey())
-            || StrUtil.isBlank(OldFileAppContext.getContext().getProjectId())
-            || StrUtil.isBlank(OldFileAppContext.getContext().getWorkOrderId())
-            || OldFileAppContext.getContext().getProjectId().length()<=2){
-            return "Incorrect parameters inputted.";
-        }
-        return commonUploadFile(inputStream,OldFileAppContext.getContext().getProjectId().substring(2),
+    public void uploadWorkOrderFile(InputStream inputStream) {
+        commonUploadFile(inputStream,OldFileAppContext.getContext().getProjectId().substring(2),
                 OldFileAppContext.getContext().getWorkOrderId());
     }
     /***
@@ -231,6 +221,45 @@ public class CompatibleOldFileServiceImpl implements CompatibleOldFileService {
     }
 
     /***
+     * Description: 更改文件key
+     * @param from : 旧文件key
+     * @param to : 新文件key
+     * @return : java.lang.Boolean
+     * @author : lijie
+     * Update By 2021/12/9 17:05
+     */
+    @Override
+    @Transactional(rollbackFor = BusinessException.class)
+    public Boolean fileKeyChange(String from, String to) {
+        FileInfo oldFileInfo = fileMapper.getFileInfoById(from);
+        if (null==oldFileInfo){
+            return true;
+        }
+        FileInfo newFileInfo = BeanUtil.copyProperties(oldFileInfo, FileInfo.class);
+        oldFileInfo.setValid(0);
+        newFileInfo.setId(to);
+        newFileInfo.setFileName(to);
+        fileService.updateById(oldFileInfo);
+        return fileService.saveOrUpdate(newFileInfo);
+    }
+    /***
+     * Description: 断点续传之注册上传id
+     * @return : java.lang.String
+     * @author : lijie
+     * Update By 2021/12/9 19:39
+     */
+    @Override
+    public String registerMultipartUpload() {
+        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.getFileMd5();
+    }
+
+    /***
      * Description: 通用下载文件逻辑
      * @param response : 响应体
      * @author : lijie
@@ -260,12 +289,7 @@ public class CompatibleOldFileServiceImpl implements CompatibleOldFileService {
      * @author : lijie
      * Update By lijie 2021/12/9 10:17
      */
-    private String commonUploadFile(InputStream inputStream,String bucketName,String businessId) {
-        // 1.如果非覆盖且文件信息已存在则直接返回文件已存在
-        if (!OldFileAppContext.getContext().getOverwrite() &&
-                null!=fileService.load(OldFileAppContext.getContext().getKey())){
-            return "File existed.";
-        }
+    private void commonUploadFile(InputStream inputStream,String bucketName,String businessId) {
         IFileStorageService service = FileStorageFactory.getService();
         // 1.获得文件大小
         byte[] streamBytes = FileStorageHelper.getStreamBytes(inputStream);
@@ -288,7 +312,7 @@ public class CompatibleOldFileServiceImpl implements CompatibleOldFileService {
             // 2.4 创建文件信息
             fileService.secondTransmission(OldFileAppContext.getContext().getKey(), CommonConstant.DEFAULT_ID,
                     businessId, null, fileMd5.getFileMd5(), OldFileAppContext.getContext().getKey());
-            return FileStorageHelper.oldSuccessResultCreate("");
+            return;
         }
         // 3.历史已上传且已完成上传
         if (ObjectUtil.isNotNull(fileMd5)
@@ -297,7 +321,7 @@ public class CompatibleOldFileServiceImpl implements CompatibleOldFileService {
             // 秒传
             fileService.secondTransmission(OldFileAppContext.getContext().getKey(),CommonConstant.DEFAULT_ID,
                     businessId, null,fileMd5.getFileMd5(),OldFileAppContext.getContext().getKey());
-            return FileStorageHelper.oldSuccessResultCreate("");
+            return;
         }
         // 4.历史已上传且未完成上传
         if (ObjectUtil.isNotNull(fileMd5)
@@ -307,7 +331,7 @@ public class CompatibleOldFileServiceImpl implements CompatibleOldFileService {
             // 4.2 秒传
             fileService.secondTransmission(OldFileAppContext.getContext().getKey(),CommonConstant.DEFAULT_ID,
                     businessId, null,fileMd5.getFileMd5(),OldFileAppContext.getContext().getKey());
-            return FileStorageHelper.oldSuccessResultCreate("");
+            return;
         }
         // 5.新上传文件
         fileMd5 = FileMd5Creator.of(bucketName, fileMd5Str, FileCommonConst.UPLOAD_SUCCESS);
@@ -321,6 +345,5 @@ public class CompatibleOldFileServiceImpl implements CompatibleOldFileService {
         // 5.3 保存文件信息
         fileService.secondTransmission(OldFileAppContext.getContext().getKey(),CommonConstant.DEFAULT_ID,
                 businessId, null,fileMd5.getFileMd5(),OldFileAppContext.getContext().getKey());
-        return FileStorageHelper.oldSuccessResultCreate("");
     }
 }

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

@@ -76,7 +76,7 @@ public class FileMd5ServiceImpl extends ServiceImpl<FileMd5Mapper, FileMd5> impl
         LambdaQueryWrapper<FileMd5> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(FileMd5::getFileMd5,fileMd5);
         queryWrapper.eq(FileMd5::getValid,true);
-        return this.getOne(queryWrapper);
+        return this.getOne(queryWrapper,false);
     }
     /***
      * Description: 更新文件MD5信息

+ 6 - 3
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/service/impl/FileServiceImpl.java

@@ -139,7 +139,7 @@ public class FileServiceImpl extends ServiceImpl<FileMapper, FileInfo> implement
             }
         }
         // 4.第一次上传或已存在但没有上传过分片的
-        return firstInitChunkUploadInternal(requestData);
+        return firstInitChunkUploadInternal(requestData,ObjectUtil.isNotNull(fileMd5)?fileMd5.getId():null);
     }
 
     /***
@@ -312,7 +312,7 @@ public class FileServiceImpl extends ServiceImpl<FileMapper, FileInfo> implement
      * @author : lijie
      * Update By lijie 2021/10/9 20:36
      */
-    private UploadMes firstInitChunkUploadInternal(FileRequestData requestData) {
+    private UploadMes firstInitChunkUploadInternal(FileRequestData requestData,String fileMd5Id) {
         IFileStorageService service = FileStorageFactory.getService();
         List<String> uploadUrls = service
                 .createUploadChunkUrlList(service.getChuckBucketName(null),
@@ -327,7 +327,10 @@ public class FileServiceImpl extends ServiceImpl<FileMapper, FileInfo> implement
         }
         //向数据库中记录该文件的Md5上传信息
         FileMd5 fileMd5 = FileMd5Creator.of(requestData.getFileBucket(),requestData.getFileMd5(),FileCommonConst.UPLOAD_PART);
-        fileMd5Service.save(fileMd5);
+        if (StrUtil.isNotBlank(fileMd5Id)){
+            fileMd5.setId(fileMd5Id);
+        }
+        fileMd5Service.saveOrUpdate(fileMd5);
         UploadMes uploadMes = new UploadMes();
         uploadMes.setUploadCode(UploadCodeEnum.CODE_203.getCode());
         uploadMes.setContent(chunkUploadUrls);

+ 4 - 3
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/service/impl/MinioStorageServiceImpl.java

@@ -60,9 +60,9 @@ public class MinioStorageServiceImpl implements IFileStorageService, Initializin
         try {
             // 确认桶是否存在
             bucketName = ensureBucket(bucketName);
-            // 上传文件
-//            PutObjectOptions options = new PutObjectOptions(input.available(), -1);
-//            minioClient.putObject(bucketName, fileName, input, options);
+            if (exists(bucketName,fileName)){
+                return;
+            }
             minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).
                             stream(input, input.available(), -1).build());
         } catch (Exception e) {
@@ -498,6 +498,7 @@ public class MinioStorageServiceImpl implements IFileStorageService, Initializin
     public Long getObjectSize(String bucketName, String objectName){
         StatObjectResponse statObject = minioClient
                 .statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
+
         return statObject.size();
     }
     /***