22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 1 | package com.pt.controller; |
| 2 | |
22301102 | fe5f841 | 2025-06-01 17:25:51 +0800 | [diff] [blame] | 3 | import com.pt.Item.ResourceInfo; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 4 | import com.pt.constant.Constants; |
| 5 | import com.pt.entity.Resource; |
| 6 | import com.pt.entity.User; |
22301102 | fe5f841 | 2025-06-01 17:25:51 +0800 | [diff] [blame] | 7 | import com.pt.service.DownloadService; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 8 | import com.pt.service.ResourceService; |
| 9 | import com.pt.service.UserService; |
| 10 | import com.pt.utils.JWTUtils; |
| 11 | import org.springframework.beans.factory.annotation.Autowired; |
| 12 | import org.springframework.http.ResponseEntity; |
| 13 | import org.springframework.web.bind.annotation.*; |
| 14 | |
22301102 | fe5f841 | 2025-06-01 17:25:51 +0800 | [diff] [blame] | 15 | import java.util.ArrayList; |
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; |
| 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; |
22301102 | fe5f841 | 2025-06-01 17:25:51 +0800 | [diff] [blame] | 28 | private DownloadService downloadService; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 29 | |
| 30 | @GetMapping("/list/all") |
| 31 | public ResponseEntity<?> getAllResources(@RequestHeader("token") String token, |
| 32 | @RequestParam("username") String username) { |
| 33 | Map<String, Object> ans = new HashMap<>(); |
| 34 | |
| 35 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
| 36 | ans.put("result", "Invalid token"); |
| 37 | return ResponseEntity.badRequest().body(ans); |
| 38 | } |
| 39 | |
| 40 | List<Resource> resources = resourceService.getAllResources(); |
| 41 | if (resources.isEmpty()) { |
| 42 | return ResponseEntity.noContent().build(); |
| 43 | } |
22301102 | fe5f841 | 2025-06-01 17:25:51 +0800 | [diff] [blame] | 44 | |
| 45 | List<ResourceInfo> resourceInfos = new ArrayList<>(); |
| 46 | for (Resource resource : resources) { |
| 47 | ResourceInfo resourceInfo = new ResourceInfo(); |
| 48 | resourceInfo.setResourceId(resource.getResourceId()); |
| 49 | resourceInfo.setName(resource.getName()); |
| 50 | resourceInfo.setSize(resource.getSize()); |
| 51 | resourceInfo.setDescription(resource.getDescription()); |
| 52 | resourceInfo.setAuthor(resource.getAuthor()); |
| 53 | resourceInfo.setPublishTime(resource.getPublishTime()); |
| 54 | |
| 55 | int downloadCount = downloadService.searchByResourceId(resource.getResourceId()).size(); |
| 56 | int seedCount = 0; // TODO: 需要根据tracker信息计算该资源的种子树 |
| 57 | resourceInfo.setDownloadCount(downloadCount); |
| 58 | resourceInfo.setSeedCount(seedCount); |
| 59 | resourceInfos.add(resourceInfo); |
| 60 | } |
| 61 | |
| 62 | ans.put("message", "Resources retrieved successfully"); |
| 63 | ans.put("resources", resourceInfos); |
| 64 | return ResponseEntity.ok(ans); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @GetMapping("/list/user") |
| 68 | public ResponseEntity<?> getUserResources(@RequestHeader("token") String token, |
| 69 | @RequestParam("username") String username) { |
| 70 | Map<String, Object> ans = new HashMap<>(); |
| 71 | |
| 72 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
| 73 | ans.put("result", "Invalid token"); |
| 74 | return ResponseEntity.badRequest().body(ans); |
| 75 | } |
| 76 | |
| 77 | List<Resource> resources = resourceService.getResourcesByAuthor(username); |
| 78 | if (resources.isEmpty()) { |
| 79 | return ResponseEntity.noContent().build(); |
| 80 | } |
| 81 | return ResponseEntity.ok(resources); |
| 82 | } |
| 83 | |
| 84 | @PostMapping("/publish") |
| 85 | public ResponseEntity<?> publishResource(@RequestHeader("token") String token, |
| 86 | @RequestParam("username") String username, |
| 87 | @RequestParam("size") double size, |
| 88 | @RequestParam("name") String name, |
| 89 | @RequestParam("description") String description) { |
| 90 | Map<String, Object> ans = new HashMap<>(); |
| 91 | |
| 92 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
| 93 | ans.put("result", "Invalid token"); |
| 94 | return ResponseEntity.badRequest().body(ans); |
| 95 | } |
| 96 | |
| 97 | User user = userService.findByUsername(username); |
| 98 | if(user.getLevel() < 2){ |
| 99 | ans.put("result", "Insufficient permissions to publish resources"); |
| 100 | return ResponseEntity.status(403).body(ans); |
| 101 | } |
| 102 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 103 | /* |
| 104 | * TODO: 在这里实现资源发布的逻辑 |
22301102 | fe5f841 | 2025-06-01 17:25:51 +0800 | [diff] [blame] | 105 | * 需要将资源同步到Tracker服务器 |
| 106 | * 种子文件需要保存 |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 107 | */ |
| 108 | |
22301102 | fe5f841 | 2025-06-01 17:25:51 +0800 | [diff] [blame] | 109 | resourceService.publishResource(name, description, username, size); |
| 110 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 111 | ans.put("result", "Resource published successfully"); |
| 112 | return ResponseEntity.ok(ans); |
| 113 | } |
| 114 | |
| 115 | @GetMapping("/get/{resourceId}") |
| 116 | public ResponseEntity<?> getResourceById(@PathVariable("resourceId") int resourceId, |
| 117 | @RequestHeader("token") String token, |
| 118 | @RequestParam("username") String username) { |
| 119 | |
| 120 | Map<String, Object> ans = new HashMap<>(); |
| 121 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) { |
| 122 | ans.put("result", "Invalid token"); |
| 123 | return ResponseEntity.badRequest().body(ans); |
| 124 | } |
| 125 | |
| 126 | Resource resource = resourceService.getResourceById(resourceId); |
| 127 | if (resource == null) { |
| 128 | return ResponseEntity.notFound().build(); |
| 129 | } |
| 130 | return ResponseEntity.ok(resource); |
| 131 | } |
| 132 | |
| 133 | @GetMapping("/download/{resourceId}") |
| 134 | public ResponseEntity<?> downloadResource(@PathVariable("resourceId") int resourceId, |
| 135 | @RequestHeader("token") String token, |
| 136 | @RequestParam("username") String username) { |
| 137 | |
| 138 | Map<String, Object> ans = new HashMap<>(); |
| 139 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) { |
| 140 | ans.put("result", "Invalid token"); |
| 141 | return ResponseEntity.badRequest().body(ans); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * TODO: 在这里实现下载资源的方法 |
22301102 | fe5f841 | 2025-06-01 17:25:51 +0800 | [diff] [blame] | 146 | * 从本地的种子文件目录获取种子文件 |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 147 | */ |
| 148 | |
| 149 | // Here you would typically return the file or a download link |
| 150 | return ResponseEntity.ok(ans); |
| 151 | } |
| 152 | |
| 153 | @GetMapping("/search") |
| 154 | public ResponseEntity<?> searchResources(@RequestHeader("token") String token, |
| 155 | @RequestParam("username") String username, |
| 156 | @RequestParam("query") String query) { |
| 157 | Map<String, Object> ans = new HashMap<>(); |
| 158 | |
| 159 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
| 160 | ans.put("result", "Invalid token"); |
| 161 | return ResponseEntity.badRequest().body(ans); |
| 162 | } |
| 163 | |
| 164 | List<Resource> resources = resourceService.searchByQuery(query); |
| 165 | if (resources.isEmpty()) { |
| 166 | return ResponseEntity.noContent().build(); |
| 167 | } |
| 168 | return ResponseEntity.ok(resources); |
| 169 | } |
| 170 | |
| 171 | @GetMapping("/delete") |
| 172 | public ResponseEntity<?> deleteResource(@RequestHeader("token") String token, |
| 173 | @RequestParam("username") String username, |
| 174 | @RequestParam("resourceId") int resourceId) { |
| 175 | Map<String, Object> ans = new HashMap<>(); |
| 176 | Resource resource = resourceService.getResourceById(resourceId); |
| 177 | |
| 178 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN) || resource == null || !resource.getAuthor().equals(username)) { |
| 179 | ans.put("result", "Invalid token"); |
| 180 | return ResponseEntity.badRequest().body(ans); |
| 181 | } |
| 182 | |
| 183 | /* |
22301102 | fe5f841 | 2025-06-01 17:25:51 +0800 | [diff] [blame] | 184 | * TODO: 在这里实现删除资源的方法 |
| 185 | * 需要同步到Tracker服务器 |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 186 | */ |
| 187 | |
| 188 | |
| 189 | resourceService.deleteResource(resourceId); |
| 190 | |
| 191 | ans.put("result", "Resource deleted successfully"); |
| 192 | return ResponseEntity.ok(ans); |
| 193 | } |
| 194 | } |