blob: 7d68817e32c8dd93d6353209a03308d28ea54209 [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;
22301102b1084372025-06-01 16:44:23 +08009import org.springframework.beans.factory.annotation.Autowired;
10import org.springframework.stereotype.Service;
11
12import java.time.LocalDateTime;
13import java.util.List;
14
15@Service
16public class ResourceService {
17
18 @Autowired
19 private ResourceRepository resourceRepository;
20
22301102bc6da0a2025-06-02 17:47:29 +080021 @Autowired
22 private DownloadRepository downloadRepository;
23
24 @Autowired
25 private TorrentService torrentService;
26
27 @Autowired
28 private TorrentMetaRepository torrentMetaRepository;
22301102b1084372025-06-01 16:44:23 +080029
30 public List<Resource> getAllResources() {
31 return resourceRepository.findAll();
32 }
33
22301102bc6da0a2025-06-02 17:47:29 +080034 public List<Resource> getResourcesByAuthor(String username) {
35 return resourceRepository.findByAuthor(username);
36 }
37
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080038 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信息
22301102b1084372025-06-01 16:44:23 +080043 Resource resource = new Resource();
44 resource.setName(name);
22301102b1084372025-06-01 16:44:23 +080045 resource.setDescription(description);
22301102bc6da0a2025-06-02 17:47:29 +080046 resource.setAuthor(author);
47 resource.setSize(size);
22301102b1084372025-06-01 16:44:23 +080048 resource.setPublishTime(LocalDateTime.now());
22301102bc6da0a2025-06-02 17:47:29 +080049
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080050 resource.setTorrentData(torrentBytes);
Edwardsamaxla78ad0a2025-06-09 21:17:29 +080051 resource.setInfo_hash(meta.getInfoHash());
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080052 // 这里可以保存torrent文件路径,或直接存数据库,依据你的设计
22301102b1084372025-06-01 16:44:23 +080053 resourceRepository.save(resource);
54 }
55
22301102bc6da0a2025-06-02 17:47:29 +080056 // 获取资源时,返回BLOB字段内容作为torrent文件
57 public byte[] getTorrentFileByResource(Resource resource, String username) {
58 if(resource == null || resource.getTorrentData() == null) return null;
59
60 // 记录下载日志
61 Download download = new Download();
62 download.setResourceId(String.valueOf(resource.getResourceId()));
63 download.setDownloader(username);
64 download.setDownloadTime(LocalDateTime.now());
65 downloadRepository.save(download);
66
67 return resource.getTorrentData();
22301102b1084372025-06-01 16:44:23 +080068 }
69
22301102bc6da0a2025-06-02 17:47:29 +080070 public Resource getResourceById(int id) {
71 return resourceRepository.findById(id).orElse(null);
72 }
73
74 public void deleteResource(int id) {
75 Resource resource = getResourceById(id);
76 if (resource != null) {
77 // 删除数据库资源记录
78 resourceRepository.deleteById(id);
79
80 // 删除对应的 TorrentMeta 元信息
81 TorrentMeta meta = torrentMetaRepository.findByFilename(resource.getName());
82 if (meta != null) {
83 torrentMetaRepository.delete(meta);
84 }
85
86 }
87 }
22301102b1084372025-06-01 16:44:23 +080088 public List<Resource> searchByQuery(String query) {
89 return resourceRepository.findByNameContainingIgnoreCase(query);
90 }
91
22301102b1084372025-06-01 16:44:23 +080092}