package space.anyi.serve.controller; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import space.anyi.serve.entity.Response; import space.anyi.serve.entity.attachment.UploadVo; import space.anyi.serve.entity.auth.JwtUserDetails; import space.anyi.serve.service.AttachmentService; @Tag(name = "AttachmentController", description = "附件上传") @RestController @RequestMapping("attachments") public class AttachmentController { private final AttachmentService attachmentService; public AttachmentController(AttachmentService attachmentService) { this.attachmentService = attachmentService; } @Operation(summary = "上传附件到OSS(需对接OSS配置)") @PreAuthorize("hasAnyRole('ROLE_user', 'ROLE_expert', 'ROLE_admin')") @PostMapping("upload") public Response upload( @RequestParam("file") MultipartFile file, @RequestParam(defaultValue = "post_image") String type, Authentication authentication) { JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal(); Long userId = details.getUser().getId(); // TODO: 对接 OSS 上传,当前返回占位 URL String url = "https://oss.example.com/" + System.currentTimeMillis() + "_" + file.getOriginalFilename(); attachmentService.upload(userId, url, type); return Response.ok(new UploadVo(url)); } }