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; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 9 | import org.springframework.beans.factory.annotation.Autowired; |
| 10 | import org.springframework.stereotype.Service; |
| 11 | |
| 12 | import java.time.LocalDateTime; |
| 13 | import java.util.List; |
| 14 | |
| 15 | @Service |
| 16 | public class ResourceService { |
| 17 | |
| 18 | @Autowired |
| 19 | private ResourceRepository resourceRepository; |
| 20 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 21 | @Autowired |
| 22 | private DownloadRepository downloadRepository; |
| 23 | |
| 24 | @Autowired |
| 25 | private TorrentService torrentService; |
| 26 | |
| 27 | @Autowired |
| 28 | private TorrentMetaRepository torrentMetaRepository; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 29 | |
| 30 | public List<Resource> getAllResources() { |
| 31 | return resourceRepository.findAll(); |
| 32 | } |
| 33 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 34 | public List<Resource> getResourcesByAuthor(String username) { |
| 35 | return resourceRepository.findByAuthor(username); |
| 36 | } |
| 37 | |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 38 | public void publishResource(String name, String description, String author, double size, byte[] torrentBytes) throws Exception { |
| 39 | // 解析并保存torrent元信息 |
| 40 | TorrentMeta meta = torrentService.parseAndSaveTorrent(torrentBytes); |
| 41 | |
| 42 | // 保存资源信息,并关联torrent信息 |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 43 | Resource resource = new Resource(); |
| 44 | resource.setName(name); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 45 | resource.setDescription(description); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 46 | resource.setAuthor(author); |
| 47 | resource.setSize(size); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 48 | resource.setPublishTime(LocalDateTime.now()); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 49 | |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 50 | resource.setTorrentData(torrentBytes); |
| 51 | // 这里可以保存torrent文件路径,或直接存数据库,依据你的设计 |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 52 | resourceRepository.save(resource); |
| 53 | } |
| 54 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame] | 55 | // 获取资源时,返回BLOB字段内容作为torrent文件 |
| 56 | public byte[] getTorrentFileByResource(Resource resource, String username) { |
| 57 | if(resource == null || resource.getTorrentData() == null) return null; |
| 58 | |
| 59 | // 记录下载日志 |
| 60 | Download download = new Download(); |
| 61 | download.setResourceId(String.valueOf(resource.getResourceId())); |
| 62 | download.setDownloader(username); |
| 63 | download.setDownloadTime(LocalDateTime.now()); |
| 64 | downloadRepository.save(download); |
| 65 | |
| 66 | return resource.getTorrentData(); |
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 | public Resource getResourceById(int id) { |
| 70 | return resourceRepository.findById(id).orElse(null); |
| 71 | } |
| 72 | |
| 73 | public void deleteResource(int id) { |
| 74 | Resource resource = getResourceById(id); |
| 75 | if (resource != null) { |
| 76 | // 删除数据库资源记录 |
| 77 | resourceRepository.deleteById(id); |
| 78 | |
| 79 | // 删除对应的 TorrentMeta 元信息 |
| 80 | TorrentMeta meta = torrentMetaRepository.findByFilename(resource.getName()); |
| 81 | if (meta != null) { |
| 82 | torrentMetaRepository.delete(meta); |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | } |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 87 | public List<Resource> searchByQuery(String query) { |
| 88 | return resourceRepository.findByNameContainingIgnoreCase(query); |
| 89 | } |
| 90 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 91 | } |