Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame^] | 1 | package api; |
| 2 | |
| 3 | import java.io.File; |
| 4 | |
| 5 | import org.springframework.core.io.FileSystemResource; |
| 6 | import org.springframework.core.io.Resource; |
| 7 | import org.springframework.http.HttpHeaders; |
| 8 | import org.springframework.http.MediaType; |
| 9 | import org.springframework.http.ResponseEntity; |
| 10 | import org.springframework.web.bind.annotation.RequestParam; |
| 11 | import org.springframework.web.bind.annotation.RestController; |
| 12 | import org.springframework.web.multipart.MultipartFile; |
| 13 | |
| 14 | import database.Database1; |
| 15 | import entity.Seed; |
| 16 | import tracker.Tracker; |
| 17 | |
| 18 | @RestController |
| 19 | public class ApiController implements ApiInterface { |
| 20 | |
| 21 | private static Database1 db; |
| 22 | |
| 23 | private static Tracker tracker; |
| 24 | |
| 25 | @Override |
| 26 | public ResponseEntity<Integer> saveTorrent( |
| 27 | @RequestParam("userid") String userid, |
| 28 | @RequestParam("title") String title, |
| 29 | @RequestParam("tag") String tag, |
| 30 | @RequestParam("file") MultipartFile file |
| 31 | ) { |
| 32 | try { |
| 33 | Seed seed = new Seed(); |
| 34 | seed.seedid = "exampleSeedId"; // 示例种子ID |
| 35 | seed.seeduserid = userid; // 示例用户ID |
| 36 | seed.title = title; // 示例标题 |
| 37 | seed.seedsize = "1GB"; // 示例种子大小 |
| 38 | seed.seedtag = tag; // 示例标签 |
| 39 | seed.url = "http://example.com/torrent"; // 示例URL |
| 40 | db = new Database1(); |
| 41 | tracker = new Tracker(); |
| 42 | int ret = db.RegisterSeed(seed); |
| 43 | System.out.println("RegisterSeed ret: " + ret); |
| 44 | System.out.println(seed.seedid + " " + seed.seeduserid + " " + seed.title + " " + seed.seedsize + " " + seed.seedtag + " " + seed.url); |
| 45 | // Convert MultipartFile to File |
| 46 | File tempFile = File.createTempFile(seed.seedid, file.getOriginalFilename()); |
| 47 | file.transferTo(tempFile); |
| 48 | tracker.SaveTorrent(seed.seedid, tempFile); |
| 49 | // Optionally, delete the temp file after saving if not needed |
| 50 | // tempFile.delete(); |
| 51 | return ResponseEntity.ok(0); // 返回 0 表示成功 |
| 52 | } catch (Exception e) { |
| 53 | e.printStackTrace(); |
| 54 | return ResponseEntity.status(500).body(1); // 返回 1 表示失败 |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public ResponseEntity<Resource> getTorrent( |
| 60 | @RequestParam("seedid") String seedid, |
| 61 | @RequestParam("userid") String userid, |
| 62 | @RequestParam("ip") String ip |
| 63 | ) { |
| 64 | // 实现获取种子文件的逻辑 |
| 65 | // 示例:从本地目录中查找文件 |
| 66 | tracker = new Tracker(); |
| 67 | File file = tracker.GetTTorent(seedid, userid, ip); |
| 68 | if (file != null) { |
| 69 | FileSystemResource resource = new FileSystemResource(file); |
| 70 | return ResponseEntity.ok() |
| 71 | .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"") |
| 72 | .contentType(MediaType.APPLICATION_OCTET_STREAM) |
| 73 | .body(resource); |
| 74 | } else { |
| 75 | return ResponseEntity.notFound().build(); // 返回 404 表示文件未找到 |
| 76 | } |
| 77 | } |
| 78 | } |