22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 1 | package com.pt.service; |
| 2 | |
| 3 | import com.pt.entity.TorrentMeta; |
| 4 | import com.pt.repository.TorrentMetaRepository; |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 5 | import com.pt.utils.BencodeCodec; |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 6 | import org.springframework.beans.factory.annotation.Autowired; |
| 7 | import org.springframework.stereotype.Service; |
| 8 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 9 | import java.security.MessageDigest; |
| 10 | import java.time.LocalDateTime; |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 11 | import java.util.HashMap; |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 12 | import java.util.List; |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 13 | import java.util.Map; |
22301102 | ca0fb2f | 2025-06-09 18:40:42 +0800 | [diff] [blame] | 14 | import java.util.Optional; |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 15 | |
| 16 | @Service |
| 17 | public class TorrentService { |
| 18 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 19 | @Autowired |
| 20 | private TorrentMetaRepository torrentMetaRepository; |
| 21 | |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 22 | public TorrentMeta parseAndSaveTorrent(byte[] torrentBytes) throws Exception { |
| 23 | Map<String, Object> torrentDict = (Map<String, Object>) BencodeCodec.decode(torrentBytes); |
| 24 | if (!torrentDict.containsKey("info")) { |
| 25 | throw new IllegalArgumentException("Invalid torrent file: missing 'info' dictionary"); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 26 | } |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 27 | |
| 28 | Map<String, Object> infoDict = (Map<String, Object>) torrentDict.get("info"); |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 29 | // 计算 info_hash |
| 30 | byte[] infoEncoded = BencodeCodec.encode(infoDict); |
| 31 | MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); |
| 32 | byte[] infoHashBytes = sha1.digest(infoEncoded); |
| 33 | String infoHash = bytesToHex(infoHashBytes); |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 34 | // 获取文件名,大小等 |
| 35 | String name = (String) infoDict.get("name"); |
| 36 | long length = 0; |
| 37 | if (infoDict.containsKey("length")) { |
| 38 | length = ((Number) infoDict.get("length")).longValue(); |
| 39 | } else if (infoDict.containsKey("files")) { |
| 40 | long totalLength = 0; |
| 41 | List<Map<String, Object>> files = (List<Map<String, Object>>) infoDict.get("files"); |
| 42 | for (Map<String, Object> file : files) { |
| 43 | totalLength += ((Number) file.get("length")).longValue(); |
| 44 | } |
| 45 | length = totalLength; |
| 46 | } |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 47 | // 添加Tracker地址到种子文件中 |
| 48 | torrentDict.put("announce", "http://your-tracker-url/api/tracker/announce"); |
| 49 | |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 50 | // 保存到数据库 |
22301102 | ca0fb2f | 2025-06-09 18:40:42 +0800 | [diff] [blame] | 51 | Optional<TorrentMeta> existing = torrentMetaRepository.findByInfoHash(infoHash); |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 52 | |
22301102 | ca0fb2f | 2025-06-09 18:40:42 +0800 | [diff] [blame] | 53 | if (existing.isPresent()) { |
| 54 | System.out.println("该种子已存在,跳过保存"); |
| 55 | return existing.get(); |
| 56 | } else { |
| 57 | TorrentMeta meta = new TorrentMeta(); |
| 58 | meta.setFilename(name); |
| 59 | meta.setInfoHash(infoHash); |
| 60 | meta.setSize(length); |
| 61 | meta.setUploadTime(LocalDateTime.now()); |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 62 | meta.setTorrentData(BencodeCodec.encode(torrentDict)); // 使用更新后的字典编码 |
22301102 | ca0fb2f | 2025-06-09 18:40:42 +0800 | [diff] [blame] | 63 | |
| 64 | return torrentMetaRepository.save(meta); |
| 65 | } |
| 66 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 67 | } |
| 68 | |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 69 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 70 | private String bytesToHex(byte[] bytes) { |
| 71 | StringBuilder sb = new StringBuilder(); |
| 72 | for (byte b : bytes) { |
| 73 | sb.append(String.format("%02x", b)); |
| 74 | } |
| 75 | return sb.toString(); |
| 76 | } |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 77 | } |