blob: c8098f1c9712b9b9b666da039173e20de42326f9 [file] [log] [blame]
崔向南03d21b92025-06-05 17:42:23 +08001package bounty.controller;
2
3import bounty.domain.BountySubmission;
4import bounty.service.BountySubmissionService;
5import bounty.service.FileStorageService;
6import com.baomidou.mybatisplus.core.metadata.IPage;
7import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
8import jakarta.validation.Valid;
9import org.springframework.beans.factory.annotation.Autowired;
10import org.springframework.core.io.Resource;
11import org.springframework.core.io.UrlResource;
12import org.springframework.http.*;
13import org.springframework.web.bind.annotation.*;
14import org.springframework.web.multipart.MultipartFile;
15
16import java.net.MalformedURLException;
17import java.nio.charset.StandardCharsets;
18import java.nio.file.Path;
19import java.nio.file.Paths;
20import java.util.HashMap;
21import java.util.Map;
22
23// 悬赏提交 Controller 类
24@RestController
25@RequestMapping("/bounty-submissions")
26public class BountySubmissionController {
27
28 @Autowired
29 private FileStorageService fileStorageService;
30
31
32
33 // BountySubmission.java
34 private String attachment; // 存储文件路径(如 /uploads/filename.ext)
35
36
37 @Autowired
38 private BountySubmissionService bountySubmissionService;
39
40
41 @PostMapping
42 public ResponseEntity<?> saveBountySubmission(
43 @RequestPart("submission") @Valid BountySubmission submission,
44 @RequestPart("file") MultipartFile file) {
45
46 try {
47 String filePath = fileStorageService.saveFile(file);
48 submission.setAttachment(filePath);
49 boolean saveResult = bountySubmissionService.saveBountySubmission(submission);
50
51 if (saveResult) {
52 return ResponseEntity.ok().body(Map.of("code", 200, "message", "提交成功", "data", submission));
53 } else {
54 return ResponseEntity.status(500).body(Map.of("code", 500, "message", "提交失败"));
55 }
56 } catch (Exception e) {
57 return ResponseEntity.status(500).body(Map.of("code", 500, "message", e.getMessage()));
58 }
59 }
60
61 @GetMapping
62 public IPage<BountySubmission> getBountySubmissions(
63 @RequestParam(defaultValue = "1") long current,
64 @RequestParam(defaultValue = "10") long size,
65 @RequestParam Long bountyId) {
66 Page<BountySubmission> page = new Page<>(current, size);
67 IPage<BountySubmission> result = bountySubmissionService.getBountySubmissionsByPage(page, bountyId);
68
69 // 👇 新增:打印分页数据
70 System.out.println("【分页数据】bountyId=" + bountyId + ", 数据量=" + result.getRecords().size());
71 result.getRecords().forEach(submission -> {
72 System.out.println("【附件路径】" + submission.getAttachment());
73 });
74
75 return result;
76 }
77
78 // ✅ 仅处理文件上传(保持原逻辑)
79 @PostMapping("/upload")
80 public String uploadAttachment(@RequestParam("file") MultipartFile file) {
81 return fileStorageService.saveFile(file);
82 }
83
84 @GetMapping("/download")
85 public ResponseEntity<Resource> downloadAttachment(@RequestParam String filename) {
86 try {
87 // ✅ 只使用文件名(避免重复拼接)
88 Path uploadDir = Paths.get("uploads/");
89 Path filePath = uploadDir.resolve(filename).normalize();
90
91 if (!filePath.startsWith(uploadDir)) {
92 return ResponseEntity.status(HttpStatus.FORBIDDEN).body(null);
93 }
94
95 Resource resource = new UrlResource(filePath.toUri());
96
97 if (resource.exists() && resource.isReadable()) {
98 HttpHeaders headers = new HttpHeaders();
99 headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
100 headers.setContentDisposition(ContentDisposition.builder("attachment")
101 .filename(filename, StandardCharsets.UTF_8)
102 .build());
103 return ResponseEntity.ok()
104 .headers(headers)
105 .body(resource);
106 } else {
107 return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
108 }
109 } catch (MalformedURLException e) {
110 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
111 }
112 }
113
114 @PutMapping("/{id}/adopt")
115 public ResponseEntity<?> adoptSubmission(@PathVariable Long id) {
116 try {
117 boolean result = bountySubmissionService.adoptSubmission(id);
118
119 // 使用 HashMap 替代 Map.of()
120 Map<String, Object> response = new HashMap<>();
121 if (result) {
122 response.put("code", 200);
123 response.put("message", "采纳成功");
124 return ResponseEntity.ok(response);
125 } else {
126 response.put("code", 500);
127 response.put("message", "采纳失败");
128 return ResponseEntity.status(500).body(response);
129 }
130 } catch (Exception e) {
131 Map<String, Object> errorResponse = new HashMap<>();
132 errorResponse.put("code", 500);
133 errorResponse.put("message", e.getMessage() != null ? e.getMessage() : "未知错误");
134
135 return ResponseEntity.status(500).body(errorResponse);
136 }
137 }
138
139
140 // BountySubmissionController.java
141
142}