提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.infra.controller.admin.file; |
H |
2 |
|
|
3 |
import cn.hutool.core.io.IoUtil; |
|
4 |
import cn.hutool.core.util.StrUtil; |
|
5 |
import cn.hutool.core.util.URLUtil; |
|
6 |
import com.iailab.framework.common.pojo.CommonResult; |
|
7 |
import com.iailab.framework.common.pojo.PageResult; |
|
8 |
import com.iailab.framework.common.util.object.BeanUtils; |
|
9 |
import com.iailab.module.infra.controller.admin.file.vo.file.*; |
|
10 |
import com.iailab.module.infra.dal.dataobject.file.FileDO; |
|
11 |
import com.iailab.module.infra.service.file.FileService; |
|
12 |
import io.swagger.v3.oas.annotations.Operation; |
|
13 |
import io.swagger.v3.oas.annotations.Parameter; |
|
14 |
import io.swagger.v3.oas.annotations.tags.Tag; |
|
15 |
import javax.annotation.Resource; |
|
16 |
import javax.annotation.security.PermitAll; |
|
17 |
import javax.servlet.http.HttpServletRequest; |
|
18 |
import javax.servlet.http.HttpServletResponse; |
|
19 |
import javax.validation.Valid; |
|
20 |
import lombok.extern.slf4j.Slf4j; |
|
21 |
import org.springframework.http.HttpStatus; |
|
22 |
import org.springframework.security.access.prepost.PreAuthorize; |
|
23 |
import org.springframework.validation.annotation.Validated; |
|
24 |
import org.springframework.web.bind.annotation.*; |
|
25 |
import org.springframework.web.multipart.MultipartFile; |
|
26 |
|
|
27 |
import static com.iailab.framework.common.pojo.CommonResult.success; |
|
28 |
import static com.iailab.module.infra.framework.file.core.utils.FileTypeUtils.writeAttachment; |
|
29 |
|
|
30 |
@Tag(name = "管理后台 - 文件存储") |
|
31 |
@RestController |
|
32 |
@RequestMapping("/infra/file") |
|
33 |
@Validated |
|
34 |
@Slf4j |
|
35 |
public class FileController { |
|
36 |
|
|
37 |
@Resource |
|
38 |
private FileService fileService; |
|
39 |
|
|
40 |
@PostMapping("/upload") |
|
41 |
@Operation(summary = "上传文件", description = "模式一:后端上传文件") |
|
42 |
public CommonResult<String> uploadFile(FileUploadReqVO uploadReqVO) throws Exception { |
|
43 |
MultipartFile file = uploadReqVO.getFile(); |
|
44 |
String path = uploadReqVO.getPath(); |
|
45 |
return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream()))); |
|
46 |
} |
|
47 |
|
|
48 |
@GetMapping("/presigned-url") |
|
49 |
@Operation(summary = "获取文件预签名地址", description = "模式二:前端上传文件:用于前端直接上传七牛、阿里云 OSS 等文件存储器") |
|
50 |
public CommonResult<FilePresignedUrlRespVO> getFilePresignedUrl(@RequestParam("path") String path) throws Exception { |
|
51 |
return success(fileService.getFilePresignedUrl(path)); |
|
52 |
} |
|
53 |
|
|
54 |
@PostMapping("/create") |
|
55 |
@Operation(summary = "创建文件", description = "模式二:前端上传文件:配合 presigned-url 接口,记录上传了上传的文件") |
|
56 |
public CommonResult<Long> createFile(@Valid @RequestBody FileCreateReqVO createReqVO) { |
|
57 |
return success(fileService.createFile(createReqVO)); |
|
58 |
} |
|
59 |
|
|
60 |
@DeleteMapping("/delete") |
|
61 |
@Operation(summary = "删除文件") |
|
62 |
@Parameter(name = "id", description = "编号", required = true) |
|
63 |
@PreAuthorize("@ss.hasPermission('infra:file:delete')") |
|
64 |
public CommonResult<Boolean> deleteFile(@RequestParam("id") Long id) throws Exception { |
|
65 |
fileService.deleteFile(id); |
|
66 |
return success(true); |
|
67 |
} |
|
68 |
|
|
69 |
@GetMapping("/{configId}/get/**") |
|
70 |
@PermitAll |
|
71 |
@Operation(summary = "下载文件") |
|
72 |
@Parameter(name = "configId", description = "配置编号", required = true) |
|
73 |
public void getFileContent(HttpServletRequest request, |
|
74 |
HttpServletResponse response, |
|
75 |
@PathVariable("configId") Long configId) throws Exception { |
|
76 |
// 获取请求的路径 |
|
77 |
String path = StrUtil.subAfter(request.getRequestURI(), "/get/", false); |
|
78 |
if (StrUtil.isEmpty(path)) { |
|
79 |
throw new IllegalArgumentException("结尾的 path 路径必须传递"); |
|
80 |
} |
|
81 |
// 解码,解决中文路径的问题 https://gitee.com/zhijiantianya/ruoyi-vue-pro/pulls/807/ |
|
82 |
path = URLUtil.decode(path); |
|
83 |
|
|
84 |
// 读取内容 |
|
85 |
byte[] content = fileService.getFileContent(configId, path); |
|
86 |
if (content == null) { |
|
87 |
log.warn("[getFileContent][configId({}) path({}) 文件不存在]", configId, path); |
|
88 |
response.setStatus(HttpStatus.NOT_FOUND.value()); |
|
89 |
return; |
|
90 |
} |
|
91 |
writeAttachment(response, path, content); |
|
92 |
} |
|
93 |
|
|
94 |
@GetMapping("/page") |
|
95 |
@Operation(summary = "获得文件分页") |
|
96 |
@PreAuthorize("@ss.hasPermission('infra:file:query')") |
|
97 |
public CommonResult<PageResult<FileRespVO>> getFilePage(@Valid FilePageReqVO pageVO) { |
|
98 |
PageResult<FileDO> pageResult = fileService.getFilePage(pageVO); |
|
99 |
return success(BeanUtils.toBean(pageResult, FileRespVO.class)); |
|
100 |
} |
|
101 |
|
|
102 |
} |