refactor: 收紧附件上传接口

This commit is contained in:
zhiye.sun
2026-05-19 16:21:10 +08:00
committed by bruce
parent 067b098aa1
commit 603c006c49
5 changed files with 45 additions and 21 deletions

View File

@@ -1,15 +1,16 @@
package com.bruce.common.controller; package com.bruce.common.controller;
import com.bruce.common.entity.SysAttachment; import com.bruce.common.domain.entity.SysAttachment;
import com.bruce.common.domain.model.RequestResult;
import com.bruce.common.dto.request.SysAttachmentUploadRequest;
import com.bruce.common.service.ISysAttachmentService; import com.bruce.common.service.ISysAttachmentService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@Tag(name = "系统附件管理") @Tag(name = "系统附件管理")
@RestController @RestController
@@ -21,9 +22,7 @@ public class SysAttachmentController {
@Operation(summary = "上传附件") @Operation(summary = "上传附件")
@PostMapping("/upload") @PostMapping("/upload")
public SysAttachment upload(@RequestParam("file") MultipartFile file, public RequestResult<SysAttachment> upload(@ModelAttribute SysAttachmentUploadRequest request) {
@RequestParam String sourceType, return RequestResult.success(sysAttachmentService.upload(request));
@RequestParam(required = false) Long sourceId) {
return sysAttachmentService.upload(file, sourceType, sourceId);
} }
} }

View File

@@ -0,0 +1,19 @@
package com.bruce.common.dto.request;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;
@Data
@Schema(description = "附件上传请求")
public class SysAttachmentUploadRequest {
@Schema(description = "上传文件")
private MultipartFile file;
@Schema(description = "来源类型")
private String sourceType;
@Schema(description = "来源业务ID")
private Long sourceId;
}

View File

@@ -1,10 +1,10 @@
package com.bruce.common.service; package com.bruce.common.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.bruce.common.entity.SysAttachment; import com.bruce.common.domain.entity.SysAttachment;
import org.springframework.web.multipart.MultipartFile; import com.bruce.common.dto.request.SysAttachmentUploadRequest;
public interface ISysAttachmentService extends IService<SysAttachment> { public interface ISysAttachmentService extends IService<SysAttachment> {
SysAttachment upload(MultipartFile file, String sourceType, Long sourceId); SysAttachment upload(SysAttachmentUploadRequest request);
} }

View File

@@ -2,7 +2,8 @@ package com.bruce.common.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bruce.common.config.AttachmentProperties; import com.bruce.common.config.AttachmentProperties;
import com.bruce.common.entity.SysAttachment; import com.bruce.common.domain.entity.SysAttachment;
import com.bruce.common.dto.request.SysAttachmentUploadRequest;
import com.bruce.common.mapper.SysAttachmentMapper; import com.bruce.common.mapper.SysAttachmentMapper;
import com.bruce.common.service.ISysAttachmentService; import com.bruce.common.service.ISysAttachmentService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -29,10 +30,15 @@ public class SysAttachmentServiceImpl extends ServiceImpl<SysAttachmentMapper, S
} }
@Override @Override
public SysAttachment upload(MultipartFile file, String sourceType, Long sourceId) { public SysAttachment upload(SysAttachmentUploadRequest request) {
if (request == null) {
throw new IllegalArgumentException("上传请求不能为空");
}
MultipartFile file = request.getFile();
if (file == null || file.isEmpty()) { if (file == null || file.isEmpty()) {
throw new IllegalArgumentException("上传文件不能为空"); throw new IllegalArgumentException("上传文件不能为空");
} }
String sourceType = request.getSourceType();
if (!StringUtils.hasText(sourceType)) { if (!StringUtils.hasText(sourceType)) {
throw new IllegalArgumentException("sourceType不能为空"); throw new IllegalArgumentException("sourceType不能为空");
} }
@@ -54,7 +60,7 @@ public class SysAttachmentServiceImpl extends ServiceImpl<SysAttachmentMapper, S
SysAttachment attachment = new SysAttachment(); SysAttachment attachment = new SysAttachment();
attachment.setSourceType(sourceType); attachment.setSourceType(sourceType);
attachment.setSourceId(sourceId); attachment.setSourceId(request.getSourceId());
attachment.setOriginalName(originalName); attachment.setOriginalName(originalName);
attachment.setFileName(storedFileName); attachment.setFileName(storedFileName);
attachment.setFileSuffix(suffix); attachment.setFileSuffix(suffix);

View File

@@ -4,15 +4,17 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bruce.common.controller.SysAttachmentController; import com.bruce.common.controller.SysAttachmentController;
import com.bruce.common.entity.SysAttachment; import com.bruce.common.domain.model.RequestResult;
import com.bruce.common.dto.request.SysAttachmentUploadRequest;
import com.bruce.common.domain.entity.SysAttachment;
import com.bruce.common.mapper.SysAttachmentMapper; import com.bruce.common.mapper.SysAttachmentMapper;
import com.bruce.common.service.ISysAttachmentService; import com.bruce.common.service.ISysAttachmentService;
import com.bruce.common.service.impl.SysAttachmentServiceImpl; import com.bruce.common.service.impl.SysAttachmentServiceImpl;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.web.multipart.MultipartFile;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -31,18 +33,16 @@ class SysAttachmentComponentStructureTests {
void sysAttachmentShouldExposeUploadMethods() throws NoSuchMethodException { void sysAttachmentShouldExposeUploadMethods() throws NoSuchMethodException {
Method serviceMethod = ISysAttachmentService.class.getMethod( Method serviceMethod = ISysAttachmentService.class.getMethod(
"upload", "upload",
MultipartFile.class, SysAttachmentUploadRequest.class
String.class,
Long.class
); );
Method controllerMethod = SysAttachmentController.class.getMethod( Method controllerMethod = SysAttachmentController.class.getMethod(
"upload", "upload",
MultipartFile.class, SysAttachmentUploadRequest.class
String.class,
Long.class
); );
assertNotNull(serviceMethod); assertNotNull(serviceMethod);
assertNotNull(controllerMethod); assertNotNull(controllerMethod);
assertEquals(SysAttachment.class, serviceMethod.getReturnType());
assertEquals(RequestResult.class, controllerMethod.getReturnType());
} }
} }