getTorrentList改成了Get,download返回URL,添加了日志,添加了依赖
Change-Id: I8a066f420cbeeaf8e23a534132b5e6470ca53f37
diff --git a/pom.xml b/pom.xml
index af34658..65998ab 100644
--- a/pom.xml
+++ b/pom.xml
@@ -108,6 +108,14 @@
</dependency>
<dependency>
+ <groupId>org.projectlombok</groupId>
+ <artifactId>lombok</artifactId>
+ <version>1.18.26</version>
+ <scope>provided</scope>
+ </dependency>
+
+
+ <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
diff --git a/src/main/java/edu/bjtu/groupone/backend/api/TorrentController.java b/src/main/java/edu/bjtu/groupone/backend/api/TorrentController.java
index ef321b1..0e36a92 100644
--- a/src/main/java/edu/bjtu/groupone/backend/api/TorrentController.java
+++ b/src/main/java/edu/bjtu/groupone/backend/api/TorrentController.java
@@ -20,6 +20,7 @@
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
+import java.util.Map;
@Tag(name = "Torrent 管理", description = "种子文件的上传、下载及列表查询")
@RestController
@@ -75,32 +76,51 @@
}
- @Operation(summary = "下载种子文件", description = "根据 infoHash 返回对应的 .torrent 文件,支持断点续传")
+ @Operation(summary = "下载种子文件", description = "根据 infoHash 返回对应的种子文件 URL(由 OSS 提供)")
@ApiResponses({
@ApiResponse(
responseCode = "200",
- description = "返回 .torrent 文件流",
+ description = "返回 OSS 上的种子文件下载地址",
content = @Content(
- mediaType = "application/x-bittorrent",
+ mediaType = "application/json",
examples = @ExampleObject(
- name = "下载文件示例",
- value = "<二进制流略>"
+ name = "下载链接示例",
+ value = "{ \"url\": \"https://your-bucket.oss-cn-region.aliyuncs.com/abc.torrent\" }"
)
)
),
@ApiResponse(responseCode = "404", description = "未找到对应的种子文件")
})
@GetMapping("/download/{infoHash}")
- public ResponseEntity<Resource> downloadTorrent(
+ public ResponseEntity<?> downloadTorrent(
@Parameter(description = "种子文件的唯一 infoHash", required = true, example = "207d69a047830000620c586e199fc3424e7e5dd8")
@PathVariable String infoHash) {
try {
- Resource resource = torrentService.downloadTorrent(infoHash);
- return ResponseEntity.ok()
- .header("Content-Disposition", "attachment; filename=\"" + infoHash + ".torrent\"")
- .body(resource);
+ String url = torrentService.getTorrentUrl(infoHash);
+ if (url != null) {
+ return ResponseEntity.ok(Map.of("url", url));
+ } else {
+ return ResponseEntity.status(HttpStatus.NOT_FOUND).body("未找到对应的种子文件");
+ }
} catch (Exception ex) {
- return ResponseEntity.notFound().build();
+ return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("下载链接生成失败");
+ }
+ }
+
+ @Operation(summary = "删除种子文件", description = "根据 infoHash 删除服务器上对应的 .torrent 文件和数据库记录")
+ @ApiResponses({
+ @ApiResponse(responseCode = "200", description = "删除成功"),
+ @ApiResponse(responseCode = "404", description = "未找到对应的种子文件")
+ })
+ @DeleteMapping("/delete/{infoHash}")
+ public ResponseEntity<?> deleteTorrent(
+ @Parameter(description = "种子文件的唯一 infoHash", required = true, example = "207d69a047830000620c586e199fc3424e7e5dd8")
+ @PathVariable String infoHash) {
+ boolean deleted = torrentService.deleteTorrent(infoHash);
+ if (deleted) {
+ return ResponseEntity.ok("删除成功");
+ } else {
+ return ResponseEntity.status(HttpStatus.NOT_FOUND).body("未找到对应的种子文件");
}
}
@@ -132,7 +152,7 @@
)
)
)
- @PostMapping("/getTorrentList")
+ @GetMapping("/getTorrentList")
public ResponseEntity<?> getTorrentList() throws Exception {
List<Torrent> list = torrentService.getTorrentList();
diff --git a/src/main/java/edu/bjtu/groupone/backend/mapper/TorrentMapper.java b/src/main/java/edu/bjtu/groupone/backend/mapper/TorrentMapper.java
index 3977ea4..454aeaa 100644
--- a/src/main/java/edu/bjtu/groupone/backend/mapper/TorrentMapper.java
+++ b/src/main/java/edu/bjtu/groupone/backend/mapper/TorrentMapper.java
@@ -1,17 +1,28 @@
package edu.bjtu.groupone.backend.mapper;
import edu.bjtu.groupone.backend.domain.entity.Torrent;
+import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
+@Mapper
public interface TorrentMapper {
- @Insert("insert into torrents (name,info_hash,size,uploader_id,url) values (#{name},#{infoHash},#{size},#{uploaderId},#{url})")
+
+ @Insert("INSERT INTO torrents (name, info_hash, size, uploader_id, url) " +
+ "VALUES (#{name}, #{infoHash}, #{size}, #{uploaderId}, #{url})")
void insertTorrent(Torrent torrent);
- Torrent selectByInfoHash(String infoHash);
+ @Select("SELECT * FROM torrents WHERE info_hash = #{infoHash}")
+ Torrent selectByInfoHash(@Param("infoHash") String infoHash);
- @Select("select * from torrents")
+ @Delete("DELETE FROM torrents WHERE info_hash = #{infoHash}")
+ void deleteByInfoHash(@Param("infoHash") String infoHash);
+
+ @Select("SELECT * FROM torrents")
List<Torrent> getTorrentList();
+
}
diff --git a/src/main/java/edu/bjtu/groupone/backend/service/TorrentService.java b/src/main/java/edu/bjtu/groupone/backend/service/TorrentService.java
index 9c51eef..3c87e63 100644
--- a/src/main/java/edu/bjtu/groupone/backend/service/TorrentService.java
+++ b/src/main/java/edu/bjtu/groupone/backend/service/TorrentService.java
@@ -9,7 +9,9 @@
public interface TorrentService {
Torrent uploadTorrent(MultipartFile file, Long userId) throws Exception;
- Resource downloadTorrent(String infoHash) throws Exception;
+ String getTorrentUrl(String infoHash);
+
+ boolean deleteTorrent(String infoHash);
List<Torrent> getTorrentList();
}
\ No newline at end of file
diff --git a/src/main/java/edu/bjtu/groupone/backend/service/impl/TorrentServiceImpl.java b/src/main/java/edu/bjtu/groupone/backend/service/impl/TorrentServiceImpl.java
index 74c8216..0c195d6 100644
--- a/src/main/java/edu/bjtu/groupone/backend/service/impl/TorrentServiceImpl.java
+++ b/src/main/java/edu/bjtu/groupone/backend/service/impl/TorrentServiceImpl.java
@@ -11,6 +11,7 @@
import java.util.List;
import java.util.Map;
+import lombok.extern.slf4j.Slf4j;
import org.hibernate.sql.Alias;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@@ -20,6 +21,7 @@
import org.springframework.web.multipart.MultipartFile;
@Service
+@Slf4j
public class TorrentServiceImpl implements TorrentService {
@Value("${torrent.storage.path}")
private String storagePath;
@@ -59,20 +61,62 @@
return torrent;
}
- public Resource downloadTorrent(String infoHash) throws Exception {
- Torrent torrent = this.torrentMapper.selectByInfoHash(infoHash);
+ @Override
+ public String getTorrentUrl(String infoHash) {
+ log.info("[getTorrentUrl] 开始,infoHash = {}", infoHash);
+ Torrent torrent;
+ try {
+ torrent = torrentMapper.selectByInfoHash(infoHash);
+ log.info("[getTorrentUrl] 查询到的 Torrent = {}", torrent);
+ } catch (Exception e) {
+ log.error("[getTorrentUrl] 数据库查询异常,infoHash = {}", infoHash, e);
+ // 将异常向上抛出,Controller 会捕获并返回 500
+ throw new RuntimeException("查询种子信息失败", e);
+ }
+
if (torrent == null) {
- throw new IllegalArgumentException("种子不存在");
- } else {
- File file = new File(torrent.getFilePath());
- if (!file.exists()) {
- throw new IllegalStateException("种子文件不存在");
- } else {
- return new FileSystemResource(file);
+ log.warn("[getTorrentUrl] 未找到 infoHash 对应的记录: {}", infoHash);
+ return null;
+ }
+
+ String url = torrent.getUrl();
+ if (url == null || url.isBlank()) {
+ log.warn("[getTorrentUrl] URL 为空,infoHash = {}", infoHash);
+ return null;
+ }
+
+ log.info("[getTorrentUrl] 返回 URL = {}", url);
+ return url;
+ }
+
+ @Override
+ public boolean deleteTorrent(String infoHash) {
+ // 1. 查询种子信息
+ Torrent torrent = torrentMapper.selectByInfoHash(infoHash);
+ if (torrent == null) {
+ return false; // 没有记录,返回失败
+ }
+
+ // 2. 删除本地文件
+ File file = new File(torrent.getFilePath());
+ if (file.exists()) {
+ boolean deleted = file.delete();
+ if (!deleted) {
+ System.err.println("文件删除失败:" + file.getAbsolutePath());
}
}
+
+ // 3. 删除数据库记录
+ torrentMapper.deleteByInfoHash(infoHash);
+
+ // 4. 可选:删除 OSS 上的副本(如果你愿意)
+ // aliOSSUtils.delete(torrent.getUrl()); // 需要你实现 delete 方法
+
+ return true;
}
+
+
@Override
public List<Torrent> getTorrentList() {
return torrentMapper.getTorrentList();