lyhzzz 2 anni fa
parent
commit
f190c64512

+ 17 - 3
src/main/java/com/fdkankan/fusion/controller/CaseFilesController.java

@@ -7,9 +7,11 @@ import com.fdkankan.fusion.exception.BusinessException;
 import com.fdkankan.fusion.common.ResultData;
 import com.fdkankan.fusion.entity.CaseFiles;
 import com.fdkankan.fusion.service.ICaseFilesService;
+import com.fdkankan.fusion.service.impl.UploadService;
 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;
 
 /**
  * <p>
@@ -25,6 +27,8 @@ public class CaseFilesController {
 
     @Autowired
     ICaseFilesService caseFilesService;
+    @Autowired
+    UploadService uploadService;
 
     @GetMapping("/allList")
     public ResultData allList(@RequestParam(required = false) Integer caseId,
@@ -33,12 +37,22 @@ public class CaseFilesController {
     }
 
     @PostMapping("/add")
-    public ResultData add(@RequestBody CaseFiles caseFiles ){
-        if(caseFiles.getCaseId() == null || StringUtils.isEmpty(caseFiles.getFilesTitle())
-                || caseFiles.getFilesTypeId()== null || StringUtils.isEmpty(caseFiles.getFilesUrl())){
+    public ResultData add(@RequestParam(required = false) MultipartFile file,
+                          @RequestParam(required = false) Integer caseId ,
+                          @RequestParam(required = false) Integer filesTypeId ,
+                          @RequestParam(required = false) String filesTitle ){
+        if(caseId == null || StringUtils.isEmpty(filesTitle)
+                || filesTypeId== null ){
             throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
         }
+        CaseFiles caseFiles = new CaseFiles();
+        caseFiles.setCaseId(caseId);
+        caseFiles.setFilesTypeId(filesTypeId);
+        caseFiles.setFilesTitle(filesTitle);
         caseFilesService.save(caseFiles);
+        String url = uploadService.uploadFile(file, false, "fusion/file/" + caseFiles.getFilesId() + "/");
+        caseFiles.setFilesUrl(url);
+        caseFilesService.saveOrUpdate(caseFiles);
         return ResultData.ok();
     }
     @PostMapping("/delete")

+ 1 - 1
src/main/java/com/fdkankan/fusion/controller/HotIconController.java

@@ -52,7 +52,7 @@ public class HotIconController extends BaseController{
             throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
         }
 
-        String iconUrl = uploadService.uploadFile(file, true, "fusion/icon/",null);
+        String iconUrl = uploadService.uploadFile(file, true, "fusion/icon/");
 
         String username = JwtUtil.getUsername(getToken());
         HotIcon hotIcon = new HotIcon();

+ 1 - 1
src/main/java/com/fdkankan/fusion/controller/UploadController.java

@@ -29,6 +29,6 @@ public class UploadController {
     @PostMapping("/file")
     public ResultData file(@RequestParam(required = false) MultipartFile file) throws Exception {
 
-        return ResultData.ok( uploadService.uploadFile(file,false,"fusion/file/",null));
+        return ResultData.ok( uploadService.uploadFile(file,true,"fusion/file/"));
     }
 }

+ 41 - 1
src/main/java/com/fdkankan/fusion/service/impl/UploadService.java

@@ -48,7 +48,7 @@ public class UploadService {
             }else {
                 fileName= fileName.substring(0,fileName.lastIndexOf("."));
             }
-            localFile = File.createTempFile(fileName,suffixName);
+            localFile = File.createTempFile(fileName + suffixName,suffixName);
             file.transferTo(localFile);
             String path = localFile.getPath();
             if(filePathSet !=null){
@@ -75,6 +75,46 @@ public class UploadService {
         }
 
     }
+    public String uploadFile(MultipartFile file, boolean newName, String filePathAdd) {
+        if(file.isEmpty()){
+            throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
+        }
+        if(file.getSize()>10 * 1024 * 1024 * 100){
+            System.out.println(file.getSize());
+            throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG);
+        }
+        //获取文件名
+        String fileName = file.getOriginalFilename();
+        if(StringUtils.isEmpty(fileName)){
+            throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
+        }
+        File localFile = null;
+        try {
+            //获取文件后缀名
+            String suffixName = fileName.substring(fileName.lastIndexOf("."));
+            //重新生成文件名
+            if(newName){
+                fileName = UUID.randomUUID().toString().replace("-","") ;
+            }else {
+                fileName= fileName.substring(0,fileName.lastIndexOf("."));
+            }
+            localFile = File.createTempFile(fileName + suffixName,suffixName);
+            file.transferTo(localFile);
+            String path = localFile.getPath();
+            uploadToOssUtil.uploadOss(path,filePathAdd+ fileName + suffixName);
+            if(!uploadToOssUtil.existKey(filePathAdd + fileName + suffixName)){
+                throw new BusinessException(ResultCode.UPLOAD_ERROR.code,ResultCode.UPLOAD_ERROR.msg);
+            }
+            return queryPath +filePathAdd+ fileName + suffixName;
+        }catch (Exception e){
+            throw new BusinessException(ResultCode.UPLOAD_ERROR.code,ResultCode.UPLOAD_ERROR.msg);
+        }finally {
+            if(localFile!=null){
+                localFile.delete();
+            }
+        }
+
+    }
 
     public void deleteOssUrl(String path) {
         try {