blob: 1d0e797530c5a37fcd2094410439953537e6b152 [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;
9import com.pt.utils.BencodeUtils;
22301102b1084372025-06-01 16:44:23 +080010import org.springframework.beans.factory.annotation.Autowired;
11import org.springframework.stereotype.Service;
12
22301102bc6da0a2025-06-02 17:47:29 +080013import java.io.File;
14import java.io.IOException;
15import java.nio.file.Files;
22301102b1084372025-06-01 16:44:23 +080016import java.time.LocalDateTime;
17import java.util.List;
22301102bc6da0a2025-06-02 17:47:29 +080018import java.util.Map;
22301102b1084372025-06-01 16:44:23 +080019
20@Service
21public class ResourceService {
22
23 @Autowired
24 private ResourceRepository resourceRepository;
25
22301102bc6da0a2025-06-02 17:47:29 +080026 @Autowired
27 private DownloadRepository downloadRepository;
28
29 @Autowired
30 private TorrentService torrentService;
31
32 @Autowired
33 private TorrentMetaRepository torrentMetaRepository;
22301102b1084372025-06-01 16:44:23 +080034
35 public List<Resource> getAllResources() {
36 return resourceRepository.findAll();
37 }
38
22301102bc6da0a2025-06-02 17:47:29 +080039 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 // 保存资源信息到数据库,包括种子文件的二进制数据
22301102b1084372025-06-01 16:44:23 +080046 Resource resource = new Resource();
47 resource.setName(name);
22301102b1084372025-06-01 16:44:23 +080048 resource.setDescription(description);
22301102bc6da0a2025-06-02 17:47:29 +080049 resource.setAuthor(author);
50 resource.setSize(size);
22301102b1084372025-06-01 16:44:23 +080051 resource.setPublishTime(LocalDateTime.now());
22301102bc6da0a2025-06-02 17:47:29 +080052
53 // 生成种子数据 byte[]
54 byte[] torrentData = torrentService.generateTorrentBytes("uploads/" + name);
55 resource.setTorrentData(torrentData);
56
22301102b1084372025-06-01 16:44:23 +080057 resourceRepository.save(resource);
58 }
59
22301102bc6da0a2025-06-02 17:47:29 +080060 // 获取资源时,返回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();
22301102b1084372025-06-01 16:44:23 +080072 }
73
22301102bc6da0a2025-06-02 17:47:29 +080074 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 }
22301102b1084372025-06-01 16:44:23 +080092 public List<Resource> searchByQuery(String query) {
93 return resourceRepository.findByNameContainingIgnoreCase(query);
94 }
95
22301102b1084372025-06-01 16:44:23 +080096}