blob: 3e3c87515a287849beb3d0dea7bebd67ac261a6d [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;
22301102f69709e2025-06-08 14:10:02 +080010import org.springframework.context.annotation.Bean;
22301102b1084372025-06-01 16:44:23 +080011import org.springframework.http.ResponseEntity;
12import org.springframework.web.bind.annotation.*;
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080013import org.springframework.web.multipart.MultipartFile;
22301102b1084372025-06-01 16:44:23 +080014
22301102bc6da0a2025-06-02 17:47:29 +080015import java.io.IOException;
22301102b1084372025-06-01 16:44:23 +080016import java.util.HashMap;
17import java.util.List;
18import java.util.Map;
22301102bc6da0a2025-06-02 17:47:29 +080019import java.io.File;
22301102b1084372025-06-01 16:44:23 +080020
21@RestController
22@RequestMapping("/api/resource")
23@CrossOrigin(origins = "*")
24public class ResourceController {
25
26 @Autowired
27 private ResourceService resourceService;
28 private UserService userService;
29
30 @GetMapping("/list/all")
31 public ResponseEntity<?> getAllResources(@RequestHeader("token") String token,
32 @RequestParam("username") String username) {
22301102f69709e2025-06-08 14:10:02 +080033
22301102b1084372025-06-01 16:44:23 +080034 Map<String, Object> ans = new HashMap<>();
35
36 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){
22301102f69709e2025-06-08 14:10:02 +080037 ans.put("message", "Invalid token");
22301102b1084372025-06-01 16:44:23 +080038 return ResponseEntity.badRequest().body(ans);
39 }
40
41 List<Resource> resources = resourceService.getAllResources();
42 if (resources.isEmpty()) {
43 return ResponseEntity.noContent().build();
44 }
22301102bc6da0a2025-06-02 17:47:29 +080045 return ResponseEntity.ok(resources);
22301102b1084372025-06-01 16:44:23 +080046 }
47
48 @GetMapping("/list/user")
49 public ResponseEntity<?> getUserResources(@RequestHeader("token") String token,
50 @RequestParam("username") String username) {
22301102f69709e2025-06-08 14:10:02 +080051
22301102b1084372025-06-01 16:44:23 +080052 Map<String, Object> ans = new HashMap<>();
53
54 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){
22301102f69709e2025-06-08 14:10:02 +080055 ans.put("message", "Invalid token");
22301102b1084372025-06-01 16:44:23 +080056 return ResponseEntity.badRequest().body(ans);
57 }
58
59 List<Resource> resources = resourceService.getResourcesByAuthor(username);
60 if (resources.isEmpty()) {
61 return ResponseEntity.noContent().build();
62 }
63 return ResponseEntity.ok(resources);
64 }
65
66 @PostMapping("/publish")
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080067 public ResponseEntity<?> publishResource(
68 @RequestHeader("token") String token,
69 @RequestParam("username") String username,
70 @RequestParam("size") double size,
71 @RequestParam("name") String name,
72 @RequestParam("description") String description,
73 @RequestParam("torrent") MultipartFile torrentFile) {
74
22301102b1084372025-06-01 16:44:23 +080075 Map<String, Object> ans = new HashMap<>();
76
22301102bc6da0a2025-06-02 17:47:29 +080077 if (!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
22301102f69709e2025-06-08 14:10:02 +080078 ans.put("message", "Invalid token");
22301102b1084372025-06-01 16:44:23 +080079 return ResponseEntity.badRequest().body(ans);
80 }
81
82 User user = userService.findByUsername(username);
22301102bc6da0a2025-06-02 17:47:29 +080083 if (user == null || user.getLevel() < 2) {
22301102f69709e2025-06-08 14:10:02 +080084 ans.put("message", "Insufficient permissions to publish resources");
22301102b1084372025-06-01 16:44:23 +080085 return ResponseEntity.status(403).body(ans);
86 }
87
22301102bc6da0a2025-06-02 17:47:29 +080088 try {
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080089 // 传入种子文件字节,同时传入资源其他信息
90 resourceService.publishResource(name, description, username, size, torrentFile.getBytes());
22301102bc6da0a2025-06-02 17:47:29 +080091 } catch (Exception e) {
22301102f69709e2025-06-08 14:10:02 +080092 ans.put("message", "Failed to publish resource: " + e.getMessage());
22301102bc6da0a2025-06-02 17:47:29 +080093 return ResponseEntity.status(500).body(ans);
94 }
22301102fe5f8412025-06-01 17:25:51 +080095
22301102f69709e2025-06-08 14:10:02 +080096 ans.put("message", "Resource published successfully");
22301102b1084372025-06-01 16:44:23 +080097 return ResponseEntity.ok(ans);
98 }
99
22301102bc6da0a2025-06-02 17:47:29 +0800100
101
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +0800102
22301102b1084372025-06-01 16:44:23 +0800103 @GetMapping("/get/{resourceId}")
104 public ResponseEntity<?> getResourceById(@PathVariable("resourceId") int resourceId,
105 @RequestHeader("token") String token,
106 @RequestParam("username") String username) {
107
108 Map<String, Object> ans = new HashMap<>();
109 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
22301102f69709e2025-06-08 14:10:02 +0800110 ans.put("message", "Invalid token");
22301102b1084372025-06-01 16:44:23 +0800111 return ResponseEntity.badRequest().body(ans);
112 }
113
114 Resource resource = resourceService.getResourceById(resourceId);
115 if (resource == null) {
116 return ResponseEntity.notFound().build();
117 }
118 return ResponseEntity.ok(resource);
119 }
120
121 @GetMapping("/download/{resourceId}")
122 public ResponseEntity<?> downloadResource(@PathVariable("resourceId") int resourceId,
123 @RequestHeader("token") String token,
22301102bc6da0a2025-06-02 17:47:29 +0800124 @RequestParam("username") String username) throws IOException {
22301102b1084372025-06-01 16:44:23 +0800125
126 Map<String, Object> ans = new HashMap<>();
127 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
22301102f69709e2025-06-08 14:10:02 +0800128 ans.put("message", "Invalid token");
22301102b1084372025-06-01 16:44:23 +0800129 return ResponseEntity.badRequest().body(ans);
130 }
131
22301102bc6da0a2025-06-02 17:47:29 +0800132 Resource resource = resourceService.getResourceById(resourceId);
133 byte[] file = resourceService.getTorrentFileByResource(resource, username);
134 if (file == null) {
135 return ResponseEntity.notFound().build();
136 }
22301102b1084372025-06-01 16:44:23 +0800137
22301102bc6da0a2025-06-02 17:47:29 +0800138 return ResponseEntity.ok()
139 .header("Content-Type", "application/x-bittorrent")
140 .header("Content-Disposition", "attachment; filename=\"" + resource.getName() + ".torrent\"")
141 .body(file);
22301102b1084372025-06-01 16:44:23 +0800142 }
143
144 @GetMapping("/search")
145 public ResponseEntity<?> searchResources(@RequestHeader("token") String token,
146 @RequestParam("username") String username,
147 @RequestParam("query") String query) {
148 Map<String, Object> ans = new HashMap<>();
149
150 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){
22301102f69709e2025-06-08 14:10:02 +0800151 ans.put("message", "Invalid token");
22301102b1084372025-06-01 16:44:23 +0800152 return ResponseEntity.badRequest().body(ans);
153 }
154
155 List<Resource> resources = resourceService.searchByQuery(query);
156 if (resources.isEmpty()) {
157 return ResponseEntity.noContent().build();
158 }
159 return ResponseEntity.ok(resources);
160 }
161
162 @GetMapping("/delete")
163 public ResponseEntity<?> deleteResource(@RequestHeader("token") String token,
164 @RequestParam("username") String username,
165 @RequestParam("resourceId") int resourceId) {
166 Map<String, Object> ans = new HashMap<>();
167 Resource resource = resourceService.getResourceById(resourceId);
168
169 if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN) || resource == null || !resource.getAuthor().equals(username)) {
22301102f69709e2025-06-08 14:10:02 +0800170 ans.put("message", "Invalid token or insufficient permissions");
22301102b1084372025-06-01 16:44:23 +0800171 return ResponseEntity.badRequest().body(ans);
172 }
173
22301102bc6da0a2025-06-02 17:47:29 +0800174 try {
175 resourceService.deleteResource(resourceId);
176 } catch (Exception e) {
22301102f69709e2025-06-08 14:10:02 +0800177 ans.put("message", "Failed to delete resource: " + e.getMessage());
22301102bc6da0a2025-06-02 17:47:29 +0800178 return ResponseEntity.status(500).body(ans);
179 }
22301102b1084372025-06-01 16:44:23 +0800180
22301102f69709e2025-06-08 14:10:02 +0800181 ans.put("message", "Resource deleted successfully");
22301102b1084372025-06-01 16:44:23 +0800182 return ResponseEntity.ok(ans);
183 }
22301102bc6da0a2025-06-02 17:47:29 +0800184
22301102b1084372025-06-01 16:44:23 +0800185}