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 | 2530524 | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 44 | public void publishResource(String name, String title, 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()); |
Edwardsamaxl | 2530524 | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 54 | resource.setTitle(title); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 55 | |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 56 | resource.setTorrentData(torrentBytes); |
| 57 | // 这里可以保存torrent文件路径,或直接存数据库,依据你的设计 |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 58 | resourceRepository.save(resource); |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame] | 59 | |
| 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); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 67 | } |
| 68 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 69 | // 获取资源时,返回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(); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 81 | } |
| 82 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 83 | 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 | } |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 101 | public List<Resource> searchByQuery(String query) { |
| 102 | return resourceRepository.findByNameContainingIgnoreCase(query); |
| 103 | } |
| 104 | |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame] | 105 | } |