| 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 表示文件未找到 |
| } |
| } |
| } |