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 | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 11 | import java.util.List; |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 12 | import java.util.Map; |
| 13 | |
| 14 | @Service |
| 15 | public class TorrentService { |
| 16 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 17 | @Autowired |
| 18 | private TorrentMetaRepository torrentMetaRepository; |
| 19 | |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 20 | public TorrentMeta parseAndSaveTorrent(byte[] torrentBytes) throws Exception { |
22301102 | 039b52a | 2025-06-08 14:10:02 +0800 | [diff] [blame^] | 21 | System.out.println("111"); |
| 22 | for (byte b : torrentBytes) { |
| 23 | System.out.println(b); |
| 24 | } |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 25 | 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"); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 28 | } |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 29 | |
| 30 | Map<String, Object> infoDict = (Map<String, Object>) torrentDict.get("info"); |
| 31 | |
22301102 | 039b52a | 2025-06-08 14:10:02 +0800 | [diff] [blame^] | 32 | System.out.println(222); |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 33 | // 计算 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 | |
22301102 | 039b52a | 2025-06-08 14:10:02 +0800 | [diff] [blame^] | 39 | System.out.println(333); |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 40 | // 获取文件名,大小等 |
| 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; |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 64 | } |
| 65 | |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 66 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 67 | 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 | } |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 74 | } |