mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-25 16:35:06 +08:00
文件表建加原文件名称字段original_name,相关代码修改
This commit is contained in:
@ -19,8 +19,8 @@ public class FileApiImpl implements FileApi {
|
||||
private FileService fileService;
|
||||
|
||||
@Override
|
||||
public String createFile(String path, byte[] content) throws Exception {
|
||||
return fileService.createFile(path, content);
|
||||
public String createFile(String originalName, byte[] content) throws Exception {
|
||||
return fileService.createFile(originalName, content);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -40,13 +40,11 @@ public class FileController {
|
||||
@PostMapping("/upload")
|
||||
@ApiOperation("上传文件")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class),
|
||||
@ApiImplicitParam(name = "path", value = "文件路径", example = "yudaoyuanma.png", dataTypeClass = String.class)
|
||||
@ApiImplicitParam(name = "file", value = "文件附件", required = true, dataTypeClass = MultipartFile.class)
|
||||
})
|
||||
@OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要
|
||||
public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file,
|
||||
@RequestParam(value = "path", required = false) String path) throws Exception {
|
||||
return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream())));
|
||||
public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
|
||||
return success(fileService.createFile(file.getOriginalFilename(), IoUtil.readBytes(file.getInputStream())));
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete")
|
||||
|
@ -16,6 +16,9 @@ public class FileRespVO {
|
||||
@ApiModelProperty(value = "文件路径", required = true, example = "yudao.jpg")
|
||||
private String path;
|
||||
|
||||
@ApiModelProperty(value = "原文件名", required = true, example = "yudao.jpg")
|
||||
private String originalName;
|
||||
|
||||
@ApiModelProperty(value = "文件 URL", required = true, example = "https://www.iocoder.cn/yudao.jpg")
|
||||
private String url;
|
||||
|
||||
|
@ -33,6 +33,10 @@ public class FileDO extends BaseDO {
|
||||
* 关联 {@link FileConfigDO#getId()}
|
||||
*/
|
||||
private Long configId;
|
||||
/**
|
||||
* 原文件名
|
||||
*/
|
||||
private String originalName;
|
||||
/**
|
||||
* 路径,即文件名
|
||||
*/
|
||||
|
@ -22,11 +22,11 @@ public interface FileService {
|
||||
/**
|
||||
* 保存文件,并返回文件的访问路径
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @param originalName 原文件名称
|
||||
* @param content 文件内容
|
||||
* @return 文件路径
|
||||
*/
|
||||
String createFile(String path, byte[] content) throws Exception;
|
||||
String createFile(String originalName, byte[] content) throws Exception;
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
|
@ -37,12 +37,10 @@ public class FileServiceImpl implements FileService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String createFile(String path, byte[] content) throws Exception {
|
||||
public String createFile(String originalName, byte[] content) throws Exception {
|
||||
// 计算默认的 path 名
|
||||
String type = FileTypeUtil.getType(new ByteArrayInputStream(content), path);
|
||||
if (StrUtil.isEmpty(path)) {
|
||||
path = DigestUtil.md5Hex(content) + '.' + type;
|
||||
}
|
||||
String type = FileTypeUtil.getType(new ByteArrayInputStream(content));
|
||||
String path = DigestUtil.md5Hex(content) + '.' + type;
|
||||
|
||||
// 上传到文件存储器
|
||||
FileClient client = fileConfigService.getMasterFileClient();
|
||||
@ -52,6 +50,7 @@ public class FileServiceImpl implements FileService {
|
||||
// 保存到数据库
|
||||
FileDO file = new FileDO();
|
||||
file.setConfigId(client.getId());
|
||||
file.setOriginalName(originalName);
|
||||
file.setPath(path);
|
||||
file.setUrl(url);
|
||||
file.setType(type);
|
||||
|
@ -80,9 +80,9 @@ public class FileServiceTest extends BaseDbUnitTest {
|
||||
String url = randomString();
|
||||
when(client.upload(same(content), same(path))).thenReturn(url);
|
||||
when(client.getId()).thenReturn(10L);
|
||||
|
||||
String originalName = "单测文件名";
|
||||
// 调用
|
||||
String result = fileService.createFile(path, content);
|
||||
String result = fileService.createFile(originalName, content);
|
||||
// 断言
|
||||
assertEquals(result, url);
|
||||
// 校验数据
|
||||
|
Reference in New Issue
Block a user