AttachmentController.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package space.anyi.serve.controller;
  2. import io.swagger.v3.oas.annotations.Operation;
  3. import io.swagger.v3.oas.annotations.tags.Tag;
  4. import org.springframework.security.access.prepost.PreAuthorize;
  5. import org.springframework.security.core.Authentication;
  6. import org.springframework.web.bind.annotation.*;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import space.anyi.serve.entity.Response;
  9. import space.anyi.serve.entity.attachment.UploadVo;
  10. import space.anyi.serve.entity.auth.JwtUserDetails;
  11. import space.anyi.serve.service.AttachmentService;
  12. @Tag(name = "AttachmentController", description = "附件上传")
  13. @RestController
  14. @RequestMapping("attachments")
  15. public class AttachmentController {
  16. private final AttachmentService attachmentService;
  17. public AttachmentController(AttachmentService attachmentService) {
  18. this.attachmentService = attachmentService;
  19. }
  20. @Operation(summary = "上传附件到OSS(需对接OSS配置)")
  21. @PreAuthorize("hasAnyRole('ROLE_user', 'ROLE_expert', 'ROLE_admin')")
  22. @PostMapping("upload")
  23. public Response<UploadVo> upload(
  24. @RequestParam("file") MultipartFile file,
  25. @RequestParam(defaultValue = "post_image") String type,
  26. Authentication authentication) {
  27. JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal();
  28. Long userId = details.getUser().getId();
  29. // TODO: 对接 OSS 上传,当前返回占位 URL
  30. String url = "https://oss.example.com/" + System.currentTimeMillis() + "_" + file.getOriginalFilename();
  31. attachmentService.upload(userId, url, type);
  32. return Response.ok(new UploadVo(url));
  33. }
  34. }