Skip to content

Commit f15a0e6

Browse files
committed
feat: 添加 x-file-storage 使用 demo controller
1 parent 6149584 commit f15a0e6

2 files changed

Lines changed: 67 additions & 5 deletions

File tree

example/example_svc/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@
6161
<groupId>com.github.cadecode</groupId>
6262
<artifactId>uni-boot-example-api</artifactId>
6363
</dependency>
64-
65-
<dependency>
66-
<groupId>com.github.cadecode</groupId>
67-
<artifactId>uni-boot-common-plugin-storage</artifactId>
68-
</dependency>
6964
</dependencies>
7065

7166
<build>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.github.cadecode.uniboot.example.svc.controller;
2+
3+
import cn.hutool.extra.servlet.ServletUtil;
4+
import com.github.cadecode.uniboot.common.core.exception.ApiException;
5+
import com.github.cadecode.uniboot.framework.base.annotation.ApiFormat;
6+
import io.swagger.annotations.Api;
7+
import io.swagger.annotations.ApiOperation;
8+
import lombok.RequiredArgsConstructor;
9+
import lombok.extern.slf4j.Slf4j;
10+
import org.dromara.x.file.storage.core.FileInfo;
11+
import org.dromara.x.file.storage.core.FileStorageService;
12+
import org.springframework.web.bind.annotation.*;
13+
import org.springframework.web.multipart.MultipartFile;
14+
15+
import javax.servlet.http.HttpServletResponse;
16+
17+
/**
18+
* X File Storage 测试 API
19+
*
20+
* @author Cade Li
21+
* @since 2023/10/24
22+
*/
23+
@ApiFormat
24+
@Slf4j
25+
@RequiredArgsConstructor
26+
@Api(tags = " X File Storage 测试")
27+
@RestController
28+
@RequestMapping("demo/x_file_storage")
29+
public class XFileStorageController {
30+
31+
private final FileStorageService fileStorageService;
32+
33+
@ApiOperation("上传")
34+
@PostMapping("upload")
35+
public FileInfo localUpload(@RequestPart MultipartFile file,
36+
@RequestPart(required = false) String customFileName) {
37+
return fileStorageService.of(file)
38+
.setSaveFilename(customFileName)
39+
.upload();
40+
}
41+
42+
@ApiOperation("上传图片(打开缩略)")
43+
@PostMapping("upload-image")
44+
public FileInfo uploadImage(@RequestPart MultipartFile file,
45+
@RequestPart(required = false) String customFileName) {
46+
return fileStorageService.of(file)
47+
.setSaveFilename(customFileName)
48+
.image(img -> img.size(1000, 1000))
49+
.thumbnail(th -> th.size(200, 200))
50+
.upload();
51+
}
52+
53+
@ApiOperation("下载")
54+
@GetMapping("download")
55+
public void download(HttpServletResponse response,
56+
@RequestParam String url) {
57+
// 手动构造文件信息,可用于其它操作
58+
FileInfo fileInfo = fileStorageService.getFileInfoByUrl(url);
59+
// 文件是否存在
60+
boolean exists = fileStorageService.exists(fileInfo);
61+
if (!exists) {
62+
throw ApiException.of("文件不存在!");
63+
}
64+
// 下载
65+
fileStorageService.download(fileInfo).inputStream(in -> ServletUtil.write(response, in));
66+
}
67+
}

0 commit comments

Comments
 (0)