22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 1 | package com.pt.service; |
| 2 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 3 | import com.pt.entity.Download; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 4 | import com.pt.entity.Resource; |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 5 | import com.pt.entity.TorrentMeta; |
| 6 | import com.pt.repository.DownloadRepository; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 7 | import com.pt.repository.ResourceRepository; |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 8 | import com.pt.repository.TorrentMetaRepository; |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 9 | import com.pt.service.TrackerService; // 新增导入 |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 10 | import org.springframework.beans.factory.annotation.Autowired; |
| 11 | import org.springframework.stereotype.Service; |
| 12 | |
| 13 | import java.time.LocalDateTime; |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 14 | import java.util.HashMap; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 15 | import java.util.List; |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 16 | import java.util.Map; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 17 | |
| 18 | @Service |
| 19 | public class ResourceService { |
| 20 | |
| 21 | @Autowired |
| 22 | private ResourceRepository resourceRepository; |
| 23 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 24 | @Autowired |
| 25 | private DownloadRepository downloadRepository; |
| 26 | |
| 27 | @Autowired |
| 28 | private TorrentService torrentService; |
| 29 | |
| 30 | @Autowired |
| 31 | private TorrentMetaRepository torrentMetaRepository; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 32 | |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 33 | @Autowired |
| 34 | private TrackerService trackerService; // 新增注入 |
| 35 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 36 | public List<Resource> getAllResources() { |
| 37 | return resourceRepository.findAll(); |
| 38 | } |
| 39 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 40 | public List<Resource> getResourcesByAuthor(String username) { |
| 41 | return resourceRepository.findByAuthor(username); |
| 42 | } |
| 43 | |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 44 | public void publishResource(String name, String description, String author, byte[] torrentBytes, String username) throws Exception { |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 45 | // 解析并保存torrent元信息 |
| 46 | TorrentMeta meta = torrentService.parseAndSaveTorrent(torrentBytes); |
| 47 | |
| 48 | // 保存资源信息,并关联torrent信息 |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 49 | Resource resource = new Resource(); |
| 50 | resource.setName(name); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 51 | resource.setDescription(description); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 52 | resource.setAuthor(author); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 53 | resource.setPublishTime(LocalDateTime.now()); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 54 | |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 55 | resource.setTorrentData(torrentBytes); |
| 56 | // 这里可以保存torrent文件路径,或直接存数据库,依据你的设计 |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 57 | resourceRepository.save(resource); |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 58 | |
| 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); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 66 | } |
| 67 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 68 | // 获取资源时,返回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(); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 80 | } |
| 81 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 82 | 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 | } |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 100 | public List<Resource> searchByQuery(String query) { |
| 101 | return resourceRepository.findByNameContainingIgnoreCase(query); |
| 102 | } |
| 103 | |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 104 | } |