Jelajahi Sumber

1、增加文件上传接口,并限制文件上传格式

rayson 8 bulan lalu
induk
melakukan
a8e09e6ac3

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

@@ -26,6 +26,9 @@ 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_NOT_NULL = new ErrorCode(1_099_000_010, "文件附件不能为空");
 
 
     // ========== 人员信息-个人档案 1_100_001_000 ==========
@@ -570,10 +573,10 @@ public interface ErrorCodeConstants {
 
     ErrorCode ENTERPRISE_ENTITLEMENT_QUOTA_OVERFLOW = new ErrorCode(1_100_054_002, "企业额度已超过");
 
-    // ========== 早报资讯 1_100_054_000 ==========
-    ErrorCode MORNING_NEWS_NOT_EXISTS = new ErrorCode(1_100_054_001, "早报资讯不存在");
+    // ========== 早报资讯 1_100_055_000 ==========
+    ErrorCode MORNING_NEWS_NOT_EXISTS = new ErrorCode(1_100_055_001, "早报资讯不存在");
 
-    // ========== 招聘会 1_100_055_000 ==========
-    ErrorCode JOB_FAIR_NOT_EXISTS = new ErrorCode(1_100_055_001, "招聘会不存在");
+    // ========== 招聘会 1_100_056_000 ==========
+    ErrorCode JOB_FAIR_NOT_EXISTS = new ErrorCode(1_100_056_001, "招聘会不存在");
 
 }

+ 5 - 0
menduner/menduner-system-biz/pom.xml

@@ -55,6 +55,11 @@
             <artifactId>citu-spring-boot-starter-job</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.apache.tika</groupId>
+            <artifactId>tika-core</artifactId> <!-- 文件客户端:文件类型的识别 -->
+        </dependency>
+
         <dependency>
             <groupId>com.citu</groupId>
             <artifactId>citu-spring-boot-starter-protection</artifactId>

+ 1 - 1
menduner/menduner-system-biz/src/main/java/com/citu/module/menduner/system/controller/app/common/auth/AppMdeAuthController.java

@@ -27,7 +27,7 @@ import javax.validation.Valid;
 
 import static com.citu.framework.common.pojo.CommonResult.success;
 
-@Tag(name = "求职端 - 用户登录")
+@Tag(name = "公共 - 用户登录")
 @RestController
 @RequestMapping("/menduner/system/auth")
 @Validated

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

@@ -0,0 +1,75 @@
+package com.citu.module.menduner.system.controller.app.common.file;
+
+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.system.controller.app.common.file.vo.AppFileUploadReqVO;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.tika.Tika;
+import org.springframework.util.StringUtils;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+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;
+
+@Tag(name = "公共 - 文件存储")
+@RestController
+@RequestMapping("/menduner/system/file")
+@Validated
+@Slf4j
+public class AppFileController {
+
+    @Resource
+    private FileApi fileApi;
+
+    @PreAuthenticated
+    @PostMapping("/upload")
+    @Operation(summary = "上传文件")
+    public CommonResult<String> uploadFile(AppFileUploadReqVO reqVO) throws Exception {
+        MultipartFile file = reqVO.getFile();
+        String path = reqVO.getPath();
+        if (!isSupportedDocument(file)) {
+            throw exception(MDE_FILE_FORMAT_ERROR);
+        }
+        return success(fileApi.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
+    }
+
+    private boolean isSupportedDocument(MultipartFile file) throws Exception {
+        // 检查文件扩展名
+        String originalFilename = file.getOriginalFilename();
+        if (originalFilename == null || originalFilename.isEmpty()) {
+            return false;
+        }
+
+        String extension = originalFilename.toLowerCase();
+        if (!extension.endsWith(".doc") && !extension.endsWith(".docx") && !extension.endsWith(".pdf")) {
+            return false;
+        }
+
+        // 使用Apache Tika检测文件的实际MIME类型
+        String detectedMimeType = new Tika().detect(file.getInputStream(), originalFilename);
+        if (!StringUtils.hasText(detectedMimeType)) {
+            return false;
+        }
+
+        switch (detectedMimeType.toLowerCase()) {
+            case "application/msword":
+            case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
+            case "application/pdf":
+                return true;
+            default:
+                return false;
+        }
+
+    }
+}

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

@@ -0,0 +1,20 @@
+package com.citu.module.menduner.system.controller.app.common.file.vo;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.validation.constraints.NotNull;
+
+@Schema(description = "上传文件 Request VO")
+@Data
+public class AppFileUploadReqVO {
+
+    @Schema(description = "文件附件", requiredMode = Schema.RequiredMode.REQUIRED)
+    @NotNull(message = "{1_099_000_010}")
+    private MultipartFile file;
+
+    @Schema(description = "文件附件", example = "cituyuanma.png")
+    private String path;
+
+}

+ 1 - 1
menduner/menduner-system-biz/src/main/java/com/citu/module/menduner/system/controller/app/common/social/AppSocialUserController.java

@@ -26,7 +26,7 @@ import java.util.List;
 
 import static com.citu.framework.common.pojo.CommonResult.success;
 
-@Tag(name = "求职端 - 社交用户")
+@Tag(name = "公共 - 社交用户")
 @RestController
 @RequestMapping("/menduner/system/social-user")
 @Validated

+ 1 - 1
menduner/menduner-system-biz/src/main/java/com/citu/module/menduner/system/controller/app/common/test/TestController.java

@@ -16,7 +16,7 @@ import java.util.Locale;
 import static com.citu.framework.common.exception.util.ServiceExceptionUtil.exception;
 import static com.citu.module.menduner.system.enums.ErrorCodeConstants.MDE_AREA_HOT_EXISTS;
 
-@Tag(name = "求职端 - test")
+@Tag(name = "公共 - test")
 @RestController
 @RequestMapping("/menduner/system/test")
 @Validated

+ 2 - 0
menduner/menduner-system-biz/src/main/java/com/citu/module/menduner/system/framework/rpc/config/RpcConfiguration.java

@@ -1,5 +1,6 @@
 package com.citu.module.menduner.system.framework.rpc.config;
 
+import com.citu.module.infra.api.file.FileApi;
 import com.citu.module.menduner.system.api.python.GraphApi;
 import com.citu.module.pay.api.order.PayOrderApi;
 import com.citu.module.pay.api.refund.PayRefundApi;
@@ -15,6 +16,7 @@ import org.springframework.context.annotation.Configuration;
 @Configuration(proxyBeanMethods = false)
 @EnableFeignClients(clients = {
         SmsCodeApi.class,
+        FileApi.class,
         LoginLogApi.class,
         SocialUserApi.class,
         SocialClientApi.class,

+ 7 - 1
menduner/menduner-system-biz/src/main/resources/i18n/messages_en_US.properties

@@ -26,6 +26,8 @@
 1_099_000_006=User ID cannot be empty
 1_099_000_007=Enterprise ID cannot be empty
 1_099_000_008=User Type cannot be empty
+1_099_000_009=The file format is incorrect and only supports. doc,. docx, and. pdf files
+1_099_000_010=File attachment cannot be empty
 # ========== 人才信息-人才档案 1_100_001_000 ==========
 1_100_001_001=Personal Profile Does Not Exist
 1_100_001_002=Personal advantage cannot be empty
@@ -399,4 +401,8 @@
 1_100_053_009=The number of talents allowed to be searched cannot be empty
 # ========== 企业权益记录 1_100_054_000 ==========
 1_100_054_001=Enterprise equity record does not exist
-1_100_054_002=Enterprise credit limit has exceeded
+1_100_054_002=Enterprise credit limit has exceeded
+# ========== 早报资讯 1_100_055_000 ==========
+1_100_055_001=Morning News does not exist
+# ========== 招聘会 1_100_056_000 ==========
+1_100_056_001=The job fair does not exist

+ 7 - 1
menduner/menduner-system-biz/src/main/resources/i18n/messages_zh_CN.properties

@@ -26,6 +26,8 @@
 1_099_000_006=用户id不能为空
 1_099_000_007=企业id不能为空
 1_099_000_008=用户类型不能为空
+1_099_000_009=文件格式不正确,仅支持.doc, .docx, .pdf文件
+1_099_000_010=文件附件不能为空
 # ========== 人才信息-人才档案 1_100_001_000 ==========
 1_100_001_001=人才档案不存在
 1_100_001_002=人才优势不能为空
@@ -399,4 +401,8 @@
 1_100_053_009=允许搜索人才的数量不能为空
 # ========== 企业权益记录 1_100_054_000 ==========
 1_100_054_001=企业权益记录不存在
-1_100_054_002=企业额度已超过
+1_100_054_002=企业额度已超过
+# ========== 早报资讯 1_100_055_000 ==========
+1_100_055_001=早报资讯不存在
+# ========== 招聘会 1_100_056_000 ==========
+1_100_056_001=招聘会不存在