22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 1 | package com.pt.controller; |
| 2 | |
| 3 | import com.pt.constant.Constants; |
| 4 | import com.pt.entity.Resource; |
| 5 | import com.pt.entity.User; |
| 6 | import com.pt.service.ResourceService; |
| 7 | import com.pt.service.UserService; |
| 8 | import com.pt.utils.JWTUtils; |
| 9 | import org.springframework.beans.factory.annotation.Autowired; |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 10 | import org.springframework.context.annotation.Bean; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 11 | import org.springframework.http.ResponseEntity; |
| 12 | import org.springframework.web.bind.annotation.*; |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 13 | import org.springframework.web.multipart.MultipartFile; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 14 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 15 | import java.io.IOException; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 16 | import java.util.HashMap; |
| 17 | import java.util.List; |
| 18 | import java.util.Map; |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 19 | import java.io.File; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 20 | |
| 21 | @RestController |
| 22 | @RequestMapping("/api/resource") |
| 23 | @CrossOrigin(origins = "*") |
| 24 | public class ResourceController { |
| 25 | |
| 26 | @Autowired |
| 27 | private ResourceService resourceService; |
yyyang | f786cfa | 2025-06-08 15:13:34 +0800 | [diff] [blame] | 28 | |
| 29 | @Autowired |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 30 | private UserService userService; |
| 31 | |
| 32 | @GetMapping("/list/all") |
| 33 | public ResponseEntity<?> getAllResources(@RequestHeader("token") String token, |
| 34 | @RequestParam("username") String username) { |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 35 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 36 | Map<String, Object> ans = new HashMap<>(); |
| 37 | |
| 38 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 39 | ans.put("message", "Invalid token"); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 40 | return ResponseEntity.badRequest().body(ans); |
| 41 | } |
| 42 | |
| 43 | List<Resource> resources = resourceService.getAllResources(); |
| 44 | if (resources.isEmpty()) { |
| 45 | return ResponseEntity.noContent().build(); |
| 46 | } |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 47 | return ResponseEntity.ok(resources); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @GetMapping("/list/user") |
| 51 | public ResponseEntity<?> getUserResources(@RequestHeader("token") String token, |
| 52 | @RequestParam("username") String username) { |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 53 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 54 | Map<String, Object> ans = new HashMap<>(); |
| 55 | |
| 56 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 57 | ans.put("message", "Invalid token"); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 58 | return ResponseEntity.badRequest().body(ans); |
| 59 | } |
| 60 | |
| 61 | List<Resource> resources = resourceService.getResourcesByAuthor(username); |
| 62 | if (resources.isEmpty()) { |
| 63 | return ResponseEntity.noContent().build(); |
| 64 | } |
| 65 | return ResponseEntity.ok(resources); |
| 66 | } |
| 67 | |
| 68 | @PostMapping("/publish") |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 69 | public ResponseEntity<?> publishResource( |
| 70 | @RequestHeader("token") String token, |
| 71 | @RequestParam("username") String username, |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 72 | @RequestParam("description") String description, |
| 73 | @RequestParam("torrent") MultipartFile torrentFile) { |
| 74 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 75 | Map<String, Object> ans = new HashMap<>(); |
| 76 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 77 | if (!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) { |
22301102 | ca0fb2f | 2025-06-09 18:40:42 +0800 | [diff] [blame] | 78 | ans.put("result", "Invalid token"); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 79 | return ResponseEntity.badRequest().body(ans); |
| 80 | } |
| 81 | |
| 82 | User user = userService.findByUsername(username); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 83 | if (user == null || user.getLevel() < 2) { |
22301102 | ca0fb2f | 2025-06-09 18:40:42 +0800 | [diff] [blame] | 84 | ans.put("result", "Insufficient permissions to publish resources"); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 85 | return ResponseEntity.status(403).body(ans); |
| 86 | } |
| 87 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 88 | try { |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 89 | // 传入种子文件字节,同时传入资源其他信息 |
Edwardsamaxl | af825a2 | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 90 | System.out.println("name" + torrentFile.getOriginalFilename()); |
| 91 | resourceService.publishResource(torrentFile.getOriginalFilename(), description, username, torrentFile.getSize(), torrentFile.getBytes()); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 92 | } catch (Exception e) { |
22301102 | ca0fb2f | 2025-06-09 18:40:42 +0800 | [diff] [blame] | 93 | ans.put("result", "Failed to publish resource: " + e.getMessage()); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 94 | return ResponseEntity.status(500).body(ans); |
| 95 | } |
22301102 | fe5f841 | 2025-06-01 17:25:51 +0800 | [diff] [blame] | 96 | |
22301102 | ca0fb2f | 2025-06-09 18:40:42 +0800 | [diff] [blame] | 97 | ans.put("result", "Resource published successfully"); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 98 | return ResponseEntity.ok(ans); |
| 99 | } |
| 100 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 101 | |
| 102 | |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 103 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 104 | @GetMapping("/get/{resourceId}") |
| 105 | public ResponseEntity<?> getResourceById(@PathVariable("resourceId") int resourceId, |
Edwardsamaxl | af825a2 | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 106 | // @RequestHeader("token") String token, |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 107 | @RequestParam("username") String username) { |
| 108 | |
| 109 | Map<String, Object> ans = new HashMap<>(); |
Edwardsamaxl | af825a2 | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 110 | // if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) { |
| 111 | // ans.put("message", "Invalid token"); |
| 112 | // return ResponseEntity.badRequest().body(ans); |
| 113 | // } |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 114 | |
| 115 | Resource resource = resourceService.getResourceById(resourceId); |
| 116 | if (resource == null) { |
| 117 | return ResponseEntity.notFound().build(); |
| 118 | } |
| 119 | return ResponseEntity.ok(resource); |
| 120 | } |
| 121 | |
| 122 | @GetMapping("/download/{resourceId}") |
| 123 | public ResponseEntity<?> downloadResource(@PathVariable("resourceId") int resourceId, |
Edwardsamaxl | af825a2 | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 124 | // @RequestHeader("token") String token, |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 125 | @RequestParam("username") String username) throws IOException { |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 126 | |
| 127 | Map<String, Object> ans = new HashMap<>(); |
Edwardsamaxl | af825a2 | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 128 | // if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) { |
| 129 | // ans.put("message", "Invalid token"); |
| 130 | // return ResponseEntity.badRequest().body(ans); |
| 131 | // } |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 132 | |
yyyang | f786cfa | 2025-06-08 15:13:34 +0800 | [diff] [blame] | 133 | User user = userService.findByUsername(username); |
| 134 | if (user == null) { |
| 135 | ans.put("message", "User not found"); |
| 136 | return ResponseEntity.status(404).body(ans); |
| 137 | } |
| 138 | |
| 139 | final double MIN_SHARE_RATIO_THRESHOLD = 0.5; // 最低共享率要求 |
| 140 | final long DOWNLOAD_EXEMPTION_BYTES = 20L * 1024L * 1024L * 1024L; // 20GB下载量豁免 |
| 141 | |
| 142 | long uploaded = user.getUploaded(); |
| 143 | long downloaded = user.getDownloaded(); |
| 144 | |
| 145 | // 如果用户的下载量超过豁免值,则检查其共享率 |
| 146 | if (downloaded > DOWNLOAD_EXEMPTION_BYTES) { |
| 147 | // 防止除以零 |
| 148 | double shareRatio = (downloaded == 0) ? Double.MAX_VALUE : (double) uploaded / downloaded; |
| 149 | if (shareRatio < MIN_SHARE_RATIO_THRESHOLD) { |
| 150 | ans.put("message", "Share ratio is too low. Please seed more to improve your ratio before downloading new resources."); |
| 151 | ans.put("current_share_ratio", String.format("%.2f", shareRatio)); |
| 152 | ans.put("required_share_ratio", MIN_SHARE_RATIO_THRESHOLD); |
| 153 | return ResponseEntity.status(403).body(ans); // 403 Forbidden |
| 154 | } |
| 155 | } |
| 156 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 157 | Resource resource = resourceService.getResourceById(resourceId); |
| 158 | byte[] file = resourceService.getTorrentFileByResource(resource, username); |
| 159 | if (file == null) { |
| 160 | return ResponseEntity.notFound().build(); |
| 161 | } |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 162 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 163 | return ResponseEntity.ok() |
| 164 | .header("Content-Type", "application/x-bittorrent") |
| 165 | .header("Content-Disposition", "attachment; filename=\"" + resource.getName() + ".torrent\"") |
| 166 | .body(file); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @GetMapping("/search") |
| 170 | public ResponseEntity<?> searchResources(@RequestHeader("token") String token, |
| 171 | @RequestParam("username") String username, |
| 172 | @RequestParam("query") String query) { |
| 173 | Map<String, Object> ans = new HashMap<>(); |
| 174 | |
| 175 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 176 | ans.put("message", "Invalid token"); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 177 | return ResponseEntity.badRequest().body(ans); |
| 178 | } |
| 179 | |
| 180 | List<Resource> resources = resourceService.searchByQuery(query); |
| 181 | if (resources.isEmpty()) { |
| 182 | return ResponseEntity.noContent().build(); |
| 183 | } |
| 184 | return ResponseEntity.ok(resources); |
| 185 | } |
| 186 | |
| 187 | @GetMapping("/delete") |
| 188 | public ResponseEntity<?> deleteResource(@RequestHeader("token") String token, |
| 189 | @RequestParam("username") String username, |
| 190 | @RequestParam("resourceId") int resourceId) { |
| 191 | Map<String, Object> ans = new HashMap<>(); |
| 192 | Resource resource = resourceService.getResourceById(resourceId); |
| 193 | |
| 194 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN) || resource == null || !resource.getAuthor().equals(username)) { |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 195 | ans.put("message", "Invalid token or insufficient permissions"); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 196 | return ResponseEntity.badRequest().body(ans); |
| 197 | } |
| 198 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 199 | try { |
| 200 | resourceService.deleteResource(resourceId); |
| 201 | } catch (Exception e) { |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 202 | ans.put("message", "Failed to delete resource: " + e.getMessage()); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 203 | return ResponseEntity.status(500).body(ans); |
| 204 | } |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 205 | |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 206 | ans.put("message", "Resource deleted successfully"); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 207 | return ResponseEntity.ok(ans); |
| 208 | } |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 209 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 210 | } |