|
@@ -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 + "/";
|
|
|
+ }
|
|
|
}
|