feat(修改): 修改用户上传头像逻辑
This commit is contained in:
parent
149b44640e
commit
f73a3e539a
|
@ -1,7 +1,7 @@
|
||||||
package cn.bunny.common.service.utils.minio;
|
package cn.bunny.common.service.utils.minio;
|
||||||
|
|
||||||
import cn.bunny.common.service.exception.BunnyException;
|
import cn.bunny.common.service.exception.BunnyException;
|
||||||
import cn.bunny.dao.pojo.common.MinioFIlePath;
|
import cn.bunny.dao.pojo.common.MinioFilePath;
|
||||||
import cn.bunny.dao.pojo.constant.MinioConstant;
|
import cn.bunny.dao.pojo.constant.MinioConstant;
|
||||||
import cn.bunny.dao.pojo.result.ResultCodeEnum;
|
import cn.bunny.dao.pojo.result.ResultCodeEnum;
|
||||||
import io.minio.GetObjectArgs;
|
import io.minio.GetObjectArgs;
|
||||||
|
@ -35,7 +35,7 @@ public class MinioUtil {
|
||||||
/**
|
/**
|
||||||
* 获取Minio文件路径
|
* 获取Minio文件路径
|
||||||
*/
|
*/
|
||||||
public static MinioFIlePath getMinioFilePath(String buckName, String minioPreType, MultipartFile file) {
|
public static MinioFilePath getMinioFilePath(String buckName, String minioPreType, MultipartFile file) {
|
||||||
String uuid = UUID.randomUUID().toString();
|
String uuid = UUID.randomUUID().toString();
|
||||||
// 定义日期时间格式
|
// 定义日期时间格式
|
||||||
LocalDateTime currentDateTime = LocalDateTime.now();
|
LocalDateTime currentDateTime = LocalDateTime.now();
|
||||||
|
@ -61,7 +61,7 @@ public class MinioUtil {
|
||||||
String buckNameFilepath = "/" + buckName + MinioConstant.getType(minioPreType) + timeUuidFilename;
|
String buckNameFilepath = "/" + buckName + MinioConstant.getType(minioPreType) + timeUuidFilename;
|
||||||
|
|
||||||
// 设置及Minio基础信息
|
// 设置及Minio基础信息
|
||||||
MinioFIlePath minioFIlePath = new MinioFIlePath();
|
MinioFilePath minioFIlePath = new MinioFilePath();
|
||||||
minioFIlePath.setFilename(filename);
|
minioFIlePath.setFilename(filename);
|
||||||
minioFIlePath.setUuidFilename(uuidFilename);
|
minioFIlePath.setUuidFilename(uuidFilename);
|
||||||
minioFIlePath.setTimeUuidFilename(timeUuidFilename);
|
minioFIlePath.setTimeUuidFilename(timeUuidFilename);
|
||||||
|
@ -74,11 +74,11 @@ public class MinioUtil {
|
||||||
/**
|
/**
|
||||||
* * 上传文件并返回处理信息
|
* * 上传文件并返回处理信息
|
||||||
*/
|
*/
|
||||||
public MinioFIlePath getUploadMinioObjectFilePath(MultipartFile file, String minioPreType) throws IOException {
|
public MinioFilePath getUploadMinioObjectFilePath(MultipartFile file, String minioPreType) throws IOException {
|
||||||
// 如果buckName为空,设置为默认的桶
|
// 如果buckName为空,设置为默认的桶
|
||||||
String bucketName = properties.getBucketName();
|
String bucketName = properties.getBucketName();
|
||||||
if (file != null) {
|
if (file != null) {
|
||||||
MinioFIlePath minioFile = getMinioFilePath(bucketName, minioPreType, file);
|
MinioFilePath minioFile = getMinioFilePath(bucketName, minioPreType, file);
|
||||||
String filepath = minioFile.getFilepath();
|
String filepath = minioFile.getFilepath();
|
||||||
|
|
||||||
// 上传对象
|
// 上传对象
|
||||||
|
|
|
@ -34,9 +34,6 @@ public class AdminUserUpdateDto {
|
||||||
@Schema(name = "phone", title = "手机号")
|
@Schema(name = "phone", title = "手机号")
|
||||||
private String phone;
|
private String phone;
|
||||||
|
|
||||||
@Schema(name = "avatar", title = "头像")
|
|
||||||
private String avatar;
|
|
||||||
|
|
||||||
@Schema(name = "sex", title = "性别", description = "0:女 1:男")
|
@Schema(name = "sex", title = "性别", description = "0:女 1:男")
|
||||||
private Byte sex;
|
private Byte sex;
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
package cn.bunny.dao.dto.system.user;
|
package cn.bunny.dao.dto.system.user;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
|
||||||
import jakarta.validation.constraints.NotEmpty;
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@ -21,8 +20,7 @@ public class UserUpdateWithAvatarDto {
|
||||||
private Long userId;
|
private Long userId;
|
||||||
|
|
||||||
@Schema(name = "avatar", title = "用户头像")
|
@Schema(name = "avatar", title = "用户头像")
|
||||||
@NotBlank(message = "用户头像不能为空")
|
@NotNull(message = "用户头像不能为空")
|
||||||
@NotEmpty
|
private MultipartFile avatar;
|
||||||
private String avatar;
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -9,7 +9,7 @@ import lombok.NoArgsConstructor;
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@Builder
|
@Builder
|
||||||
public class MinioFIlePath {
|
public class MinioFilePath {
|
||||||
private String filename;
|
private String filename;
|
||||||
private String uuidFilename;
|
private String uuidFilename;
|
||||||
private String timeUuidFilename;
|
private String timeUuidFilename;
|
|
@ -0,0 +1,72 @@
|
||||||
|
package cn.bunny.services.factory;
|
||||||
|
|
||||||
|
import cn.bunny.common.service.context.BaseContext;
|
||||||
|
import cn.bunny.common.service.exception.BunnyException;
|
||||||
|
import cn.bunny.common.service.utils.FileUtil;
|
||||||
|
import cn.bunny.common.service.utils.minio.MinioUtil;
|
||||||
|
import cn.bunny.dao.entity.system.Files;
|
||||||
|
import cn.bunny.dao.pojo.common.MinioFilePath;
|
||||||
|
import cn.bunny.dao.pojo.result.ResultCodeEnum;
|
||||||
|
import cn.bunny.dao.vo.system.files.FileInfoVo;
|
||||||
|
import cn.bunny.services.mapper.FilesMapper;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class FileFactory {
|
||||||
|
@Autowired
|
||||||
|
private FilesMapper filesMapper;
|
||||||
|
@Value("${spring.servlet.multipart.max-file-size}")
|
||||||
|
private String maxFileSize;
|
||||||
|
@Autowired
|
||||||
|
private MinioUtil minioUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
*
|
||||||
|
* @param file 文件
|
||||||
|
* @param type 文件类型(MinioConstant)
|
||||||
|
* @return 返回文件信息
|
||||||
|
*/
|
||||||
|
@SneakyThrows
|
||||||
|
public FileInfoVo uploadFile(MultipartFile file, String type) {
|
||||||
|
// 管理员Id
|
||||||
|
Long userId = BaseContext.getUserId();
|
||||||
|
// 文件大小
|
||||||
|
long fileSize = file.getSize();
|
||||||
|
// 文件类型
|
||||||
|
String contentType = file.getContentType();
|
||||||
|
// 文件名
|
||||||
|
String filename = file.getOriginalFilename();
|
||||||
|
|
||||||
|
// 上传文件
|
||||||
|
MinioFilePath minioFIlePath = minioUtil.getUploadMinioObjectFilePath(file, type);
|
||||||
|
String bucketNameFilepath = minioFIlePath.getBucketNameFilepath();
|
||||||
|
|
||||||
|
// 盘读研数据是否过大
|
||||||
|
String mb = maxFileSize.replace("MB", "");
|
||||||
|
if (fileSize / 1024 / 1024 > Long.parseLong(mb)) throw new BunnyException(ResultCodeEnum.DATA_TOO_LARGE);
|
||||||
|
|
||||||
|
// 插入文件信息
|
||||||
|
Files adminFiles = new Files();
|
||||||
|
adminFiles.setFileSize(fileSize);
|
||||||
|
adminFiles.setFileType(contentType);
|
||||||
|
adminFiles.setFilename(filename);
|
||||||
|
adminFiles.setFilepath(bucketNameFilepath);
|
||||||
|
adminFiles.setCreateUser(userId);
|
||||||
|
filesMapper.insert(adminFiles);
|
||||||
|
|
||||||
|
// 返回信息内容化
|
||||||
|
return FileInfoVo.builder()
|
||||||
|
.size(FileUtil.getSize(fileSize))
|
||||||
|
.filepath(bucketNameFilepath)
|
||||||
|
.fileSize(fileSize)
|
||||||
|
.fileType(contentType)
|
||||||
|
.filename(filename)
|
||||||
|
.url(minioUtil.getObjectNameFullPath(bucketNameFilepath))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,20 +1,14 @@
|
||||||
package cn.bunny.services.service.impl;
|
package cn.bunny.services.service.impl;
|
||||||
|
|
||||||
import cn.bunny.common.service.context.BaseContext;
|
|
||||||
import cn.bunny.common.service.exception.BunnyException;
|
|
||||||
import cn.bunny.common.service.utils.FileUtil;
|
|
||||||
import cn.bunny.common.service.utils.minio.MinioUtil;
|
|
||||||
import cn.bunny.dao.dto.system.files.FileUploadDto;
|
import cn.bunny.dao.dto.system.files.FileUploadDto;
|
||||||
import cn.bunny.dao.entity.system.Files;
|
import cn.bunny.dao.entity.system.Files;
|
||||||
import cn.bunny.dao.pojo.common.MinioFIlePath;
|
|
||||||
import cn.bunny.dao.pojo.result.ResultCodeEnum;
|
|
||||||
import cn.bunny.dao.vo.system.files.FileInfoVo;
|
import cn.bunny.dao.vo.system.files.FileInfoVo;
|
||||||
|
import cn.bunny.services.factory.FileFactory;
|
||||||
import cn.bunny.services.mapper.FilesMapper;
|
import cn.bunny.services.mapper.FilesMapper;
|
||||||
import cn.bunny.services.service.FilesService;
|
import cn.bunny.services.service.FilesService;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
@ -30,10 +24,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||||
public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements FilesService {
|
public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements FilesService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MinioUtil minioUtil;
|
private FileFactory fileFactory;
|
||||||
|
|
||||||
@Value("${spring.servlet.multipart.max-file-size}")
|
|
||||||
private String maxFileSize;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* * 上传文件
|
* * 上传文件
|
||||||
|
@ -47,40 +38,6 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
|
||||||
MultipartFile file = dto.getFile();
|
MultipartFile file = dto.getFile();
|
||||||
String type = dto.getType();
|
String type = dto.getType();
|
||||||
|
|
||||||
// 管理员Id
|
return fileFactory.uploadFile(file, type);
|
||||||
Long userId = BaseContext.getUserId();
|
|
||||||
// 文件大小
|
|
||||||
long fileSize = file.getSize();
|
|
||||||
// 文件类型
|
|
||||||
String contentType = file.getContentType();
|
|
||||||
// 文件名
|
|
||||||
String filename = file.getOriginalFilename();
|
|
||||||
|
|
||||||
// 上传文件
|
|
||||||
MinioFIlePath minioFIlePath = minioUtil.getUploadMinioObjectFilePath(file, type);
|
|
||||||
String bucketNameFilepath = minioFIlePath.getBucketNameFilepath();
|
|
||||||
|
|
||||||
// 盘读研数据是否过大
|
|
||||||
String mb = maxFileSize.replace("MB", "");
|
|
||||||
if (fileSize / 1024 / 1024 > Long.parseLong(mb)) throw new BunnyException(ResultCodeEnum.DATA_TOO_LARGE);
|
|
||||||
|
|
||||||
// 插入文件信息
|
|
||||||
Files adminFiles = new Files();
|
|
||||||
adminFiles.setFileSize(fileSize);
|
|
||||||
adminFiles.setFileType(contentType);
|
|
||||||
adminFiles.setFilename(filename);
|
|
||||||
adminFiles.setFilepath(bucketNameFilepath);
|
|
||||||
adminFiles.setCreateUser(userId);
|
|
||||||
save(adminFiles);
|
|
||||||
|
|
||||||
// 返回信息内容化
|
|
||||||
return FileInfoVo.builder()
|
|
||||||
.size(FileUtil.getSize(fileSize))
|
|
||||||
.filepath(bucketNameFilepath)
|
|
||||||
.fileSize(fileSize)
|
|
||||||
.fileType(contentType)
|
|
||||||
.filename(filename)
|
|
||||||
.url(minioUtil.getObjectNameFullPath(bucketNameFilepath))
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,13 +4,16 @@ import cn.bunny.common.service.context.BaseContext;
|
||||||
import cn.bunny.common.service.exception.BunnyException;
|
import cn.bunny.common.service.exception.BunnyException;
|
||||||
import cn.bunny.common.service.utils.JwtHelper;
|
import cn.bunny.common.service.utils.JwtHelper;
|
||||||
import cn.bunny.common.service.utils.minio.MinioUtil;
|
import cn.bunny.common.service.utils.minio.MinioUtil;
|
||||||
|
import cn.bunny.dao.dto.system.files.FileUploadDto;
|
||||||
import cn.bunny.dao.dto.system.user.*;
|
import cn.bunny.dao.dto.system.user.*;
|
||||||
import cn.bunny.dao.entity.system.AdminUser;
|
import cn.bunny.dao.entity.system.AdminUser;
|
||||||
import cn.bunny.dao.entity.system.EmailUsers;
|
import cn.bunny.dao.entity.system.EmailUsers;
|
||||||
import cn.bunny.dao.pojo.common.EmailSendInit;
|
import cn.bunny.dao.pojo.common.EmailSendInit;
|
||||||
|
import cn.bunny.dao.pojo.constant.MinioConstant;
|
||||||
import cn.bunny.dao.pojo.constant.RedisUserConstant;
|
import cn.bunny.dao.pojo.constant.RedisUserConstant;
|
||||||
import cn.bunny.dao.pojo.result.PageResult;
|
import cn.bunny.dao.pojo.result.PageResult;
|
||||||
import cn.bunny.dao.pojo.result.ResultCodeEnum;
|
import cn.bunny.dao.pojo.result.ResultCodeEnum;
|
||||||
|
import cn.bunny.dao.vo.system.files.FileInfoVo;
|
||||||
import cn.bunny.dao.vo.system.user.AdminUserVo;
|
import cn.bunny.dao.vo.system.user.AdminUserVo;
|
||||||
import cn.bunny.dao.vo.system.user.LoginVo;
|
import cn.bunny.dao.vo.system.user.LoginVo;
|
||||||
import cn.bunny.dao.vo.system.user.RefreshTokenVo;
|
import cn.bunny.dao.vo.system.user.RefreshTokenVo;
|
||||||
|
@ -19,12 +22,14 @@ import cn.bunny.services.factory.EmailFactory;
|
||||||
import cn.bunny.services.factory.UserFactory;
|
import cn.bunny.services.factory.UserFactory;
|
||||||
import cn.bunny.services.mapper.EmailUsersMapper;
|
import cn.bunny.services.mapper.EmailUsersMapper;
|
||||||
import cn.bunny.services.mapper.UserMapper;
|
import cn.bunny.services.mapper.UserMapper;
|
||||||
|
import cn.bunny.services.service.FilesService;
|
||||||
import cn.bunny.services.service.UserService;
|
import cn.bunny.services.service.UserService;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -33,6 +38,7 @@ import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.DigestUtils;
|
import org.springframework.util.DigestUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -62,6 +68,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, AdminUser> implemen
|
||||||
@Autowired
|
@Autowired
|
||||||
private MinioUtil minioUtil;
|
private MinioUtil minioUtil;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private FilesService filesService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登录发送邮件验证码
|
* 登录发送邮件验证码
|
||||||
*
|
*
|
||||||
|
@ -170,19 +179,24 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, AdminUser> implemen
|
||||||
*
|
*
|
||||||
* @param dto 管理员用户修改头像
|
* @param dto 管理员用户修改头像
|
||||||
*/
|
*/
|
||||||
|
@SneakyThrows
|
||||||
@Override
|
@Override
|
||||||
public void uploadAvatarByAdmin(UserUpdateWithAvatarDto dto) {
|
public void uploadAvatarByAdmin(UserUpdateWithAvatarDto dto) {
|
||||||
String avatar = dto.getAvatar();
|
MultipartFile avatar = dto.getAvatar();
|
||||||
Long userId = dto.getUserId();
|
Long userId = dto.getUserId();
|
||||||
|
|
||||||
// 判断是否存在这个用户
|
// 判断是否存在这个用户
|
||||||
AdminUser adminUser = getOne(Wrappers.<AdminUser>lambdaQuery().eq(AdminUser::getId, userId));
|
AdminUser adminUser = getOne(Wrappers.<AdminUser>lambdaQuery().eq(AdminUser::getId, userId));
|
||||||
if (adminUser == null) throw new BunnyException(ResultCodeEnum.USER_IS_EMPTY);
|
if (adminUser == null) throw new BunnyException(ResultCodeEnum.USER_IS_EMPTY);
|
||||||
|
|
||||||
|
// 上传头像
|
||||||
|
FileUploadDto uploadDto = FileUploadDto.builder().file(avatar).type(MinioConstant.avatar).build();
|
||||||
|
FileInfoVo fileInfoVo = filesService.upload(uploadDto);
|
||||||
|
|
||||||
// 更新用户
|
// 更新用户
|
||||||
adminUser = new AdminUser();
|
adminUser = new AdminUser();
|
||||||
adminUser.setId(userId);
|
adminUser.setId(userId);
|
||||||
adminUser.setAvatar(avatar);
|
adminUser.setAvatar(fileInfoVo.getFilepath());
|
||||||
updateById(adminUser);
|
updateById(adminUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue