blob: 947c4af3eb20d85f0208e6a45ea094ab5c65e3c5 [file] [log] [blame]
xiukira687b9cb2025-05-29 15:15:02 +08001package com.g9.g9backend.controller;
2
Seamher5ff35f82025-06-09 01:23:27 +08003import com.g9.g9backend.pojo.TorrentRecord;
4import com.g9.g9backend.service.TorrentRecordService;
5import lombok.Getter;
6import lombok.Setter;
xiukira687b9cb2025-05-29 15:15:02 +08007import org.slf4j.Logger;
8import org.slf4j.LoggerFactory;
Seamher5ff35f82025-06-09 01:23:27 +08009import org.springframework.http.ResponseEntity;
10import org.springframework.web.bind.annotation.PostMapping;
11import org.springframework.web.bind.annotation.RequestBody;
xiukira687b9cb2025-05-29 15:15:02 +080012import org.springframework.web.bind.annotation.RequestMapping;
13import org.springframework.web.bind.annotation.RestController;
Seamher5ff35f82025-06-09 01:23:27 +080014import org.springframework.web.multipart.MultipartFile;
15
16import java.io.File;
17import java.io.IOException;
xiukira687b9cb2025-05-29 15:15:02 +080018
19/**
20 * FileController 文件控制器类,处理与文件相关的请求
21 *
22 * @author Seamher
23 */
Seamher5ff35f82025-06-09 01:23:27 +080024@Getter
25@Setter
xiukira687b9cb2025-05-29 15:15:02 +080026@RestController
27@RequestMapping("/file")
28public class FileController {
29
Seamher5ff35f82025-06-09 01:23:27 +080030 private TorrentRecordService torrentRecordService;
31
xiukira687b9cb2025-05-29 15:15:02 +080032 private final Logger logger = LoggerFactory.getLogger(FileController.class);
Seamher5ff35f82025-06-09 01:23:27 +080033
34 public FileController(TorrentRecordService torrentRecordService) {
35 this.torrentRecordService = torrentRecordService;
36 }
37
38 @PostMapping
39 public ResponseEntity<String> uploadFile(@RequestBody MultipartFile file) {
40 // 相对路径(可在资源根路径创建 upload 文件夹)
Seamher3eb882d2025-06-09 23:56:09 +080041 String UPLOAD_DIR = "/app/upload";
Seamher5ff35f82025-06-09 01:23:27 +080042
43 // 获取原始文件名
44 String originalFilename = file.getOriginalFilename();
45 if (originalFilename.isEmpty()) {
46 return ResponseEntity.badRequest().body("文件名为空");
47 }
48
49 // 为文件名加时间戳,防止重名冲突
50 String filename = System.currentTimeMillis() + "_" + originalFilename;
51
52 // 构建文件保存目录(确保 upload 目录存在)
53 File uploadDir = new File(UPLOAD_DIR);
54 if (!uploadDir.exists()) {
55 boolean created = uploadDir.mkdirs(); // 创建目录
56 if (!created) {
57 logger.error("无法创建上传目录: " + uploadDir.getAbsolutePath());
58 return ResponseEntity.internalServerError().body("无法创建上传目录");
59 }
60 }
61
62 // 构建完整的文件路径
63 File dest = new File(uploadDir, filename);
64
65 try {
66 // 保存文件
67 file.transferTo(dest);
68 } catch (IOException e) {
69 logger.error("上传失败: " + e.getMessage(), e);
70 return ResponseEntity.internalServerError().body("上传失败");
71 }
72
73 // 返回访问 URL(你可能需要根据 Nginx 的静态资源映射来配置)
74 String url = "http://localhost:65/" + filename;
75
76 return ResponseEntity.ok(url);
77 }
78
79 @PostMapping(value = "bt")
80 public ResponseEntity<String> uploadBTFile(@RequestBody TorrentRecord torrentRecord) {
81 torrentRecordService.save(torrentRecord);
82
83 return ResponseEntity.ok("");
84 }
85
xiukira687b9cb2025-05-29 15:15:02 +080086}