blob: 8e08e16f3ab70529d467f84a6913e8ad25c91087 [file] [log] [blame]
22301102b1084372025-06-01 16:44:23 +08001package com.pt.controller;
2
3import com.pt.constant.Constants;
4import com.pt.entity.Resource;
5import com.pt.entity.User;
6import com.pt.service.ResourceService;
7import com.pt.service.UserService;
8import com.pt.utils.JWTUtils;
9import org.springframework.beans.factory.annotation.Autowired;
10import org.springframework.http.ResponseEntity;
11import org.springframework.web.bind.annotation.*;
12
22301102bc6da0a2025-06-02 17:47:29 +080013import java.io.IOException;
22301102b1084372025-06-01 16:44:23 +080014import java.util.HashMap;
15import java.util.List;
16import java.util.Map;
22301102bc6da0a2025-06-02 17:47:29 +080017import java.io.File;
22301102b1084372025-06-01 16:44:23 +080018
19@RestController
20@RequestMapping("/api/resource")
21@CrossOrigin(origins = "*")
22public 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 }
22301102bc6da0a2025-06-02 17:47:29 +080042 return ResponseEntity.ok(resources);
22301102b1084372025-06-01 16:44:23 +080043 }
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
22301102bc6da0a2025-06-02 17:47:29 +080070 if (!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
22301102b1084372025-06-01 16:44:23 +080071 ans.put("result", "Invalid token");
72 return ResponseEntity.badRequest().body(ans);
73 }
74
75 User user = userService.findByUsername(username);
22301102bc6da0a2025-06-02 17:47:29 +080076 if (user == null || user.getLevel() < 2) {
22301102b1084372025-06-01 16:44:23 +080077 ans.put("result", "Insufficient permissions to publish resources");
78 return ResponseEntity.status(403).body(ans);
79 }
80
22301102bc6da0a2025-06-02 17:47:29 +080081 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 }
22301102fe5f8412025-06-01 17:25:51 +080087
22301102b1084372025-06-01 16:44:23 +080088 ans.put("result", "Resource published successfully");
89 return ResponseEntity.ok(ans);
90 }
91
22301102bc6da0a2025-06-02 17:47:29 +080092
93
22301102b1084372025-06-01 16:44:23 +080094 @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,
22301102bc6da0a2025-06-02 17:47:29 +0800115 @RequestParam("username") String username) throws IOException {
22301102b1084372025-06-01 16:44:23 +0800116
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
22301102bc6da0a2025-06-02 17:47:29 +0800123 Resource resource = resourceService.getResourceById(resourceId);
124 byte[] file = resourceService.getTorrentFileByResource(resource, username);
125 if (file == null) {
126 return ResponseEntity.notFound().build();
127 }
22301102b1084372025-06-01 16:44:23 +0800128
22301102bc6da0a2025-06-02 17:47:29 +0800129 return ResponseEntity.ok()
130 .header("Content-Type", "application/x-bittorrent")
131 .header("Content-Disposition", "attachment; filename=\"" + resource.getName() + ".torrent\"")
132 .body(file);
22301102b1084372025-06-01 16:44:23 +0800133 }
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)) {
22301102bc6da0a2025-06-02 17:47:29 +0800161 ans.put("result", "Invalid token or insufficient permissions");
22301102b1084372025-06-01 16:44:23 +0800162 return ResponseEntity.badRequest().body(ans);
163 }
164
22301102bc6da0a2025-06-02 17:47:29 +0800165 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 }
22301102b1084372025-06-01 16:44:23 +0800171
172 ans.put("result", "Resource deleted successfully");
173 return ResponseEntity.ok(ans);
174 }
22301102bc6da0a2025-06-02 17:47:29 +0800175
22301102b1084372025-06-01 16:44:23 +0800176}