Parcourir la source

消安合并代码

dsx il y a 2 ans
Parent
commit
18534aa9a4

+ 1 - 7
pom.xml

@@ -148,13 +148,7 @@
 
     <dependency>
       <groupId>com.fdkankan</groupId>
-      <artifactId>4dkankan-utils-fyun-s3</artifactId>
-      <version>3.0.0-SNAPSHOT</version>
-    </dependency>
-
-    <dependency>
-      <groupId>com.fdkankan</groupId>
-      <artifactId>4dkankan-utils-fyun-oss</artifactId>
+      <artifactId>4dkankan-utils-fyun-local</artifactId>
       <version>3.0.0-SNAPSHOT</version>
     </dependency>
 

+ 13 - 0
src/main/java/com/fdkankan/contro/controller/SceneFileController.java

@@ -13,6 +13,7 @@ import lombok.extern.log4j.Log4j2;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -73,6 +74,18 @@ public class SceneFileController{
         return sceneFileBuildService.rebuildScene(num,force,deleteExtras);
     }
 
+    /**
+     * 单个文件上传
+     * @param file
+     * @param params
+     * @return
+     */
+    @PostMapping("upload")
+    public ResultData upload(@RequestParam(value = "file",required = false) MultipartFile file,
+                             String params) throws Exception {
+        return sceneFileBuildService.uploadFile(file, params);
+    }
+
 
     /**
      * 国际八目相机调用

+ 3 - 0
src/main/java/com/fdkankan/contro/service/ISceneFileBuildService.java

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
 import com.fdkankan.contro.entity.SceneFileBuild;
 import com.fdkankan.contro.vo.ResponseSceneFile;
 import com.fdkankan.web.response.ResultData;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
 
@@ -28,4 +29,6 @@ public interface ISceneFileBuildService extends IService<SceneFileBuild> {
     ResultData rebuildScene(String num,Boolean force,Boolean deleteExtras) throws IOException;
 
     ResultData copyDataAndBuild(String sourceBucet,String dataSource,String sceneVer) throws Exception;
+
+    ResultData uploadFile(MultipartFile file, String params) throws Exception;
 }

+ 161 - 0
src/main/java/com/fdkankan/contro/service/impl/SceneFileBuildServiceImpl.java

@@ -18,6 +18,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fdkankan.common.constant.*;
 import com.fdkankan.common.exception.BusinessException;
 import com.fdkankan.common.util.DateExtUtil;
+import com.fdkankan.common.util.FileMd5Util;
 import com.fdkankan.common.util.FileUtils;
 import com.fdkankan.common.util.SnowflakeIdGenerator;
 import com.fdkankan.contro.constant.RedisConstants;
@@ -53,6 +54,7 @@ import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
 import org.springframework.util.ObjectUtils;
 import org.springframework.web.client.RestTemplate;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
 import java.io.IOException;
@@ -149,6 +151,9 @@ public class SceneFileBuildServiceImpl extends ServiceImpl<ISceneFileBuildMapper
     private ICompanyService companyService;
 
     @Autowired
+    private ISceneFileUploadService sceneFileUploadService;
+
+    @Autowired
     private FYunFileServiceInterface fYunFileService;
 
     @Autowired
@@ -1754,4 +1759,160 @@ public class SceneFileBuildServiceImpl extends ServiceImpl<ISceneFileBuildMapper
         return ResultData.ok();
     }
 
+    @Override
+    public ResultData uploadFile(MultipartFile file, String params) throws Exception {
+
+        log.info("upload-params: "+params);
+        if (StringUtils.isEmpty(params)){
+            throw new BusinessException(ErrorCode.PARAM_REQUIRED);
+        }
+        params = params.replaceAll("%2B", "+");
+        Base64 base64 = new Base64();
+        String cipher = params;
+        // 私钥解密过程
+        byte[] res = RSAEncrypt.decrypt(RSAEncrypt.loadPrivateKeyByStr(RSAEncrypt.loadPrivateKeyByFile()),
+                base64.decode(cipher));
+        String restr = new String(res, "UTF-8");
+        log.info("upload-params解密结果:" + restr);
+        String[] strArr = restr.split(SPLICE);
+        if (strArr.length != 6) {
+            throw new BusinessException(ErrorCode.PARAM_REQUIRED);
+        }
+        String mac = strArr[0];
+        String fileId = strArr[1];
+        String picNum = strArr[2];
+        String md5 = strArr[3];
+        String chunk = strArr[4];
+
+        log.info("mac:{},fileId:{},picNum:{},md5:{},chunk:{}", mac, fileId, picNum, md5, chunk);
+
+        ResultData result = null;
+        if (file.isEmpty()){
+            log.error("文件为空。");
+            throw new BusinessException(ErrorCode.PARAM_REQUIRED.code(),"文件为空。");
+        }
+        if (StringUtils.isEmpty(fileId)){
+            log.error("文件Id为空。");
+            throw new BusinessException(ErrorCode.PARAM_REQUIRED.code(),"文件Id为空。");
+        }
+        if (picNum == null){
+            log.error("照片数目为空。");
+            throw new BusinessException(ErrorCode.PARAM_REQUIRED.code(),"照片数目为空。");
+        }
+        if (StringUtils.isEmpty(mac)){
+            log.error("mac为空。");
+            throw new BusinessException(ErrorCode.PARAM_REQUIRED.code(),"mac为空。");
+        }
+        if (StringUtils.isEmpty(md5)){
+            log.error("md5为空。");
+            throw new BusinessException(ErrorCode.PARAM_REQUIRED.code(),"md5为空。");
+        }
+        long size = file.getSize();
+        log.info("mac:{}, fileId:{}, picNum:{}, md5:{}, chunk:{}", mac, fileId, picNum, md5, chunk);
+        chunk = chunk.split(SPLICE)[0];
+        // 获取文件名
+        String fileName = file.getOriginalFilename();
+        log.info("上传的文件名为:" + fileName);
+        // 获取文件的后缀名
+        String folderName = redisUtil.get(String.format(RedisConstants.FILEID_FOLDER_BUILD, fileId));
+        if(StringUtils.isEmpty(folderName)){
+            SceneFileBuild fileBuild = findByFileId(fileId);
+            if(ObjectUtils.isEmpty(fileBuild)){
+                log.error("数据不正常,fileBuild没找到");
+                throw new BusinessException(ErrorCode.FAILURE_CODE_5012);
+            }
+            folderName = fileBuild.getUnicode();
+            redisUtil.set(String.format(RedisConstants.FILEID_FOLDER_BUILD, fileId), folderName,2 * 24 * 60 * 60);
+        }
+        // 1. 判断该文件是否已经上传过
+        // 2. 如果已经上传过,判断MD5值和文件大小是否相等。如果相等,更新数据记录。如果不相等,删除该文件,重新上传。
+        // 3. 如果未上传过,需要上传。
+        String filePath = mac.concat(File.separator).concat(fileId).concat(File.separator).concat(folderName);
+
+        String yunFilePath = LocalConstants.BASE_PATH.concat(fYunFileConfig.getBucket()).concat(File.separator).concat(ConstantFilePath.OSS_PREFIX)
+                .concat(filePath).concat(File.separator).concat(fileName);
+        log.info("yunFilePath:{}", yunFilePath);
+
+        boolean needUpload = false;
+        File yunFile = new File(yunFilePath);
+        if (yunFile.exists()){
+            String fileMD5 = FileMd5Util.getFileMD5(yunFile);
+            if (md5.equals(fileMD5) && yunFile.length() == size){
+                log.info("文件已存在,MD5和文件大小一致。");
+                SceneFileUpload uploadEntity = sceneFileUploadService.findByFileIdAndChunk(fileId, Integer.valueOf(chunk));
+                if (uploadEntity != null){
+                    uploadEntity.setSize((int) size);
+                    uploadEntity.setMd5(md5);
+                    uploadEntity.setFilePath(yunFilePath);
+                    uploadEntity.setFileSourceName(fileName);
+                    uploadEntity.setUploadStatus(1);
+                    sceneFileUploadService.updateById(uploadEntity);
+                }else{
+                    SceneFileUpload sceneFileUploadEntity = new SceneFileUpload();
+                    sceneFileUploadEntity.setSize((int) size);
+                    sceneFileUploadEntity.setMd5(md5);
+                    sceneFileUploadEntity.setFilePath(yunFilePath.toString());
+                    sceneFileUploadEntity.setFileSourceName(fileName);
+                    sceneFileUploadEntity.setUploadStatus(1);
+                    sceneFileUploadEntity.setFileId(fileId);
+                    sceneFileUploadEntity.setChunk(Integer.valueOf(chunk));
+                    sceneFileUploadService.save(sceneFileUploadEntity);
+                }
+                result = ResultData.ok();
+            }else if (!md5.equals(fileMD5)) {
+                log.error("文件已上传,上传MD5:"+md5+",服务器MD5:"+fileMD5+"。不一致。上传失败");
+                FileUtils.delFile(yunFilePath.toString());
+                needUpload = true;
+            }else if (yunFile.length() != size){
+                log.error("文件已上传,文件大小不一致。上传失败");
+                FileUtils.delFile(yunFilePath);
+                needUpload = true;
+            }
+        }else {
+            log.error("文件不存在,需要重新上传");
+            needUpload = true;
+        }
+
+        // 4. 上传成功后,校验MD5和文件大小是否相等
+        // 5. 如果相等,更新数据记录。如果不相等,返回上传失败结果。
+        try {
+            if (needUpload){
+                fYunFileService.uploadFile(file.getInputStream(),ConstantFilePath.OSS_PREFIX.concat(filePath).concat(File.separator).concat(fileName));
+                File uploadFile = new File(yunFilePath);
+                String fileMD5 = FileMd5Util.getFileMD5(uploadFile);
+
+                SceneFileUpload sceneFileUploadEntity = new SceneFileUpload();
+                sceneFileUploadEntity.setSize((int) size);
+                sceneFileUploadEntity.setMd5(md5);
+                sceneFileUploadEntity.setFilePath(yunFilePath);
+                sceneFileUploadEntity.setFileSourceName(fileName);
+                sceneFileUploadEntity.setFileId(fileId);
+                sceneFileUploadEntity.setChunk(Integer.valueOf(chunk));
+
+                if (md5.equals(fileMD5) && uploadFile.length() == size){
+                    log.info("文件已上传,MD5和文件大小一致。上传成功");
+
+                    sceneFileUploadEntity.setUploadStatus(1);
+                    sceneFileUploadService.save(sceneFileUploadEntity);
+                    result = ResultData.ok();
+                }else if (!md5.equals(fileMD5)) {
+                    log.error("文件已上传,上传MD5:"+md5+",服务器MD5:"+fileMD5+"。不一致。上传失败");
+                    sceneFileUploadEntity.setUploadStatus(-1);
+                    sceneFileUploadService.save(sceneFileUploadEntity);
+                    result = ResultData.error(ErrorCode.SYSTEM_ERROR,"上传失败, 请重新上传。");
+                }else if (uploadFile.length() != size){
+                    log.error("文件已上传,文件大小不一致。上传失败");
+                    sceneFileUploadEntity.setUploadStatus(-1);
+                    sceneFileUploadService.save(sceneFileUploadEntity);
+                    result = ResultData.error(ErrorCode.SYSTEM_ERROR,"上传失败, 请重新上传。");
+                }
+            }
+        }catch (IllegalStateException | IOException e) {
+            log.error("上传失败, 请重新上传,md5:{},chunk:{}", md5, chunk);
+            log.error("上传失败", e);
+            result = ResultData.error(ErrorCode.SYSTEM_ERROR,"上传失败, 请重新上传。");
+        }
+        return result;
+    }
+
 }