Commit 250db847f6da7c8ab8faea8d4a90f2ebf26324fe

Authored by 谢华宁
1 parent 20411fa6

文件上传接口保留path参数,方便覆盖文件

yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApi.java
... ... @@ -14,16 +14,28 @@ public interface FileApi {
14 14 * @return 文件路径
15 15 */
16 16 default String createFile(byte[] content) throws Exception {
17   - return createFile(null, content);
  17 + return createFile(null, null, content);
  18 + }
  19 +
  20 + /**
  21 + * 保存文件,并返回文件的访问路径
  22 + *
  23 + * @param path 文件路径
  24 + * @param content 文件内容
  25 + * @return 文件路径
  26 + */
  27 + default String createFile(String path, byte[] content) throws Exception {
  28 + return createFile(null, path, content);
18 29 }
19 30  
20 31 /**
21 32 * 保存文件,并返回文件的访问路径
22 33 *
23 34 * @param name 原文件名称
  35 + * @param path 文件路径
24 36 * @param content 文件内容
25 37 * @return 文件路径
26 38 */
27   - String createFile(String name, byte[] content) throws Exception;
  39 + String createFile(String name, String path, byte[] content) throws Exception;
28 40  
29 41 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/file/FileApiImpl.java
... ... @@ -19,8 +19,8 @@ public class FileApiImpl implements FileApi {
19 19 private FileService fileService;
20 20  
21 21 @Override
22   - public String createFile(String name, byte[] content) throws Exception {
23   - return fileService.createFile(name, content);
  22 + public String createFile(String name, String path, byte[] content) throws Exception {
  23 + return fileService.createFile(name, path, content);
24 24 }
25 25  
26 26 }
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java
... ... @@ -40,11 +40,13 @@ public class FileController {
40 40 @PostMapping("/upload")
41 41 @ApiOperation("上传文件")
42 42 @ApiImplicitParams({
43   - @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class)
  43 + @ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class),
  44 + @ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class)
44 45 })
45 46 @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要
46   - public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
47   - return success(fileService.createFile(file.getOriginalFilename(), IoUtil.readBytes(file.getInputStream())));
  47 + public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file,
  48 + @RequestParam(value = "path", required = false) String path) throws Exception {
  49 + return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
48 50 }
49 51  
50 52 @DeleteMapping("/delete")
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java
... ... @@ -23,10 +23,11 @@ public interface FileService {
23 23 * 保存文件,并返回文件的访问路径
24 24 *
25 25 * @param name 原文件名称
  26 + * @param path 文件路径
26 27 * @param content 文件内容
27 28 * @return 文件路径
28 29 */
29   - String createFile(String name, byte[] content) throws Exception;
  30 + String createFile(String name, String path, byte[] content) throws Exception;
30 31  
31 32 /**
32 33 * 删除文件
... ...
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java
... ... @@ -37,10 +37,12 @@ public class FileServiceImpl implements FileService {
37 37 }
38 38  
39 39 @Override
40   - public String createFile(String name, byte[] content) throws Exception {
  40 + public String createFile(String name, String path, byte[] content) throws Exception {
41 41 // 计算默认的 path 名
42 42 String type = FileTypeUtil.getType(new ByteArrayInputStream(content), name);
43   - String path = DigestUtil.md5Hex(content) + '.' + type;
  43 + if (StrUtil.isEmpty(path)) {
  44 + path = DigestUtil.md5Hex(content) + '.' + type;
  45 + }
44 46  
45 47 // 上传到文件存储器
46 48 FileClient client = fileConfigService.getMasterFileClient();
... ...
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceTest.java
... ... @@ -82,7 +82,7 @@ public class FileServiceTest extends BaseDbUnitTest {
82 82 when(client.getId()).thenReturn(10L);
83 83 String name = "单测文件名";
84 84 // 调用
85   - String result = fileService.createFile(name, content);
  85 + String result = fileService.createFile(name, path, content);
86 86 // 断言
87 87 assertEquals(result, url);
88 88 // 校验数据
... ...
yudao-ui-admin/src/views/infra/file/index.vue
... ... @@ -161,7 +161,7 @@ export default {
161 161 },
162 162 /** 处理上传的文件发生变化 */
163 163 handleFileChange(file, fileList) {
164   - this.upload.data.path = file.name;
  164 +
165 165 },
166 166 /** 处理文件上传中 */
167 167 handleFileUploadProgress(event, file, fileList) {
... ...