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; |
| 9 | import com.pt.utils.BencodeUtils; |
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 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame^] | 13 | import java.io.File; |
| 14 | import java.io.IOException; |
| 15 | import java.nio.file.Files; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 16 | import java.time.LocalDateTime; |
| 17 | import java.util.List; |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame^] | 18 | import java.util.Map; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 19 | |
| 20 | @Service |
| 21 | public class ResourceService { |
| 22 | |
| 23 | @Autowired |
| 24 | private ResourceRepository resourceRepository; |
| 25 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame^] | 26 | @Autowired |
| 27 | private DownloadRepository downloadRepository; |
| 28 | |
| 29 | @Autowired |
| 30 | private TorrentService torrentService; |
| 31 | |
| 32 | @Autowired |
| 33 | private TorrentMetaRepository torrentMetaRepository; |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 34 | |
| 35 | public List<Resource> getAllResources() { |
| 36 | return resourceRepository.findAll(); |
| 37 | } |
| 38 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame^] | 39 | public List<Resource> getResourcesByAuthor(String username) { |
| 40 | return resourceRepository.findByAuthor(username); |
| 41 | } |
| 42 | |
| 43 | // 发布资源时,将种子文件二进制数据保存到数据库 |
| 44 | public void publishResource(String name, String description, String author, double size) throws Exception { |
| 45 | // 保存资源信息到数据库,包括种子文件的二进制数据 |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 46 | Resource resource = new Resource(); |
| 47 | resource.setName(name); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 48 | resource.setDescription(description); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame^] | 49 | resource.setAuthor(author); |
| 50 | resource.setSize(size); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 51 | resource.setPublishTime(LocalDateTime.now()); |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame^] | 52 | |
| 53 | // 生成种子数据 byte[] |
| 54 | byte[] torrentData = torrentService.generateTorrentBytes("uploads/" + name); |
| 55 | resource.setTorrentData(torrentData); |
| 56 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 57 | resourceRepository.save(resource); |
| 58 | } |
| 59 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame^] | 60 | // 获取资源时,返回BLOB字段内容作为torrent文件 |
| 61 | public byte[] getTorrentFileByResource(Resource resource, String username) { |
| 62 | if(resource == null || resource.getTorrentData() == null) return null; |
| 63 | |
| 64 | // 记录下载日志 |
| 65 | Download download = new Download(); |
| 66 | download.setResourceId(String.valueOf(resource.getResourceId())); |
| 67 | download.setDownloader(username); |
| 68 | download.setDownloadTime(LocalDateTime.now()); |
| 69 | downloadRepository.save(download); |
| 70 | |
| 71 | return resource.getTorrentData(); |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 72 | } |
| 73 | |
22301102 | bc6da0a | 2025-06-02 17:47:29 +0800 | [diff] [blame^] | 74 | public Resource getResourceById(int id) { |
| 75 | return resourceRepository.findById(id).orElse(null); |
| 76 | } |
| 77 | |
| 78 | public void deleteResource(int id) { |
| 79 | Resource resource = getResourceById(id); |
| 80 | if (resource != null) { |
| 81 | // 删除数据库资源记录 |
| 82 | resourceRepository.deleteById(id); |
| 83 | |
| 84 | // 删除对应的 TorrentMeta 元信息 |
| 85 | TorrentMeta meta = torrentMetaRepository.findByFilename(resource.getName()); |
| 86 | if (meta != null) { |
| 87 | torrentMetaRepository.delete(meta); |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | } |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 92 | public List<Resource> searchByQuery(String query) { |
| 93 | return resourceRepository.findByNameContainingIgnoreCase(query); |
| 94 | } |
| 95 | |
22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 96 | } |