add torrent upload(finished) and tracker announce(unimplemnt)
Change-Id: I017c03df2bc1c40c4a080837821d56dfe58d6eb6
diff --git a/src/main/java/com/example/g8backend/controller/AuthController.java b/src/main/java/com/example/g8backend/controller/AuthController.java
index 4b3be4b..2f36500 100644
--- a/src/main/java/com/example/g8backend/controller/AuthController.java
+++ b/src/main/java/com/example/g8backend/controller/AuthController.java
@@ -63,6 +63,9 @@
user.setUserName(registerDTO.getUserName());
user.setPassword(passwordEncoder.encode(registerDTO.getPassword()));
user.setEmail(registerDTO.getEmail());
+
+ // passkey 用于在客户端发送announce请求时获取用户信息
+ user.setPasskey(UUID.randomUUID().toString().replace("-", ""));
userService.registerUser(user);
return ResponseEntity.ok("注册成功");
diff --git a/src/main/java/com/example/g8backend/controller/TorrentController.java b/src/main/java/com/example/g8backend/controller/TorrentController.java
new file mode 100644
index 0000000..bec187e
--- /dev/null
+++ b/src/main/java/com/example/g8backend/controller/TorrentController.java
@@ -0,0 +1,46 @@
+package com.example.g8backend.controller;
+
+import com.example.g8backend.entity.User;
+import com.example.g8backend.service.IUserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import com.example.g8backend.service.ITorrentService;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.IOException;
+
+@RestController
+@RequestMapping("/torrent")
+public class TorrentController {
+ @Autowired
+ private ITorrentService torrentService;
+
+ @Autowired
+ private IUserService userService;
+
+ @RequestMapping("/upload")
+ public ResponseEntity<?> handleTorrentUpload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+ long userId = (long) authentication.getPrincipal();
+
+ User user = userService.getById(userId);
+ String passkey = user.getPasskey();
+
+ File tempFile = File.createTempFile("upload-", ".torrent");
+ multipartFile.transferTo(tempFile);
+
+ torrentService.handleTorrentUpload(tempFile, userId, passkey);
+
+ // 删除临时文件
+ if(!tempFile.delete()){
+ throw new IOException("Failed to delete temporary file: " + tempFile.getAbsolutePath());
+ }
+ return ResponseEntity.ok("种子上传成功");
+ }
+}
diff --git a/src/main/java/com/example/g8backend/controller/TrackerController.java b/src/main/java/com/example/g8backend/controller/TrackerController.java
index 7ee9e5a..d6cc957 100644
--- a/src/main/java/com/example/g8backend/controller/TrackerController.java
+++ b/src/main/java/com/example/g8backend/controller/TrackerController.java
@@ -1,15 +1,33 @@
package com.example.g8backend.controller;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
import com.example.g8backend.service.ITrackerService;
@RestController
-@RequestMapping("/announce")
+@RequestMapping("/tracker")
public class TrackerController {
+ @Autowired
+ private ITrackerService trackerService;
+
+ @Autowired
+ private RedisTemplate<String, Object> redisTemplate;
+
+ @GetMapping("/announce/{passkey}}")
+ public ResponseEntity<?> getAnnouncements(
+ @RequestParam(name = "info_hash") String infoHash,
+ @RequestParam(name = "peer_id") String peerId,
+ @RequestParam(name = "port") int port,
+ @RequestParam(name = "uploaded") long uploaded,
+ @RequestParam(name = "downloaded") long downloaded,
+ @RequestParam(name = "left") long left,
+ @RequestParam(name = "compact", required = false) int compact,
+ @RequestParam(name = "event", required = false) String event,
+ @PathVariable String passkey) {
+
+ return null;
+ }
}