blob: 411e7bc4ef704260f2ec63b0f0db593049d30064 [file] [log] [blame]
22301102b1084372025-06-01 16:44:23 +08001package com.pt.controller;
2
3import com.pt.constant.Constants;
4import com.pt.entity.Resource;
5import com.pt.entity.User;
6import com.pt.service.ResourceService;
7import com.pt.service.UserService;
8import com.pt.utils.JWTUtils;
9import org.springframework.beans.factory.annotation.Autowired;
10import org.springframework.http.ResponseEntity;
11import org.springframework.web.bind.annotation.*;
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080012import org.springframework.web.multipart.MultipartFile;
22301102b1084372025-06-01 16:44:23 +080013
22301102bc6da0a2025-06-02 17:47:29 +080014import java.io.IOException;
22301102b1084372025-06-01 16:44:23 +080015import java.util.HashMap;
16import java.util.List;
17import java.util.Map;
22301102bc6da0a2025-06-02 17:47:29 +080018import java.io.File;
22301102b1084372025-06-01 16:44:23 +080019
20@RestController
21@RequestMapping("/api/resource")
22@CrossOrigin(origins = "*")
23public class ResourceController {
24
25 @Autowired
26 private ResourceService resourceService;
27 private UserService userService;
28
29 @GetMapping("/list/all")
30 public ResponseEntity<?> getAllResources(@RequestHeader("token") String token,
31 @RequestParam("username") String username) {
32 Map<String, Object> ans = new HashMap<>();
33
34 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){
35 ans.put("result", "Invalid token");
36 return ResponseEntity.badRequest().body(ans);
37 }
38
39 List<Resource> resources = resourceService.getAllResources();
40 if (resources.isEmpty()) {
41 return ResponseEntity.noContent().build();
42 }
22301102bc6da0a2025-06-02 17:47:29 +080043 return ResponseEntity.ok(resources);
22301102b1084372025-06-01 16:44:23 +080044 }
45
46 @GetMapping("/list/user")
47 public ResponseEntity<?> getUserResources(@RequestHeader("token") String token,
48 @RequestParam("username") String username) {
49 Map<String, Object> ans = new HashMap<>();
50
51 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){
52 ans.put("result", "Invalid token");
53 return ResponseEntity.badRequest().body(ans);
54 }
55
56 List<Resource> resources = resourceService.getResourcesByAuthor(username);
57 if (resources.isEmpty()) {
58 return ResponseEntity.noContent().build();
59 }
60 return ResponseEntity.ok(resources);
61 }
62
63 @PostMapping("/publish")
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080064 public ResponseEntity<?> publishResource(
65 @RequestHeader("token") String token,
66 @RequestParam("username") String username,
67 @RequestParam("size") double size,
68 @RequestParam("name") String name,
69 @RequestParam("description") String description,
70 @RequestParam("torrent") MultipartFile torrentFile) {
71
22301102b1084372025-06-01 16:44:23 +080072 Map<String, Object> ans = new HashMap<>();
73
22301102bc6da0a2025-06-02 17:47:29 +080074 if (!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
22301102b1084372025-06-01 16:44:23 +080075 ans.put("result", "Invalid token");
76 return ResponseEntity.badRequest().body(ans);
77 }
78
79 User user = userService.findByUsername(username);
22301102bc6da0a2025-06-02 17:47:29 +080080 if (user == null || user.getLevel() < 2) {
22301102b1084372025-06-01 16:44:23 +080081 ans.put("result", "Insufficient permissions to publish resources");
82 return ResponseEntity.status(403).body(ans);
83 }
84
22301102bc6da0a2025-06-02 17:47:29 +080085 try {
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080086 // 传入种子文件字节,同时传入资源其他信息
87 resourceService.publishResource(name, description, username, size, torrentFile.getBytes());
22301102bc6da0a2025-06-02 17:47:29 +080088 } catch (Exception e) {
89 ans.put("result", "Failed to publish resource: " + e.getMessage());
90 return ResponseEntity.status(500).body(ans);
91 }
22301102fe5f8412025-06-01 17:25:51 +080092
22301102b1084372025-06-01 16:44:23 +080093 ans.put("result", "Resource published successfully");
94 return ResponseEntity.ok(ans);
95 }
96
22301102bc6da0a2025-06-02 17:47:29 +080097
98
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080099
22301102b1084372025-06-01 16:44:23 +0800100 @GetMapping("/get/{resourceId}")
101 public ResponseEntity<?> getResourceById(@PathVariable("resourceId") int resourceId,
102 @RequestHeader("token") String token,
103 @RequestParam("username") String username) {
104
105 Map<String, Object> ans = new HashMap<>();
106 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
107 ans.put("result", "Invalid token");
108 return ResponseEntity.badRequest().body(ans);
109 }
110
111 Resource resource = resourceService.getResourceById(resourceId);
112 if (resource == null) {
113 return ResponseEntity.notFound().build();
114 }
115 return ResponseEntity.ok(resource);
116 }
117
118 @GetMapping("/download/{resourceId}")
119 public ResponseEntity<?> downloadResource(@PathVariable("resourceId") int resourceId,
120 @RequestHeader("token") String token,
22301102bc6da0a2025-06-02 17:47:29 +0800121 @RequestParam("username") String username) throws IOException {
22301102b1084372025-06-01 16:44:23 +0800122
123 Map<String, Object> ans = new HashMap<>();
124 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
125 ans.put("result", "Invalid token");
126 return ResponseEntity.badRequest().body(ans);
127 }
128
22301102bc6da0a2025-06-02 17:47:29 +0800129 Resource resource = resourceService.getResourceById(resourceId);
130 byte[] file = resourceService.getTorrentFileByResource(resource, username);
131 if (file == null) {
132 return ResponseEntity.notFound().build();
133 }
22301102b1084372025-06-01 16:44:23 +0800134
22301102bc6da0a2025-06-02 17:47:29 +0800135 return ResponseEntity.ok()
136 .header("Content-Type", "application/x-bittorrent")
137 .header("Content-Disposition", "attachment; filename=\"" + resource.getName() + ".torrent\"")
138 .body(file);
22301102b1084372025-06-01 16:44:23 +0800139 }
140
141 @GetMapping("/search")
142 public ResponseEntity<?> searchResources(@RequestHeader("token") String token,
143 @RequestParam("username") String username,
144 @RequestParam("query") String query) {
145 Map<String, Object> ans = new HashMap<>();
146
147 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){
148 ans.put("result", "Invalid token");
149 return ResponseEntity.badRequest().body(ans);
150 }
151
152 List<Resource> resources = resourceService.searchByQuery(query);
153 if (resources.isEmpty()) {
154 return ResponseEntity.noContent().build();
155 }
156 return ResponseEntity.ok(resources);
157 }
158
159 @GetMapping("/delete")
160 public ResponseEntity<?> deleteResource(@RequestHeader("token") String token,
161 @RequestParam("username") String username,
162 @RequestParam("resourceId") int resourceId) {
163 Map<String, Object> ans = new HashMap<>();
164 Resource resource = resourceService.getResourceById(resourceId);
165
166 if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN) || resource == null || !resource.getAuthor().equals(username)) {
22301102bc6da0a2025-06-02 17:47:29 +0800167 ans.put("result", "Invalid token or insufficient permissions");
22301102b1084372025-06-01 16:44:23 +0800168 return ResponseEntity.badRequest().body(ans);
169 }
170
22301102bc6da0a2025-06-02 17:47:29 +0800171 try {
172 resourceService.deleteResource(resourceId);
173 } catch (Exception e) {
174 ans.put("result", "Failed to delete resource: " + e.getMessage());
175 return ResponseEntity.status(500).body(ans);
176 }
22301102b1084372025-06-01 16:44:23 +0800177
178 ans.put("result", "Resource deleted successfully");
179 return ResponseEntity.ok(ans);
180 }
22301102bc6da0a2025-06-02 17:47:29 +0800181
22301102b1084372025-06-01 16:44:23 +0800182}