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; |
| 10 | import org.springframework.http.ResponseEntity; |
| 11 | import org.springframework.web.bind.annotation.*; |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 12 | import org.springframework.web.multipart.MultipartFile; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 13 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 14 | import java.io.IOException; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 15 | import java.util.HashMap; |
| 16 | import java.util.List; |
| 17 | import java.util.Map; |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 18 | import java.io.File; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 19 | |
| 20 | @RestController |
| 21 | @RequestMapping("/api/resource") |
| 22 | @CrossOrigin(origins = "*") |
| 23 | public 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 | } |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 43 | return ResponseEntity.ok(resources); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 44 | } |
| 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") |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 64 | 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 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 72 | Map<String, Object> ans = new HashMap<>(); |
| 73 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 74 | if (!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) { |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 75 | ans.put("result", "Invalid token"); |
| 76 | return ResponseEntity.badRequest().body(ans); |
| 77 | } |
| 78 | |
| 79 | User user = userService.findByUsername(username); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 80 | if (user == null || user.getLevel() < 2) { |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 81 | ans.put("result", "Insufficient permissions to publish resources"); |
| 82 | return ResponseEntity.status(403).body(ans); |
| 83 | } |
| 84 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 85 | try { |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 86 | // 传入种子文件字节,同时传入资源其他信息 |
| 87 | resourceService.publishResource(name, description, username, size, torrentFile.getBytes()); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 88 | } catch (Exception e) { |
| 89 | ans.put("result", "Failed to publish resource: " + e.getMessage()); |
| 90 | return ResponseEntity.status(500).body(ans); |
| 91 | } |
22301102 | fe5f841 | 2025-06-01 17:25:51 +0800 | [diff] [blame] | 92 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 93 | ans.put("result", "Resource published successfully"); |
| 94 | return ResponseEntity.ok(ans); |
| 95 | } |
| 96 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 97 | |
| 98 | |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 99 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 100 | @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, |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 121 | @RequestParam("username") String username) throws IOException { |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 122 | |
| 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 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 129 | Resource resource = resourceService.getResourceById(resourceId); |
| 130 | byte[] file = resourceService.getTorrentFileByResource(resource, username); |
| 131 | if (file == null) { |
| 132 | return ResponseEntity.notFound().build(); |
| 133 | } |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 134 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 135 | return ResponseEntity.ok() |
| 136 | .header("Content-Type", "application/x-bittorrent") |
| 137 | .header("Content-Disposition", "attachment; filename=\"" + resource.getName() + ".torrent\"") |
| 138 | .body(file); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 139 | } |
| 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)) { |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 167 | ans.put("result", "Invalid token or insufficient permissions"); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 168 | return ResponseEntity.badRequest().body(ans); |
| 169 | } |
| 170 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 171 | 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 | } |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 177 | |
| 178 | ans.put("result", "Resource deleted successfully"); |
| 179 | return ResponseEntity.ok(ans); |
| 180 | } |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 181 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 182 | } |