xiewj 1 jaar geleden
bovenliggende
commit
c0864de4a9

+ 6 - 5
src/main/java/com/fdkankan/site/controller/UploadController.java

@@ -4,6 +4,7 @@ import cn.dev33.satoken.annotation.SaCheckLogin;
 import cn.dev33.satoken.annotation.SaIgnore;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.fdkankan.filestorage.FileStorageTemplate;
 import com.fdkankan.site.common.BaseController;
 import com.fdkankan.site.common.FilePath;
 import com.fdkankan.site.common.ResultCode;
@@ -46,8 +47,6 @@ public class UploadController extends BaseController {
 
     @Resource
     private UploadToOssUtil uploadToOssUtil;
-    @Value("${upload.query-path}")
-    private String queryPath;
     @Autowired
     private IProjectBimService projectBimService;
     @Autowired
@@ -56,6 +55,8 @@ public class UploadController extends BaseController {
     private IProjectService projectService;
     @Resource
     BimClient bimClient;
+    @Resource
+    FileStorageTemplate fileStorageTemplate;
 
 
     @PostMapping("/file")
@@ -63,7 +64,7 @@ public class UploadController extends BaseController {
         File localFile = uploadService.uploadFile(file);
         uploadToOssUtil.uploadOss(localFile.getPath(), FilePath.OSS_FILE_PATH + localFile.getName());
         localFile.delete();
-        return ResultData.ok(queryPath + FilePath.OSS_FILE_PATH + localFile.getName() );
+        return ResultData.ok(fileStorageTemplate.calculateUrl(FilePath.OSS_FILE_PATH + localFile.getName())  );
     }
 
     /**
@@ -77,7 +78,7 @@ public class UploadController extends BaseController {
             File localFile = uploadService.uploadFile(file);
             String ossKey = String.format(FilePath.OSS_MEDIA_FILE_PATH,projectId,type,localFile.getName());
             uploadToOssUtil.uploadOss(localFile.getPath(), ossKey);
-            return ResultData.ok(queryPath+ossKey );
+            return ResultData.ok(fileStorageTemplate.calculateUrl(ossKey) );
         }
         return ResultData.error(ResultCode.UPLOAD_FILE_NO_EXIST);
     }
@@ -121,7 +122,7 @@ public class UploadController extends BaseController {
                 throw new BusinessException(ResultCode.UPLOAD_BIM_ERROR);
             }
 
-            projectBim.setBimLocalFilePath(queryPath + ossKey);
+            projectBim.setBimLocalFilePath(fileStorageTemplate.calculateUrl(ossKey));
             projectBim.setFileName(file.getOriginalFilename());
             projectBimService.updateById(projectBim);
 

+ 20 - 18
src/main/java/com/fdkankan/site/service/impl/UploadService.java

@@ -1,5 +1,6 @@
 package com.fdkankan.site.service.impl;
 
+import com.fdkankan.filestorage.FileStorageTemplate;
 import com.fdkankan.site.common.FilePath;
 import com.fdkankan.site.common.ResultCode;
 import com.fdkankan.site.common.util.UploadToOssUtil;
@@ -18,56 +19,57 @@ public class UploadService {
 
     @Resource
     private UploadToOssUtil uploadToOssUtil;
+    @Resource
+    FileStorageTemplate fileStorageTemplate;
 
-    @Value("${upload.query-path}")
-    private String queryPath;
 
     public File uploadFile(MultipartFile file) {
-        if(file.isEmpty()){
+        if (file.isEmpty()) {
             throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
         }
-        if(file.getSize()>10 * 1024 * 1024 * 100){
+        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)){
+        if (StringUtils.isEmpty(fileName)) {
             throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
         }
         File localFile = null;
         try {
             String suffixName = fileName.substring(fileName.lastIndexOf("."));
-            fileName = UUID.randomUUID().toString().replace("-","");
-            localFile = File.createTempFile(fileName,suffixName);
+            fileName = UUID.randomUUID().toString().replace("-", "");
+            localFile = File.createTempFile(fileName, suffixName);
             file.transferTo(localFile);
             return localFile;
-        }catch (Exception e){
-           e.printStackTrace();
+        } catch (Exception e) {
+            e.printStackTrace();
         }
         return null;
     }
-    public File uploadFile(MultipartFile file,String sceneCode,String type) {
-        if(file.isEmpty()){
+
+    public File uploadFile(MultipartFile file, String sceneCode, String type) {
+        if (file.isEmpty()) {
             throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
         }
-        if(file.getSize()>10 * 1024 * 1024 * 100){
+        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)){
+        if (StringUtils.isEmpty(fileName)) {
             throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
         }
         File localFile = null;
         try {
             String suffixName = fileName.substring(fileName.lastIndexOf("."));
-            fileName = UUID.randomUUID().toString().replace("-","");
-            localFile = File.createTempFile(fileName,suffixName);
+            fileName = UUID.randomUUID().toString().replace("-", "");
+            localFile = File.createTempFile(fileName, suffixName);
             file.transferTo(localFile);
             return localFile;
-        }catch (Exception e){
+        } catch (Exception e) {
             e.printStackTrace();
         }
         return null;
@@ -75,9 +77,9 @@ public class UploadService {
 
     public void deleteOssUrl(String path) {
         try {
-            String replace = path.replace(queryPath, "");
+            String replace = path.replace(fileStorageTemplate.calculateUrl(""), "");
             uploadToOssUtil.delete(replace);
-        }catch (Exception e){
+        } catch (Exception e) {
             e.printStackTrace();
         }
     }