文件表建加原文件名称字段original_name,相关代码修改
parent
add9317d89
commit
0fd5de2d73
|
@ -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;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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")
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,10 @@ public class FileDO extends BaseDO {
|
||||||
* 关联 {@link FileConfigDO#getId()}
|
* 关联 {@link FileConfigDO#getId()}
|
||||||
*/
|
*/
|
||||||
private Long configId;
|
private Long configId;
|
||||||
|
/**
|
||||||
|
* 原文件名
|
||||||
|
*/
|
||||||
|
private String originalName;
|
||||||
/**
|
/**
|
||||||
* 路径,即文件名
|
* 路径,即文件名
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除文件
|
* 删除文件
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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);
|
||||||
// 校验数据
|
// 校验数据
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改手机
|
* 修改手机
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改密码
|
* 修改密码
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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());
|
||||||
|
|
Loading…
Reference in New Issue