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.*; |
| 12 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 13 | import java.io.IOException; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 14 | import java.util.HashMap; |
| 15 | import java.util.List; |
| 16 | import java.util.Map; |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 17 | import java.io.File; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 18 | |
| 19 | @RestController |
| 20 | @RequestMapping("/api/resource") |
| 21 | @CrossOrigin(origins = "*") |
| 22 | public class ResourceController { |
| 23 | |
| 24 | @Autowired |
| 25 | private ResourceService resourceService; |
| 26 | private UserService userService; |
| 27 | |
| 28 | @GetMapping("/list/all") |
| 29 | public ResponseEntity<?> getAllResources(@RequestHeader("token") String token, |
| 30 | @RequestParam("username") String username) { |
| 31 | Map<String, Object> ans = new HashMap<>(); |
| 32 | |
| 33 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
| 34 | ans.put("result", "Invalid token"); |
| 35 | return ResponseEntity.badRequest().body(ans); |
| 36 | } |
| 37 | |
| 38 | List<Resource> resources = resourceService.getAllResources(); |
| 39 | if (resources.isEmpty()) { |
| 40 | return ResponseEntity.noContent().build(); |
| 41 | } |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 42 | return ResponseEntity.ok(resources); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | @GetMapping("/list/user") |
| 46 | public ResponseEntity<?> getUserResources(@RequestHeader("token") String token, |
| 47 | @RequestParam("username") String username) { |
| 48 | Map<String, Object> ans = new HashMap<>(); |
| 49 | |
| 50 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
| 51 | ans.put("result", "Invalid token"); |
| 52 | return ResponseEntity.badRequest().body(ans); |
| 53 | } |
| 54 | |
| 55 | List<Resource> resources = resourceService.getResourcesByAuthor(username); |
| 56 | if (resources.isEmpty()) { |
| 57 | return ResponseEntity.noContent().build(); |
| 58 | } |
| 59 | return ResponseEntity.ok(resources); |
| 60 | } |
| 61 | |
| 62 | @PostMapping("/publish") |
| 63 | public ResponseEntity<?> publishResource(@RequestHeader("token") String token, |
| 64 | @RequestParam("username") String username, |
| 65 | @RequestParam("size") double size, |
| 66 | @RequestParam("name") String name, |
| 67 | @RequestParam("description") String description) { |
| 68 | Map<String, Object> ans = new HashMap<>(); |
| 69 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 70 | if (!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) { |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 71 | ans.put("result", "Invalid token"); |
| 72 | return ResponseEntity.badRequest().body(ans); |
| 73 | } |
| 74 | |
| 75 | User user = userService.findByUsername(username); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 76 | if (user == null || user.getLevel() < 2) { |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 77 | ans.put("result", "Insufficient permissions to publish resources"); |
| 78 | return ResponseEntity.status(403).body(ans); |
| 79 | } |
| 80 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 81 | try { |
| 82 | resourceService.publishResource(name, description, username, size); |
| 83 | } catch (Exception e) { |
| 84 | ans.put("result", "Failed to publish resource: " + e.getMessage()); |
| 85 | return ResponseEntity.status(500).body(ans); |
| 86 | } |
22301102 | fe5f841 | 2025-06-01 17:25:51 +0800 | [diff] [blame] | 87 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 88 | ans.put("result", "Resource published successfully"); |
| 89 | return ResponseEntity.ok(ans); |
| 90 | } |
| 91 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 92 | |
| 93 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 94 | @GetMapping("/get/{resourceId}") |
| 95 | public ResponseEntity<?> getResourceById(@PathVariable("resourceId") int resourceId, |
| 96 | @RequestHeader("token") String token, |
| 97 | @RequestParam("username") String username) { |
| 98 | |
| 99 | Map<String, Object> ans = new HashMap<>(); |
| 100 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) { |
| 101 | ans.put("result", "Invalid token"); |
| 102 | return ResponseEntity.badRequest().body(ans); |
| 103 | } |
| 104 | |
| 105 | Resource resource = resourceService.getResourceById(resourceId); |
| 106 | if (resource == null) { |
| 107 | return ResponseEntity.notFound().build(); |
| 108 | } |
| 109 | return ResponseEntity.ok(resource); |
| 110 | } |
| 111 | |
| 112 | @GetMapping("/download/{resourceId}") |
| 113 | public ResponseEntity<?> downloadResource(@PathVariable("resourceId") int resourceId, |
| 114 | @RequestHeader("token") String token, |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 115 | @RequestParam("username") String username) throws IOException { |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 116 | |
| 117 | Map<String, Object> ans = new HashMap<>(); |
| 118 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) { |
| 119 | ans.put("result", "Invalid token"); |
| 120 | return ResponseEntity.badRequest().body(ans); |
| 121 | } |
| 122 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 123 | Resource resource = resourceService.getResourceById(resourceId); |
| 124 | byte[] file = resourceService.getTorrentFileByResource(resource, username); |
| 125 | if (file == null) { |
| 126 | return ResponseEntity.notFound().build(); |
| 127 | } |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 128 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 129 | return ResponseEntity.ok() |
| 130 | .header("Content-Type", "application/x-bittorrent") |
| 131 | .header("Content-Disposition", "attachment; filename=\"" + resource.getName() + ".torrent\"") |
| 132 | .body(file); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @GetMapping("/search") |
| 136 | public ResponseEntity<?> searchResources(@RequestHeader("token") String token, |
| 137 | @RequestParam("username") String username, |
| 138 | @RequestParam("query") String query) { |
| 139 | Map<String, Object> ans = new HashMap<>(); |
| 140 | |
| 141 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
| 142 | ans.put("result", "Invalid token"); |
| 143 | return ResponseEntity.badRequest().body(ans); |
| 144 | } |
| 145 | |
| 146 | List<Resource> resources = resourceService.searchByQuery(query); |
| 147 | if (resources.isEmpty()) { |
| 148 | return ResponseEntity.noContent().build(); |
| 149 | } |
| 150 | return ResponseEntity.ok(resources); |
| 151 | } |
| 152 | |
| 153 | @GetMapping("/delete") |
| 154 | public ResponseEntity<?> deleteResource(@RequestHeader("token") String token, |
| 155 | @RequestParam("username") String username, |
| 156 | @RequestParam("resourceId") int resourceId) { |
| 157 | Map<String, Object> ans = new HashMap<>(); |
| 158 | Resource resource = resourceService.getResourceById(resourceId); |
| 159 | |
| 160 | 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] | 161 | ans.put("result", "Invalid token or insufficient permissions"); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 162 | return ResponseEntity.badRequest().body(ans); |
| 163 | } |
| 164 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 165 | try { |
| 166 | resourceService.deleteResource(resourceId); |
| 167 | } catch (Exception e) { |
| 168 | ans.put("result", "Failed to delete resource: " + e.getMessage()); |
| 169 | return ResponseEntity.status(500).body(ans); |
| 170 | } |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 171 | |
| 172 | ans.put("result", "Resource deleted successfully"); |
| 173 | return ResponseEntity.ok(ans); |
| 174 | } |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 175 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 176 | } |