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

pull/2/head
谢华宁 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 * @return
*/ */
default String createFile(byte[] content) throws Exception { 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 name
* @param path
* @param content * @param content
* @return * @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; private FileService fileService;
@Override @Override
public String createFile(String name, byte[] content) throws Exception { public String createFile(String name, String path, byte[] content) throws Exception {
return fileService.createFile(name, content); return fileService.createFile(name, path, content);
} }
} }

View File

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

View File

@ -23,10 +23,11 @@ public interface FileService {
* 访 * 访
* *
* @param name * @param name
* @param path
* @param content * @param content
* @return * @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 @Override
public String createFile(String name, byte[] content) throws Exception { public String createFile(String name, String path, byte[] content) throws Exception {
// 计算默认的 path 名 // 计算默认的 path 名
String type = FileTypeUtil.getType(new ByteArrayInputStream(content), name); 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(); FileClient client = fileConfigService.getMasterFileClient();

View File

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

View File

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