Browse Source

1、优化文件上传接口

rayson 8 months ago
parent
commit
d95a376ac7

+ 1 - 1
menduner/menduner-system-api/src/main/java/com/citu/module/menduner/system/enums/ErrorCodeConstants.java

@@ -26,7 +26,7 @@ public interface ErrorCodeConstants {
     ErrorCode MDE_COMMON_USER_ID_NOT_NULL = new ErrorCode(1_099_000_006, "用户id不能为空");
     ErrorCode MDE_COMMON_ENTERPRISE_ID_NOT_NULL = new ErrorCode(1_099_000_007, "企业id不能为空");
     ErrorCode MDE_COMMON_USER_TYPE_NOT_NULL = new ErrorCode(1_099_000_008, "用户类型不能为空");
-    ErrorCode MDE_FILE_FORMAT_ERROR = new ErrorCode(1_099_000_009, "文件格式不正确,仅支持.doc, .docx, .pdf文件");
+    ErrorCode MDE_FILE_FORMAT_ERROR = new ErrorCode(1_099_000_009, "文件格式不正确");
     ErrorCode MDE_FILE_NOT_NULL = new ErrorCode(1_099_000_010, "文件附件不能为空");
     ErrorCode MDE_REQUEST_ILLEGAL = new ErrorCode(1_099_000_011, "非法请求");
 

+ 45 - 4
menduner/menduner-system-biz/src/main/java/com/citu/module/menduner/system/controller/app/common/file/AppFileController.java

@@ -4,6 +4,7 @@ import cn.hutool.core.io.IoUtil;
 import com.citu.framework.common.pojo.CommonResult;
 import com.citu.framework.security.core.annotations.PreAuthenticated;
 import com.citu.module.infra.api.file.FileApi;
+import com.citu.module.menduner.common.util.LoginUserContext;
 import com.citu.module.menduner.system.controller.app.common.file.vo.AppFileUploadReqVO;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
@@ -21,6 +22,7 @@ import javax.annotation.Resource;
 import static com.citu.framework.common.exception.util.ServiceExceptionUtil.exception;
 import static com.citu.framework.common.pojo.CommonResult.success;
 import static com.citu.module.menduner.system.enums.ErrorCodeConstants.MDE_FILE_FORMAT_ERROR;
+import static com.citu.module.menduner.system.enums.ErrorCodeConstants.MDE_REQUEST_ILLEGAL;
 
 @Tag(name = "公共 - 文件存储")
 @RestController
@@ -36,12 +38,21 @@ public class AppFileController {
     @PostMapping("/upload")
     @Operation(summary = "上传文件")
     public CommonResult<String> uploadFile(AppFileUploadReqVO reqVO) throws Exception {
+        if (!checkFileType(reqVO)) {
+            throw exception(MDE_FILE_FORMAT_ERROR);
+        }
+
         MultipartFile file = reqVO.getFile();
         String path = reqVO.getPath();
-        if (!isSupportedDocument(file)) {
-            throw exception(MDE_FILE_FORMAT_ERROR);
+        if (StringUtils.hasText(path)) {
+            if (path.endsWith("/")) {
+                path += file.getOriginalFilename();
+            } else {
+                path += "/" + file.getOriginalFilename();
+            }
         }
-        return success(fileApi.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
+
+        return success(fileApi.createFile(file.getOriginalFilename(), getPathStartWith() + path, IoUtil.readBytes(file.getInputStream())));
     }
 
     private boolean isSupportedDocument(MultipartFile file) throws Exception {
@@ -52,7 +63,10 @@ public class AppFileController {
         }
 
         String extension = originalFilename.toLowerCase();
-        if (!extension.endsWith(".doc") && !extension.endsWith(".docx") && !extension.endsWith(".pdf")) {
+        if (!extension.endsWith(".doc")
+                && !extension.endsWith(".docx")
+                && !extension.endsWith(".pdf")
+        ) {
             return false;
         }
 
@@ -67,9 +81,36 @@ public class AppFileController {
             case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
             case "application/pdf":
                 return true;
+            default:
+                throw exception(MDE_REQUEST_ILLEGAL);
+        }
+
+    }
+
+    private boolean checkFileType(AppFileUploadReqVO reqVO) throws Exception {
+        switch (reqVO.getPath()) {
+
+            case AppFileUploadReqVO.ATTACHMENT_TYPE:
+                return isSupportedDocument(reqVO.getFile());
+            case AppFileUploadReqVO.IMG_TYPE:
+            case AppFileUploadReqVO.VIDEO_TYPE:
+            case AppFileUploadReqVO.OTHER_TYPE:
+                return true;
             default:
                 return false;
         }
 
     }
+
+    private String getPathStartWith() {
+        Long userId = LoginUserContext.getUserId();
+        String path = "";
+        if (LoginUserContext.checkIsEnterpriseUserRetBool()) {
+            // 是企业操作
+            path += "enterprise/" + LoginUserContext.getEnterpriseId();
+        } else {
+            path += "person/" + userId;
+        }
+        return path + "/";
+    }
 }

+ 6 - 1
menduner/menduner-system-biz/src/main/java/com/citu/module/menduner/system/controller/app/common/file/vo/AppFileUploadReqVO.java

@@ -9,12 +9,17 @@ import javax.validation.constraints.NotNull;
 @Schema(description = "上传文件 Request VO")
 @Data
 public class AppFileUploadReqVO {
+    public static final String ATTACHMENT_TYPE = "attachment";
+    public static final String IMG_TYPE = "img";
+    public static final String VIDEO_TYPE = "video";
+    public static final String OTHER_TYPE = "other";
+
 
     @Schema(description = "文件附件", requiredMode = Schema.RequiredMode.REQUIRED)
     @NotNull(message = "{1_099_000_010}")
     private MultipartFile file;
 
-    @Schema(description = "文件附件", example = "cituyuanma.png")
+    @Schema(description = "文件路径", example = "attachment | img | video | other")
     private String path;
 
 }