yeg
2026-09-17 e3f412c5474311a471345ea508db4875c9ec648e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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<ImageUploadResult> uploadImage(@RequestParam("file") MultipartFile file) {
        return ApiResponse.success(imageUploadService.upload(file));
    }
 
    @PostMapping("/pdfs")
    @ResponseStatus(HttpStatus.CREATED)
    public ApiResponse<ImageUploadResult> uploadPdf(@RequestParam("file") MultipartFile file) {
        return ApiResponse.success(imageUploadService.uploadPdf(file));
    }
 
    @PostMapping("/videos")
    @ResponseStatus(HttpStatus.CREATED)
    public ApiResponse<ImageUploadResult> 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));
    }
}