修改cheat文件

Change-Id: I0ffce820f7ff78b6fe5b9a714baf17814636dc19

API修改

Change-Id: I63a5b9752afcfe6f8088d2cfc065b385ee2a4464

merge

Change-Id: I783bc0349b858b16bccd1ec21623182239170df2

修改cheat文件

Change-Id: I0ffce820f7ff78b6fe5b9a714baf17814636dc19

merge

Change-Id: I783bc0349b858b16bccd1ec21623182239170df2

修改cheat文件

Change-Id: I0ffce820f7ff78b6fe5b9a714baf17814636dc19

merge

Change-Id: I783bc0349b858b16bccd1ec21623182239170df2

合并API代码

Change-Id: I5ac299271c14d247413d78b5da51adc28977e05d
diff --git a/src/main/java/api/ApiApplication.java b/src/main/java/api/ApiApplication.java
new file mode 100644
index 0000000..93eb640
--- /dev/null
+++ b/src/main/java/api/ApiApplication.java
@@ -0,0 +1,11 @@
+package api;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class ApiApplication {
+    public static void main(String[] args) {
+        SpringApplication.run(ApiApplication.class, args);
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/api/ApiController.java b/src/main/java/api/ApiController.java
new file mode 100644
index 0000000..a683b0e
--- /dev/null
+++ b/src/main/java/api/ApiController.java
@@ -0,0 +1,78 @@
+package api;
+
+import java.io.File;
+
+import org.springframework.core.io.FileSystemResource;
+import org.springframework.core.io.Resource;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+import database.Database1;
+import entity.Seed;
+import tracker.Tracker;
+
+@RestController
+public class ApiController implements ApiInterface {
+
+    private static Database1 db;
+
+    private static Tracker tracker;
+
+    @Override
+    public ResponseEntity<Integer> saveTorrent(
+        @RequestParam("userid") String userid,
+        @RequestParam("title") String title,
+        @RequestParam("tag") String tag,
+        @RequestParam("file") MultipartFile file
+    ) {
+        try {
+            Seed seed = new Seed();
+            seed.seedid = "exampleSeedId"; // 示例种子ID
+            seed.seeduserid = userid; // 示例用户ID
+            seed.title = title; // 示例标题
+            seed.seedsize = "1GB"; // 示例种子大小
+            seed.seedtag = tag; // 示例标签
+            seed.url = "http://example.com/torrent"; // 示例URL
+            db = new Database1();
+            tracker = new Tracker();
+            int ret = db.RegisterSeed(seed);
+            System.out.println("RegisterSeed ret: " + ret);
+            System.out.println(seed.seedid + " " + seed.seeduserid + " " + seed.title + " " + seed.seedsize + " " + seed.seedtag + " " + seed.url);
+            // Convert MultipartFile to File
+            File tempFile = File.createTempFile(seed.seedid, file.getOriginalFilename());
+            file.transferTo(tempFile);
+            tracker.SaveTorrent(seed.seedid, tempFile);
+            // Optionally, delete the temp file after saving if not needed
+            // tempFile.delete();
+            return ResponseEntity.ok(0); // 返回 0 表示成功
+        } catch (Exception e) {
+            e.printStackTrace();
+            return ResponseEntity.status(500).body(1); // 返回 1 表示失败
+        }
+    }
+
+    @Override
+    public ResponseEntity<Resource> getTorrent(
+        @RequestParam("seedid") String seedid,
+        @RequestParam("userid") String userid,
+        @RequestParam("ip") String ip
+    ) {
+        // 实现获取种子文件的逻辑
+        // 示例:从本地目录中查找文件
+        tracker = new Tracker();
+        File file = tracker.GetTTorent(seedid, userid, ip);
+        if (file != null) {
+            FileSystemResource resource = new FileSystemResource(file);
+            return ResponseEntity.ok()
+                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"")
+                .contentType(MediaType.APPLICATION_OCTET_STREAM)
+                .body(resource);
+        } else {
+            return ResponseEntity.notFound().build(); // 返回 404 表示文件未找到
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/api/ApiInterface.java b/src/main/java/api/ApiInterface.java
index 495226d..ab3408c 100644
--- a/src/main/java/api/ApiInterface.java
+++ b/src/main/java/api/ApiInterface.java
@@ -1,5 +1,30 @@
 package api;
 
+import org.springframework.core.io.Resource;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+@RestController
+@RequestMapping("/api")
 public interface ApiInterface {
-    
+
+    @PostMapping("/save-torrent")
+    ResponseEntity<Integer> saveTorrent(
+        @RequestParam("userid") String userid,
+        @RequestParam("title") String title,
+        @RequestParam("tag") String tag,
+        @RequestParam("file") MultipartFile file
+    );
+
+    @GetMapping("/get-torrent")
+    ResponseEntity<Resource> getTorrent(
+        @RequestParam("seedid") String seedid,
+        @RequestParam("userid") String userid,
+        @RequestParam("ip") String ip
+    );
 }
\ No newline at end of file