liyong
2 天以前 88ae1e650fc2fc30928edfe8f3cc39108d8d1ccd
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
package com.ruoyi.project.common;
 
import com.ruoyi.basic.service.StorageBlobService;
import com.ruoyi.basic.utils.FileUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.framework.web.domain.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
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.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
 
/**
 * 通用请求处理
 *
 * @author ruoyi
 */
@Tag(name = "通用接口")
@AllArgsConstructor
@RestController
@RequestMapping("/common")
public class CommonController {
 
 
    private final StorageBlobService storageBlobService;
    private final FileUtil fileUtil;
 
    @PostMapping({"/upload"})
    @Operation(summary = "文件上传")
    public R upload(@RequestParam("files") List<MultipartFile> files) {
        return R.ok(storageBlobService.upload(files, false));
    }
 
    /**
     * 公共文件上传
     * 此接口上传的文件永久有效,慎用
     */
    @PostMapping({"/public/upload"})
    @Operation(summary = "文件上传")
    public R publicUpload(@RequestParam("files") List<MultipartFile> files) {
        return R.ok(storageBlobService.upload(files, true));
    }
 
    @GetMapping("/download/{fileName}")
    @Anonymous
    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());
    }
 
    @GetMapping("/preview/{fileName}")
    @Anonymous
    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));
    }
}