mirror of https://gitee.com/y_project/RuoYi.git
用户头像更换后移除旧头像文件
parent
4c74687ca2
commit
bcbb4ac994
|
@ -16,7 +16,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||
import org.springframework.web.multipart.MultipartFile;
|
||||
import com.ruoyi.common.config.RuoYiConfig;
|
||||
import com.ruoyi.common.config.ServerConfig;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.file.FileUploadUtils;
|
||||
|
@ -150,7 +149,7 @@ public class CommonController
|
|||
// 本地资源路径
|
||||
String localPath = RuoYiConfig.getProfile();
|
||||
// 数据库资源地址
|
||||
String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
|
||||
String downloadPath = localPath + FileUtils.stripPrefix(resource);
|
||||
// 下载名称
|
||||
String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
|
||||
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
||||
|
|
|
@ -21,6 +21,7 @@ import com.ruoyi.common.utils.DateUtils;
|
|||
import com.ruoyi.common.utils.ShiroUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.file.FileUploadUtils;
|
||||
import com.ruoyi.common.utils.file.FileUtils;
|
||||
import com.ruoyi.common.utils.file.MimeTypeUtils;
|
||||
import com.ruoyi.framework.shiro.service.SysPasswordService;
|
||||
import com.ruoyi.system.service.ISysUserService;
|
||||
|
@ -162,11 +163,16 @@ public class SysProfileController extends BaseController
|
|||
{
|
||||
if (!file.isEmpty())
|
||||
{
|
||||
String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
|
||||
currentUser.setAvatar(avatar);
|
||||
if (userService.updateUserInfo(currentUser) > 0)
|
||||
String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION, true);
|
||||
if (userService.updateUserAvatar(currentUser.getUserId(), avatar))
|
||||
{
|
||||
setSysUser(userService.selectUserById(currentUser.getUserId()));
|
||||
String oldAvatar = currentUser.getAvatar();
|
||||
if (StringUtils.isNotEmpty(oldAvatar))
|
||||
{
|
||||
FileUtils.deleteFile(RuoYiConfig.getProfile() + FileUtils.stripPrefix(oldAvatar));
|
||||
}
|
||||
currentUser.setAvatar(avatar);
|
||||
setSysUser(currentUser);
|
||||
return success();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
|
|||
import com.ruoyi.common.exception.file.InvalidExtensionException;
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.uuid.IdUtils;
|
||||
import com.ruoyi.common.utils.uuid.Seq;
|
||||
|
||||
/**
|
||||
|
@ -101,6 +102,26 @@ public class FileUploadUtils
|
|||
public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
|
||||
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
|
||||
InvalidExtensionException
|
||||
{
|
||||
return upload(baseDir, file, allowedExtension, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param baseDir 相对应用的基目录
|
||||
* @param file 上传的文件
|
||||
* @param useCustomNaming 系统自定义文件名
|
||||
* @param allowedExtension 上传文件类型
|
||||
* @return 返回上传成功的文件名
|
||||
* @throws FileSizeLimitExceededException 如果超出最大大小
|
||||
* @throws FileNameLengthLimitExceededException 文件名太长
|
||||
* @throws IOException 比如读写文件出错时
|
||||
* @throws InvalidExtensionException 文件校验异常
|
||||
*/
|
||||
public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension, boolean useCustomNaming)
|
||||
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
|
||||
InvalidExtensionException
|
||||
{
|
||||
int fileNameLength = Objects.requireNonNull(file.getOriginalFilename()).length();
|
||||
if (fileNameLength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
|
||||
|
@ -110,7 +131,7 @@ public class FileUploadUtils
|
|||
|
||||
assertAllowed(file, allowedExtension);
|
||||
|
||||
String fileName = extractFilename(file);
|
||||
String fileName = useCustomNaming ? uuidFilename(file) : extractFilename(file);
|
||||
|
||||
String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
|
||||
file.transferTo(Paths.get(absPath));
|
||||
|
@ -118,12 +139,19 @@ public class FileUploadUtils
|
|||
}
|
||||
|
||||
/**
|
||||
* 编码文件名
|
||||
* 编码文件名(日期格式目录 + 原文件名 + 序列值 + 后缀)
|
||||
*/
|
||||
public static final String extractFilename(MultipartFile file)
|
||||
{
|
||||
return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
|
||||
FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
|
||||
return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(), FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
|
||||
}
|
||||
|
||||
/**
|
||||
* 编编码文件名(日期格式目录 + UUID + 后缀)
|
||||
*/
|
||||
public static final String uuidFilename(MultipartFile file)
|
||||
{
|
||||
return StringUtils.format("{}/{}.{}", DateUtils.datePath(), IdUtils.fastSimpleUUID(), getExtension(file));
|
||||
}
|
||||
|
||||
public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
|
||||
|
@ -193,6 +221,7 @@ public class FileUploadUtils
|
|||
throw new InvalidExtensionException(allowedExtension, extension, fileName);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -15,6 +15,7 @@ import org.apache.commons.io.FilenameUtils;
|
|||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import com.ruoyi.common.config.RuoYiConfig;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.uuid.IdUtils;
|
||||
|
@ -103,6 +104,17 @@ public class FileUtils
|
|||
return FileUploadUtils.getPathFileName(uploadDir, pathName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除路径中的请求前缀片段
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @return 移除后的文件路径
|
||||
*/
|
||||
public static String stripPrefix(String filePath)
|
||||
{
|
||||
return StringUtils.substringAfter(filePath, Constants.RESOURCE_PREFIX);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
|
@ -288,4 +300,3 @@ public class FileUtils
|
|||
return baseName;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.ruoyi.system.mapper;
|
||||
|
||||
import java.util.List;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||
|
||||
/**
|
||||
|
@ -90,6 +91,15 @@ public interface SysUserMapper
|
|||
*/
|
||||
public int updateUser(SysUser user);
|
||||
|
||||
/**
|
||||
* 修改用户头像
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param avatar 头像地址
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateUserAvatar(@Param("userId") Long userId, @Param("avatar") String avatar);
|
||||
|
||||
/**
|
||||
* 新增用户信息
|
||||
*
|
||||
|
|
|
@ -124,6 +124,15 @@ public interface ISysUserService
|
|||
*/
|
||||
public int updateUserInfo(SysUser user);
|
||||
|
||||
/**
|
||||
* 修改用户头像
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param avatar 头像地址
|
||||
* @return 结果
|
||||
*/
|
||||
public boolean updateUserAvatar(Long userId, String avatar);
|
||||
|
||||
/**
|
||||
* 用户授权角色
|
||||
*
|
||||
|
|
|
@ -275,6 +275,18 @@ public class SysUserServiceImpl implements ISysUserService
|
|||
return userMapper.updateUser(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改用户头像
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param avatar 头像地址
|
||||
* @return 结果
|
||||
*/
|
||||
public boolean updateUserAvatar(Long userId, String avatar)
|
||||
{
|
||||
return userMapper.updateUserAvatar(userId, avatar) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户授权角色
|
||||
*
|
||||
|
|
|
@ -166,6 +166,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
</foreach>
|
||||
</delete>
|
||||
|
||||
<update id="updateUserAvatar" parameterType="SysUser">
|
||||
update sys_user set avatar = #{avatar} where user_id = #{userId}
|
||||
</update>
|
||||
|
||||
<update id="updateUser" parameterType="SysUser">
|
||||
update sys_user
|
||||
<set>
|
||||
|
|
Loading…
Reference in New Issue