xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 1 | package com.g9.g9backend.controller; |
| 2 | |
Seamher | 5ff35f8 | 2025-06-09 01:23:27 +0800 | [diff] [blame] | 3 | import com.g9.g9backend.pojo.TorrentRecord; |
| 4 | import com.g9.g9backend.service.TorrentRecordService; |
| 5 | import lombok.Getter; |
| 6 | import lombok.Setter; |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 7 | import org.slf4j.Logger; |
| 8 | import org.slf4j.LoggerFactory; |
Seamher | 5ff35f8 | 2025-06-09 01:23:27 +0800 | [diff] [blame] | 9 | import org.springframework.http.ResponseEntity; |
| 10 | import org.springframework.web.bind.annotation.PostMapping; |
| 11 | import org.springframework.web.bind.annotation.RequestBody; |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 12 | import org.springframework.web.bind.annotation.RequestMapping; |
| 13 | import org.springframework.web.bind.annotation.RestController; |
Seamher | 5ff35f8 | 2025-06-09 01:23:27 +0800 | [diff] [blame] | 14 | import org.springframework.web.multipart.MultipartFile; |
| 15 | |
| 16 | import java.io.File; |
| 17 | import java.io.IOException; |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 18 | |
| 19 | /** |
| 20 | * FileController 文件控制器类,处理与文件相关的请求 |
| 21 | * |
| 22 | * @author Seamher |
| 23 | */ |
Seamher | 5ff35f8 | 2025-06-09 01:23:27 +0800 | [diff] [blame] | 24 | @Getter |
| 25 | @Setter |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 26 | @RestController |
| 27 | @RequestMapping("/file") |
| 28 | public class FileController { |
| 29 | |
Seamher | 5ff35f8 | 2025-06-09 01:23:27 +0800 | [diff] [blame] | 30 | private TorrentRecordService torrentRecordService; |
| 31 | |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 32 | private final Logger logger = LoggerFactory.getLogger(FileController.class); |
Seamher | 5ff35f8 | 2025-06-09 01:23:27 +0800 | [diff] [blame] | 33 | |
| 34 | public FileController(TorrentRecordService torrentRecordService) { |
| 35 | this.torrentRecordService = torrentRecordService; |
| 36 | } |
| 37 | |
| 38 | @PostMapping |
| 39 | public ResponseEntity<String> uploadFile(@RequestBody MultipartFile file) { |
| 40 | // 相对路径(可在资源根路径创建 upload 文件夹) |
Seamher | 8ba71e0 | 2025-06-09 22:38:35 +0800 | [diff] [blame] | 41 | String UPLOAD_DIR = "D:/nginx/nginx-1.26.2/MCPT"; |
Seamher | 5ff35f8 | 2025-06-09 01:23:27 +0800 | [diff] [blame] | 42 | |
| 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 | |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 86 | } |