liyong
4 天以前 d62a74c14a4002c0f401c94976fba8cc77cda6e1
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package cn.iocoder.yudao.module.system.controller.admin.storage;
 
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.controller.admin.storage.vo.SystemStorageBlobRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.storage.SystemStorageBlobDO;
import cn.iocoder.yudao.module.system.service.storage.SystemStorageBlobService;
import cn.iocoder.yudao.module.system.util.storage.StorageFileUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.annotation.security.PermitAll;
import lombok.AllArgsConstructor;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ContentDisposition;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
 
/**
 * 管理后台 - 文件上传/预览/下载
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - 文件上传")
@RestController
@RequestMapping("/system/storage-blob")
@Validated
public class SystemStorageBlobController {
 
    @Resource
    private SystemStorageBlobService storageBlobService;
    @Resource
    private StorageFileUtil fileUtil;
 
    @PostMapping("/upload")
    @Operation(summary = "文件上传")
    public CommonResult<List<SystemStorageBlobRespVO>> uploadFile(
            @RequestParam("files") List<MultipartFile> files) {
        return success(storageBlobService.upload(files, false));
    }
 
    @PostMapping("/public-upload")
    @Operation(summary = "公共文件上传(永久有效,慎用)")
    public CommonResult<List<SystemStorageBlobRespVO>> publicUploadFile(
            @RequestParam("files") List<MultipartFile> files) {
        return success(storageBlobService.upload(files, true));
    }
 
    @GetMapping("/preview/{fileName}")
    @Operation(summary = "文件预览")
    @PermitAll
    public ResponseEntity<FileSystemResource> preview(
            @PathVariable String fileName,
            @RequestParam(value = "token", required = false) String token,
            @RequestParam(value = "publicKey", required = false) String publicKey) throws Exception {
        File file;
        if (publicKey != null) {
            file = fileUtil.compressFile(storageBlobService.getPublicFile(fileName, publicKey));
        } else {
            file = fileUtil.compressFile(storageBlobService.getFileByToken(fileName, token));
        }
        String contentType = Files.probeContentType(file.toPath());
 
        ContentDisposition contentDisposition = ContentDisposition.inline()
                .filename(fileName, StandardCharsets.UTF_8)
                .build();
 
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType(contentType != null ? contentType : "application/octet-stream"))
                .contentLength(file.length())
                .header("Content-Disposition", contentDisposition.toString())
                .body(new FileSystemResource(file));
    }
 
    @GetMapping("/download/{fileName}")
    @Operation(summary = "文件下载")
    @PermitAll
    public void download(
            @PathVariable String fileName,
            @RequestParam(value = "token", required = false) String token,
            @RequestParam(value = "publicKey", required = false) String publicKey,
            HttpServletResponse response) throws Exception {
        File file;
        if (publicKey != null) {
            file = fileUtil.compressFile(storageBlobService.getPublicFile(fileName, publicKey));
        } else {
            file = fileUtil.compressFile(storageBlobService.getFileByToken(fileName, token));
        }
        String originalFileName = storageBlobService.getDownloadFileName(fileName);
        String encodedFileName = URLEncoder.encode(originalFileName, StandardCharsets.UTF_8.name()).replace("+", "%20");
        response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + encodedFileName);
        response.setContentLengthLong(file.length());
        Files.copy(file.toPath(), response.getOutputStream());
    }
 
}