blob: 4eb782335eff93521702439fcdef18ed2a95c89d [file] [log] [blame]
22301102b1084372025-06-01 16:44:23 +08001package com.pt.service;
2
22301102bc6da0a2025-06-02 17:47:29 +08003import com.pt.entity.Download;
22301102b1084372025-06-01 16:44:23 +08004import com.pt.entity.Resource;
22301102bc6da0a2025-06-02 17:47:29 +08005import com.pt.entity.TorrentMeta;
6import com.pt.repository.DownloadRepository;
22301102b1084372025-06-01 16:44:23 +08007import com.pt.repository.ResourceRepository;
22301102bc6da0a2025-06-02 17:47:29 +08008import com.pt.repository.TorrentMetaRepository;
Edwardsamaxlcba512d2025-06-09 21:17:29 +08009import com.pt.service.TrackerService; // 新增导入
22301102b1084372025-06-01 16:44:23 +080010import org.springframework.beans.factory.annotation.Autowired;
11import org.springframework.stereotype.Service;
12
13import java.time.LocalDateTime;
Edwardsamaxlcba512d2025-06-09 21:17:29 +080014import java.util.HashMap;
22301102b1084372025-06-01 16:44:23 +080015import java.util.List;
Edwardsamaxlcba512d2025-06-09 21:17:29 +080016import java.util.Map;
22301102b1084372025-06-01 16:44:23 +080017
18@Service
19public class ResourceService {
20
21 @Autowired
22 private ResourceRepository resourceRepository;
23
22301102bc6da0a2025-06-02 17:47:29 +080024 @Autowired
25 private DownloadRepository downloadRepository;
26
27 @Autowired
28 private TorrentService torrentService;
29
30 @Autowired
31 private TorrentMetaRepository torrentMetaRepository;
22301102b1084372025-06-01 16:44:23 +080032
Edwardsamaxlcba512d2025-06-09 21:17:29 +080033 @Autowired
34 private TrackerService trackerService; // 新增注入
35
22301102b1084372025-06-01 16:44:23 +080036 public List<Resource> getAllResources() {
37 return resourceRepository.findAll();
38 }
39
22301102bc6da0a2025-06-02 17:47:29 +080040 public List<Resource> getResourcesByAuthor(String username) {
41 return resourceRepository.findByAuthor(username);
42 }
43
Edwardsamaxlcba512d2025-06-09 21:17:29 +080044 public void publishResource(String name, String description, String author, byte[] torrentBytes, String username) throws Exception {
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080045 // 解析并保存torrent元信息
46 TorrentMeta meta = torrentService.parseAndSaveTorrent(torrentBytes);
47
48 // 保存资源信息,并关联torrent信息
22301102b1084372025-06-01 16:44:23 +080049 Resource resource = new Resource();
50 resource.setName(name);
22301102b1084372025-06-01 16:44:23 +080051 resource.setDescription(description);
22301102bc6da0a2025-06-02 17:47:29 +080052 resource.setAuthor(author);
22301102b1084372025-06-01 16:44:23 +080053 resource.setPublishTime(LocalDateTime.now());
22301102bc6da0a2025-06-02 17:47:29 +080054
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080055 resource.setTorrentData(torrentBytes);
56 // 这里可以保存torrent文件路径,或直接存数据库,依据你的设计
22301102b1084372025-06-01 16:44:23 +080057 resourceRepository.save(resource);
Edwardsamaxlcba512d2025-06-09 21:17:29 +080058
59 // 在Tracker中注册相关信息
60// Map<String, String[]> params = new HashMap<>();
61// params.put("info_hash", new String[]{meta.getInfoHash()});
62// // 这里peer_id和port可以先使用默认值,实际应用中可以根据需求修改
63// params.put("peer_id", new String[]{username});
64// params.put("port", new String[]{"6881"});
65// trackerService.handleAnnounce(params, ip);
22301102b1084372025-06-01 16:44:23 +080066 }
67
22301102bc6da0a2025-06-02 17:47:29 +080068 // 获取资源时,返回BLOB字段内容作为torrent文件
69 public byte[] getTorrentFileByResource(Resource resource, String username) {
70 if(resource == null || resource.getTorrentData() == null) return null;
71
72 // 记录下载日志
73 Download download = new Download();
74 download.setResourceId(String.valueOf(resource.getResourceId()));
75 download.setDownloader(username);
76 download.setDownloadTime(LocalDateTime.now());
77 downloadRepository.save(download);
78
79 return resource.getTorrentData();
22301102b1084372025-06-01 16:44:23 +080080 }
81
22301102bc6da0a2025-06-02 17:47:29 +080082 public Resource getResourceById(int id) {
83 return resourceRepository.findById(id).orElse(null);
84 }
85
86 public void deleteResource(int id) {
87 Resource resource = getResourceById(id);
88 if (resource != null) {
89 // 删除数据库资源记录
90 resourceRepository.deleteById(id);
91
92 // 删除对应的 TorrentMeta 元信息
93 TorrentMeta meta = torrentMetaRepository.findByFilename(resource.getName());
94 if (meta != null) {
95 torrentMetaRepository.delete(meta);
96 }
97
98 }
99 }
22301102b1084372025-06-01 16:44:23 +0800100 public List<Resource> searchByQuery(String query) {
101 return resourceRepository.findByNameContainingIgnoreCase(query);
102 }
103
Edwardsamaxlcba512d2025-06-09 21:17:29 +0800104}