blob: 3d06d856f8b7a4fc150162014b8283af6cfbf2af [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
Edwardsamaxl25305242025-06-09 21:17:29 +080044 public void publishResource(String name, String title, 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());
Edwardsamaxl25305242025-06-09 21:17:29 +080054 resource.setTitle(title);
22301102bc6da0a2025-06-02 17:47:29 +080055
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080056 resource.setTorrentData(torrentBytes);
57 // 这里可以保存torrent文件路径,或直接存数据库,依据你的设计
22301102b1084372025-06-01 16:44:23 +080058 resourceRepository.save(resource);
Edwardsamaxlcba512d2025-06-09 21:17:29 +080059
60 // 在Tracker中注册相关信息
61// Map<String, String[]> params = new HashMap<>();
62// params.put("info_hash", new String[]{meta.getInfoHash()});
63// // 这里peer_id和port可以先使用默认值,实际应用中可以根据需求修改
64// params.put("peer_id", new String[]{username});
65// params.put("port", new String[]{"6881"});
66// trackerService.handleAnnounce(params, ip);
22301102b1084372025-06-01 16:44:23 +080067 }
68
22301102bc6da0a2025-06-02 17:47:29 +080069 // 获取资源时,返回BLOB字段内容作为torrent文件
70 public byte[] getTorrentFileByResource(Resource resource, String username) {
71 if(resource == null || resource.getTorrentData() == null) return null;
72
73 // 记录下载日志
74 Download download = new Download();
75 download.setResourceId(String.valueOf(resource.getResourceId()));
76 download.setDownloader(username);
77 download.setDownloadTime(LocalDateTime.now());
78 downloadRepository.save(download);
79
80 return resource.getTorrentData();
22301102b1084372025-06-01 16:44:23 +080081 }
82
22301102bc6da0a2025-06-02 17:47:29 +080083 public Resource getResourceById(int id) {
84 return resourceRepository.findById(id).orElse(null);
85 }
86
87 public void deleteResource(int id) {
88 Resource resource = getResourceById(id);
89 if (resource != null) {
90 // 删除数据库资源记录
91 resourceRepository.deleteById(id);
92
93 // 删除对应的 TorrentMeta 元信息
94 TorrentMeta meta = torrentMetaRepository.findByFilename(resource.getName());
95 if (meta != null) {
96 torrentMetaRepository.delete(meta);
97 }
98
99 }
100 }
22301102b1084372025-06-01 16:44:23 +0800101 public List<Resource> searchByQuery(String query) {
102 return resourceRepository.findByNameContainingIgnoreCase(query);
103 }
104
Edwardsamaxlcba512d2025-06-09 21:17:29 +0800105}