| package com.pt.controller; |
| |
| import com.pt.Item.ResourceInfo; |
| import com.pt.constant.Constants; |
| import com.pt.entity.Resource; |
| import com.pt.entity.User; |
| import com.pt.service.DownloadService; |
| import com.pt.service.ResourceService; |
| import com.pt.service.UserService; |
| import com.pt.utils.JWTUtils; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.http.ResponseEntity; |
| import org.springframework.web.bind.annotation.*; |
| |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| @RestController |
| @RequestMapping("/api/resource") |
| @CrossOrigin(origins = "*") |
| public class ResourceController { |
| |
| @Autowired |
| private ResourceService resourceService; |
| private UserService userService; |
| private DownloadService downloadService; |
| |
| @GetMapping("/list/all") |
| public ResponseEntity<?> getAllResources(@RequestHeader("token") String token, |
| @RequestParam("username") String username) { |
| Map<String, Object> ans = new HashMap<>(); |
| |
| if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
| ans.put("result", "Invalid token"); |
| return ResponseEntity.badRequest().body(ans); |
| } |
| |
| List<Resource> resources = resourceService.getAllResources(); |
| if (resources.isEmpty()) { |
| return ResponseEntity.noContent().build(); |
| } |
| |
| List<ResourceInfo> resourceInfos = new ArrayList<>(); |
| for (Resource resource : resources) { |
| ResourceInfo resourceInfo = new ResourceInfo(); |
| resourceInfo.setResourceId(resource.getResourceId()); |
| resourceInfo.setName(resource.getName()); |
| resourceInfo.setSize(resource.getSize()); |
| resourceInfo.setDescription(resource.getDescription()); |
| resourceInfo.setAuthor(resource.getAuthor()); |
| resourceInfo.setPublishTime(resource.getPublishTime()); |
| |
| int downloadCount = downloadService.searchByResourceId(resource.getResourceId()).size(); |
| int seedCount = 0; // TODO: 需要根据tracker信息计算该资源的种子树 |
| resourceInfo.setDownloadCount(downloadCount); |
| resourceInfo.setSeedCount(seedCount); |
| resourceInfos.add(resourceInfo); |
| } |
| |
| ans.put("message", "Resources retrieved successfully"); |
| ans.put("resources", resourceInfos); |
| return ResponseEntity.ok(ans); |
| } |
| |
| @GetMapping("/list/user") |
| public ResponseEntity<?> getUserResources(@RequestHeader("token") String token, |
| @RequestParam("username") String username) { |
| Map<String, Object> ans = new HashMap<>(); |
| |
| if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
| ans.put("result", "Invalid token"); |
| return ResponseEntity.badRequest().body(ans); |
| } |
| |
| List<Resource> resources = resourceService.getResourcesByAuthor(username); |
| if (resources.isEmpty()) { |
| return ResponseEntity.noContent().build(); |
| } |
| return ResponseEntity.ok(resources); |
| } |
| |
| @PostMapping("/publish") |
| public ResponseEntity<?> publishResource(@RequestHeader("token") String token, |
| @RequestParam("username") String username, |
| @RequestParam("size") double size, |
| @RequestParam("name") String name, |
| @RequestParam("description") String description) { |
| Map<String, Object> ans = new HashMap<>(); |
| |
| if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
| ans.put("result", "Invalid token"); |
| return ResponseEntity.badRequest().body(ans); |
| } |
| |
| User user = userService.findByUsername(username); |
| if(user.getLevel() < 2){ |
| ans.put("result", "Insufficient permissions to publish resources"); |
| return ResponseEntity.status(403).body(ans); |
| } |
| |
| /* |
| * TODO: 在这里实现资源发布的逻辑 |
| * 需要将资源同步到Tracker服务器 |
| * 种子文件需要保存 |
| */ |
| |
| resourceService.publishResource(name, description, username, size); |
| |
| ans.put("result", "Resource published successfully"); |
| return ResponseEntity.ok(ans); |
| } |
| |
| @GetMapping("/get/{resourceId}") |
| public ResponseEntity<?> getResourceById(@PathVariable("resourceId") int resourceId, |
| @RequestHeader("token") String token, |
| @RequestParam("username") String username) { |
| |
| Map<String, Object> ans = new HashMap<>(); |
| if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) { |
| ans.put("result", "Invalid token"); |
| return ResponseEntity.badRequest().body(ans); |
| } |
| |
| Resource resource = resourceService.getResourceById(resourceId); |
| if (resource == null) { |
| return ResponseEntity.notFound().build(); |
| } |
| return ResponseEntity.ok(resource); |
| } |
| |
| @GetMapping("/download/{resourceId}") |
| public ResponseEntity<?> downloadResource(@PathVariable("resourceId") int resourceId, |
| @RequestHeader("token") String token, |
| @RequestParam("username") String username) { |
| |
| Map<String, Object> ans = new HashMap<>(); |
| if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) { |
| ans.put("result", "Invalid token"); |
| return ResponseEntity.badRequest().body(ans); |
| } |
| |
| /* |
| * TODO: 在这里实现下载资源的方法 |
| * 从本地的种子文件目录获取种子文件 |
| */ |
| |
| // Here you would typically return the file or a download link |
| return ResponseEntity.ok(ans); |
| } |
| |
| @GetMapping("/search") |
| public ResponseEntity<?> searchResources(@RequestHeader("token") String token, |
| @RequestParam("username") String username, |
| @RequestParam("query") String query) { |
| Map<String, Object> ans = new HashMap<>(); |
| |
| if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
| ans.put("result", "Invalid token"); |
| return ResponseEntity.badRequest().body(ans); |
| } |
| |
| List<Resource> resources = resourceService.searchByQuery(query); |
| if (resources.isEmpty()) { |
| return ResponseEntity.noContent().build(); |
| } |
| return ResponseEntity.ok(resources); |
| } |
| |
| @GetMapping("/delete") |
| public ResponseEntity<?> deleteResource(@RequestHeader("token") String token, |
| @RequestParam("username") String username, |
| @RequestParam("resourceId") int resourceId) { |
| Map<String, Object> ans = new HashMap<>(); |
| Resource resource = resourceService.getResourceById(resourceId); |
| |
| if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN) || resource == null || !resource.getAuthor().equals(username)) { |
| ans.put("result", "Invalid token"); |
| return ResponseEntity.badRequest().body(ans); |
| } |
| |
| /* |
| * TODO: 在这里实现删除资源的方法 |
| * 需要同步到Tracker服务器 |
| */ |
| |
| |
| resourceService.deleteResource(resourceId); |
| |
| ans.put("result", "Resource deleted successfully"); |
| return ResponseEntity.ok(ans); |
| } |
| } |