文件表建加原文件名称字段original_name,相关代码修改

pull/2/head
谢华宁 2022-05-29 16:19:56 +08:00
parent add9317d89
commit 0fd5de2d73
16 changed files with 40 additions and 43 deletions

View File

@ -1,7 +1,5 @@
package cn.iocoder.yudao.module.infra.api.file; package cn.iocoder.yudao.module.infra.api.file;
import cn.hutool.core.util.IdUtil;
/** /**
* API * API
* *
@ -12,20 +10,10 @@ public interface FileApi {
/** /**
* 访 * 访
* *
* @param originalName
* @param content * @param content
* @return * @return
*/ */
default String createFile(byte[] content) throws Exception { String createFile(String originalName, byte[] content) throws Exception;
return createFile(IdUtil.fastUUID(), content);
}
/**
* 访
*
* @param path
* @param content
* @return
*/
String createFile(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 path, byte[] content) throws Exception { public String createFile(String originalName, byte[] content) throws Exception {
return fileService.createFile(path, content); return fileService.createFile(originalName, content);
} }
} }

View File

@ -40,13 +40,11 @@ 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, public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
@RequestParam(value = "path", required = false) String path) throws Exception { return success(fileService.createFile(file.getOriginalFilename(), IoUtil.readBytes(file.getInputStream())));
return success(fileService.createFile(path, IoUtil.readBytes(file.getInputStream())));
} }
@DeleteMapping("/delete") @DeleteMapping("/delete")

View File

@ -16,6 +16,9 @@ public class FileRespVO {
@ApiModelProperty(value = "文件路径", required = true, example = "yudao.jpg") @ApiModelProperty(value = "文件路径", required = true, example = "yudao.jpg")
private String path; 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") @ApiModelProperty(value = "文件 URL", required = true, example = "https://www.iocoder.cn/yudao.jpg")
private String url; private String url;

View File

@ -33,6 +33,10 @@ public class FileDO extends BaseDO {
* {@link FileConfigDO#getId()} * {@link FileConfigDO#getId()}
*/ */
private Long configId; private Long configId;
/**
*
*/
private String originalName;
/** /**
* *
*/ */

View File

@ -22,11 +22,11 @@ public interface FileService {
/** /**
* 访 * 访
* *
* @param path * @param originalName
* @param content * @param content
* @return * @return
*/ */
String createFile(String path, byte[] content) throws Exception; String createFile(String originalName, byte[] content) throws Exception;
/** /**
* *

View File

@ -37,12 +37,10 @@ public class FileServiceImpl implements FileService {
} }
@Override @Override
public String createFile(String path, byte[] content) throws Exception { public String createFile(String originalName, byte[] content) throws Exception {
// 计算默认的 path 名 // 计算默认的 path 名
String type = FileTypeUtil.getType(new ByteArrayInputStream(content), path); String type = FileTypeUtil.getType(new ByteArrayInputStream(content));
if (StrUtil.isEmpty(path)) { String path = DigestUtil.md5Hex(content) + '.' + type;
path = DigestUtil.md5Hex(content) + '.' + type;
}
// 上传到文件存储器 // 上传到文件存储器
FileClient client = fileConfigService.getMasterFileClient(); FileClient client = fileConfigService.getMasterFileClient();
@ -52,6 +50,7 @@ public class FileServiceImpl implements FileService {
// 保存到数据库 // 保存到数据库
FileDO file = new FileDO(); FileDO file = new FileDO();
file.setConfigId(client.getId()); file.setConfigId(client.getId());
file.setOriginalName(originalName);
file.setPath(path); file.setPath(path);
file.setUrl(url); file.setUrl(url);
file.setType(type); file.setType(type);

View File

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

View File

@ -47,7 +47,7 @@ public class AppUserController {
if (file.isEmpty()) { if (file.isEmpty()) {
throw exception(FILE_IS_EMPTY); throw exception(FILE_IS_EMPTY);
} }
String avatar = userService.updateUserAvatar(getLoginUserId(), file.getInputStream()); String avatar = userService.updateUserAvatar(getLoginUserId(), file.getOriginalFilename(), file.getInputStream());
return success(avatar); return success(avatar);
} }

View File

@ -57,10 +57,11 @@ public interface MemberUserService {
/** /**
* *
* @param userId id * @param userId id
* @param originalName
* @param inputStream * @param inputStream
* @return url * @return url
*/ */
String updateUserAvatar(Long userId, InputStream inputStream) throws Exception; String updateUserAvatar(Long userId, String originalName, InputStream inputStream) throws Exception;
/** /**
* *

View File

@ -100,10 +100,10 @@ public class MemberUserServiceImpl implements MemberUserService {
} }
@Override @Override
public String updateUserAvatar(Long userId, InputStream avatarFile) throws Exception { public String updateUserAvatar(Long userId, String originalName, InputStream avatarFile) throws Exception {
this.checkUserExists(userId); this.checkUserExists(userId);
// 创建文件 // 创建文件
String avatar = fileApi.createFile(IoUtil.readBytes(avatarFile)); String avatar = fileApi.createFile(originalName,IoUtil.readBytes(avatarFile));
// 更新头像路径 // 更新头像路径
memberUserMapper.updateById(MemberUserDO.builder().id(userId).avatar(avatar).build()); memberUserMapper.updateById(MemberUserDO.builder().id(userId).avatar(avatar).build());
return avatar; return avatar;

View File

@ -86,9 +86,10 @@ public class MemberUserServiceImplTest extends BaseDbAndRedisUnitTest {
ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes); ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes);
// mock 方法 // mock 方法
String avatar = randomString(); String avatar = randomString();
when(fileApi.createFile(eq(avatarFileBytes))).thenReturn(avatar); String originalName = "单测文件名";
when(fileApi.createFile(originalName, eq(avatarFileBytes))).thenReturn(avatar);
// 调用 // 调用
String str = memberUserService.updateUserAvatar(userId, avatarFile); String str = memberUserService.updateUserAvatar(userId, originalName, avatarFile);
// 断言 // 断言
assertEquals(avatar, str); assertEquals(avatar, str);
} }

View File

@ -97,11 +97,12 @@ public class UserProfileController {
@PutMapping("/update-avatar") @PutMapping("/update-avatar")
@ApiOperation("上传用户个人头像") @ApiOperation("上传用户个人头像")
public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws Exception { public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file,
@RequestParam(value = "originalName", required = false) String originalName) throws Exception {
if (file.isEmpty()) { if (file.isEmpty()) {
throw ServiceExceptionUtil.exception(FILE_IS_EMPTY); throw ServiceExceptionUtil.exception(FILE_IS_EMPTY);
} }
String avatar = userService.updateUserAvatar(getLoginUserId(), file.getInputStream()); String avatar = userService.updateUserAvatar(getLoginUserId(), originalName, file.getInputStream());
return success(avatar); return success(avatar);
} }

View File

@ -62,9 +62,10 @@ public interface AdminUserService {
* *
* *
* @param id id * @param id id
* @param originalName
* @param avatarFile * @param avatarFile
*/ */
String updateUserAvatar(Long id, InputStream avatarFile) throws Exception; String updateUserAvatar(Long id, String originalName, InputStream avatarFile) throws Exception;
/** /**
* *

View File

@ -153,10 +153,10 @@ public class AdminUserServiceImpl implements AdminUserService {
} }
@Override @Override
public String updateUserAvatar(Long id, InputStream avatarFile) throws Exception { public String updateUserAvatar(Long id, String originalName, InputStream avatarFile) throws Exception {
checkUserExists(id); checkUserExists(id);
// 存储文件 // 存储文件
String avatar = fileApi.createFile(IoUtil.readBytes(avatarFile)); String avatar = fileApi.createFile(originalName,IoUtil.readBytes(avatarFile));
// 更新路径 // 更新路径
AdminUserDO sysUserDO = new AdminUserDO(); AdminUserDO sysUserDO = new AdminUserDO();
sysUserDO.setId(id); sysUserDO.setId(id);

View File

@ -225,10 +225,11 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest {
ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes); ByteArrayInputStream avatarFile = new ByteArrayInputStream(avatarFileBytes);
// mock 方法 // mock 方法
String avatar = randomString(); String avatar = randomString();
when(fileApi.createFile(eq(avatarFileBytes))).thenReturn(avatar); String originalName = "单测文件名";
when(fileApi.createFile(originalName, eq( avatarFileBytes))).thenReturn(avatar);
// 调用 // 调用
userService.updateUserAvatar(userId, avatarFile); userService.updateUserAvatar(userId, originalName, avatarFile);
// 断言 // 断言
AdminUserDO user = userMapper.selectById(userId); AdminUserDO user = userMapper.selectById(userId);
assertEquals(avatar, user.getAvatar()); assertEquals(avatar, user.getAvatar());