mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 15:21:53 +08:00
文件表建加原文件名称字段name,相关代码修改
This commit is contained in:
parent
0fd5de2d73
commit
20411fa6b5
@ -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;
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -36,7 +36,7 @@ public class FileDO extends BaseDO {
|
||||
/**
|
||||
* 原文件名
|
||||
*/
|
||||
private String originalName;
|
||||
private String name;
|
||||
/**
|
||||
* 路径,即文件名
|
||||
*/
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
// 校验数据
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* 修改手机
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
|
@ -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);
|
||||
|
@ -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());
|
||||
|
@ -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" />
|
||||
|
Loading…
Reference in New Issue
Block a user