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

This commit is contained in:
谢华宁 2022-05-31 22:26:26 +08:00
parent 20411fa6b5
commit 250db847f6
7 changed files with 29 additions and 12 deletions

View File

@ -14,16 +14,28 @@ public interface FileApi {
* @return 文件路径
*/
default String createFile(byte[] content) throws Exception {
return createFile(null, content);
return createFile(null, null, content);
}
/**
* 保存文件并返回文件的访问路径
*
* @param path 文件路径
* @param content 文件内容
* @return 文件路径
*/
default String createFile(String path, byte[] content) throws Exception {
return createFile(null, path, content);
}
/**
* 保存文件并返回文件的访问路径
*
* @param name 原文件名称
* @param path 文件路径
* @param content 文件内容
* @return 文件路径
*/
String createFile(String name, byte[] content) throws Exception;
String createFile(String name, String path, byte[] content) throws Exception;
}

View File

@ -19,8 +19,8 @@ public class FileApiImpl implements FileApi {
private FileService fileService;
@Override
public String createFile(String name, byte[] content) throws Exception {
return fileService.createFile(name, content);
public String createFile(String name, String path, byte[] content) throws Exception {
return fileService.createFile(name, path, content);
}
}

View File

@ -40,11 +40,13 @@ public class FileController {
@PostMapping("/upload")
@ApiOperation("上传文件")
@ApiImplicitParams({
@ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class)
@ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class),
@ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class)
})
@OperateLog(logArgs = false) // 上传文件没有记录操作日志的必要
public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
return success(fileService.createFile(file.getOriginalFilename(), IoUtil.readBytes(file.getInputStream())));
public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file,
@RequestParam(value = "path", required = false) String path) throws Exception {
return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
}
@DeleteMapping("/delete")

View File

@ -23,10 +23,11 @@ public interface FileService {
* 保存文件并返回文件的访问路径
*
* @param name 原文件名称
* @param path 文件路径
* @param content 文件内容
* @return 文件路径
*/
String createFile(String name, byte[] content) throws Exception;
String createFile(String name, String path, byte[] content) throws Exception;
/**
* 删除文件

View File

@ -37,10 +37,12 @@ public class FileServiceImpl implements FileService {
}
@Override
public String createFile(String name, byte[] content) throws Exception {
public String createFile(String name, String path, byte[] content) throws Exception {
// 计算默认的 path
String type = FileTypeUtil.getType(new ByteArrayInputStream(content), name);
String path = DigestUtil.md5Hex(content) + '.' + type;
if (StrUtil.isEmpty(path)) {
path = DigestUtil.md5Hex(content) + '.' + type;
}
// 上传到文件存储器
FileClient client = fileConfigService.getMasterFileClient();

View File

@ -82,7 +82,7 @@ public class FileServiceTest extends BaseDbUnitTest {
when(client.getId()).thenReturn(10L);
String name = "单测文件名";
// 调用
String result = fileService.createFile(name, content);
String result = fileService.createFile(name, path, content);
// 断言
assertEquals(result, url);
// 校验数据

View File

@ -161,7 +161,7 @@ export default {
},
/** 处理上传的文件发生变化 */
handleFileChange(file, fileList) {
this.upload.data.path = file.name;
},
/** 处理文件上传中 */
handleFileUploadProgress(event, file, fileList) {