package com.talentcard.server.upload; import com.talentcard.server.common.ApiResponse; import com.talentcard.server.common.BusinessException; import com.talentcard.server.common.ErrorCode; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.nio.file.*; import java.util.UUID; @RestController @RequestMapping("/api/admin/uploads") public class AdminUploadController { private final ImageUploadService imageUploadService; public AdminUploadController(ImageUploadService imageUploadService) { this.imageUploadService = imageUploadService; } @PostMapping("/images") @ResponseStatus(HttpStatus.CREATED) public ApiResponse uploadImage(@RequestParam("file") MultipartFile file) { return ApiResponse.success(imageUploadService.upload(file)); } @PostMapping("/pdfs") @ResponseStatus(HttpStatus.CREATED) public ApiResponse uploadPdf(@RequestParam("file") MultipartFile file) { return ApiResponse.success(imageUploadService.uploadPdf(file)); } @PostMapping("/videos") @ResponseStatus(HttpStatus.CREATED) public ApiResponse uploadVideo(@RequestParam("file") MultipartFile file) throws java.io.IOException { if (file == null || file.isEmpty() || file.getSize() > 100L * 1024 * 1024) throw new BusinessException(ErrorCode.BAD_REQUEST, "请选择不超过100MB的视频文件"); String type = file.getContentType() == null ? "" : file.getContentType().toLowerCase(); if (!type.equals("video/mp4")) throw new BusinessException(ErrorCode.BAD_REQUEST, "仅支持 MP4 视频"); Path directory = Path.of("uploads").toAbsolutePath().normalize(); Files.createDirectories(directory); String filename = UUID.randomUUID() + ".mp4"; Files.copy(file.getInputStream(), directory.resolve(filename), StandardCopyOption.REPLACE_EXISTING); return ApiResponse.success(new ImageUploadResult("/uploads/" + filename)); } }