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

This commit is contained in:
谢华宁 2022-05-29 22:40:03 +08:00
parent 0fd5de2d73
commit 20411fa6b5
16 changed files with 37 additions and 31 deletions

View File

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

View File

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

View File

@ -17,7 +17,7 @@ public class FileRespVO {
private String path;
@ApiModelProperty(value = "原文件名", required = true, example = "yudao.jpg")
private String originalName;
private String name;
@ApiModelProperty(value = "文件 URL", required = true, example = "https://www.iocoder.cn/yudao.jpg")
private String url;

View File

@ -36,7 +36,7 @@ public class FileDO extends BaseDO {
/**
* 原文件名
*/
private String originalName;
private String name;
/**
* 路径即文件名
*/

View File

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

View File

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

View File

@ -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 name = "单测文件名";
// 调用
String result = fileService.createFile(originalName, content);
String result = fileService.createFile(name, content);
// 断言
assertEquals(result, url);
// 校验数据

View File

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

View File

@ -57,11 +57,10 @@ public interface MemberUserService {
/**
* 修改用户头像
* @param userId 用户id
* @param originalName 原文件名
* @param inputStream 头像文件
* @return 头像url
*/
String updateUserAvatar(Long userId, String originalName, InputStream inputStream) throws Exception;
String updateUserAvatar(Long userId, InputStream inputStream) throws Exception;
/**
* 修改手机

View File

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

View File

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

View File

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

View File

@ -62,10 +62,9 @@ public interface AdminUserService {
* 更新用户头像
*
* @param id 用户 id
* @param originalName 原文件名称
* @param avatarFile 头像文件
*/
String updateUserAvatar(Long id, String originalName, InputStream avatarFile) throws Exception;
String updateUserAvatar(Long id, InputStream avatarFile) throws Exception;
/**
* 修改密码

View File

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

View File

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

View File

@ -26,6 +26,7 @@
<!-- 列表 -->
<el-table v-loading="loading" :data="list">
<el-table-column label="原文件名" align="center" prop="name" />
<el-table-column label="文件名" align="center" prop="path" />
<el-table-column label="URL" align="center" prop="url" />
<el-table-column label="文件大小" align="center" prop="size" width="120" :formatter="sizeFormat" />