blob: d85315e40e6dcbade6780760009925e13bf20740 [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);
51 // 这里可以保存torrent文件路径,或直接存数据库,依据你的设计
22301102b1084372025-06-01 16:44:23 +080052 resourceRepository.save(resource);
53 }
54
22301102bc6da0a2025-06-02 17:47:29 +080055 // 获取资源时,返回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();
22301102b1084372025-06-01 16:44:23 +080067 }
68
22301102bc6da0a2025-06-02 17:47:29 +080069 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 }
22301102b1084372025-06-01 16:44:23 +080087 public List<Resource> searchByQuery(String query) {
88 return resourceRepository.findByNameContainingIgnoreCase(query);
89 }
90
22301102b1084372025-06-01 16:44:23 +080091}