blob: ad386223236894fbbf4002712c433c7e8a5aa01c [file] [log] [blame]
22301102bc6da0a2025-06-02 17:47:29 +08001package com.pt.service;
2
3import com.pt.entity.TorrentMeta;
4import com.pt.repository.TorrentMetaRepository;
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +08005import com.pt.utils.BencodeCodec;
22301102bc6da0a2025-06-02 17:47:29 +08006import org.springframework.beans.factory.annotation.Autowired;
7import org.springframework.stereotype.Service;
8
22301102bc6da0a2025-06-02 17:47:29 +08009import java.security.MessageDigest;
10import java.time.LocalDateTime;
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080011import java.util.List;
22301102bc6da0a2025-06-02 17:47:29 +080012import java.util.Map;
13
14@Service
15public class TorrentService {
16
22301102bc6da0a2025-06-02 17:47:29 +080017 @Autowired
18 private TorrentMetaRepository torrentMetaRepository;
19
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080020 public TorrentMeta parseAndSaveTorrent(byte[] torrentBytes) throws Exception {
22301102039b52a2025-06-08 14:10:02 +080021 System.out.println("111");
22 for (byte b : torrentBytes) {
23 System.out.println(b);
24 }
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080025 Map<String, Object> torrentDict = (Map<String, Object>) BencodeCodec.decode(torrentBytes);
26 if (!torrentDict.containsKey("info")) {
27 throw new IllegalArgumentException("Invalid torrent file: missing 'info' dictionary");
22301102bc6da0a2025-06-02 17:47:29 +080028 }
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080029
30 Map<String, Object> infoDict = (Map<String, Object>) torrentDict.get("info");
31
22301102039b52a2025-06-08 14:10:02 +080032 System.out.println(222);
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080033 // 计算 info_hash
34 byte[] infoEncoded = BencodeCodec.encode(infoDict);
35 MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
36 byte[] infoHashBytes = sha1.digest(infoEncoded);
37 String infoHash = bytesToHex(infoHashBytes);
38
22301102039b52a2025-06-08 14:10:02 +080039 System.out.println(333);
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080040 // 获取文件名,大小等
41 String name = (String) infoDict.get("name");
42 long length = 0;
43 if (infoDict.containsKey("length")) {
44 length = ((Number) infoDict.get("length")).longValue();
45 } else if (infoDict.containsKey("files")) {
46 long totalLength = 0;
47 List<Map<String, Object>> files = (List<Map<String, Object>>) infoDict.get("files");
48 for (Map<String, Object> file : files) {
49 totalLength += ((Number) file.get("length")).longValue();
50 }
51 length = totalLength;
52 }
53
54 // 保存到数据库
55 TorrentMeta meta = new TorrentMeta();
56 meta.setFilename(name);
57 meta.setInfoHash(infoHash);
58 meta.setSize(length);
59 meta.setUploadTime(LocalDateTime.now());
60 meta.setTorrentData(torrentBytes);
61
62 torrentMetaRepository.save(meta);
63 return meta;
22301102bc6da0a2025-06-02 17:47:29 +080064 }
65
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080066
22301102bc6da0a2025-06-02 17:47:29 +080067 private String bytesToHex(byte[] bytes) {
68 StringBuilder sb = new StringBuilder();
69 for (byte b : bytes) {
70 sb.append(String.format("%02x", b));
71 }
72 return sb.toString();
73 }
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080074}