revert: 头像不再存储为 base64,而是存储到文件管理中 (#142)

This commit is contained in:
luoqiz
2025-03-13 09:59:39 +08:00
committed by GitHub
parent 093d2d3c8a
commit 27cf46409a
6 changed files with 127 additions and 15 deletions

View File

@@ -29,6 +29,11 @@ public class ContainerConstants {
*/ */
public static final String USER_NICKNAME = "UserNickname"; public static final String USER_NICKNAME = "UserNickname";
/**
* 文件信息
*/
public static final String FILE_INFO = "FileInfo";
/** /**
* 用户角色 ID 列表 * 用户角色 ID 列表
*/ */

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.admin.common.constant;
import top.continew.starter.core.constant.StringConstants;
import java.time.LocalDate;
/**
* 系统文件相关常量
*
* @author luoqiz
* @since 2025/3/12 18:11
*/
public class SysFileConstants {
/**
* 用户头像路径前缀
*/
public static final String USER_AVATAR_PATH = "user/avatar/";
/**
* 默认文件路径
*/
public static String getDefaultFilePath() {
LocalDate today = LocalDate.now();
String path = today.getYear() + StringConstants.SLASH + today.getMonthValue() + StringConstants.SLASH + today
.getDayOfMonth() + StringConstants.SLASH;
return path;
}
private SysFileConstants() {
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.admin.system.config.container;
import cn.crane4j.core.container.Container;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import top.continew.admin.common.constant.ContainerConstants;
import top.continew.admin.system.model.entity.FileDO;
import top.continew.admin.system.service.FileService;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Component
@RequiredArgsConstructor
public class FileInfoContainer implements Container<Long> {
private final FileService fileService;
public String getNamespace() {
return ContainerConstants.FILE_INFO;
}
public Map<Long, FileDO> get(Collection<Long> ids) {
List<FileDO> list = fileService.listByIds(ids);
return list.stream().collect(Collectors.toMap(FileDO::getId, Function.identity()));
}
}

View File

@@ -18,6 +18,7 @@ package top.continew.admin.system.service;
import org.dromara.x.file.storage.core.FileInfo; import org.dromara.x.file.storage.core.FileInfo;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.common.constant.SysFileConstants;
import top.continew.admin.system.model.entity.FileDO; import top.continew.admin.system.model.entity.FileDO;
import top.continew.admin.system.model.query.FileQuery; import top.continew.admin.system.model.query.FileQuery;
import top.continew.admin.system.model.req.FileReq; import top.continew.admin.system.model.req.FileReq;
@@ -43,17 +44,28 @@ public interface FileService extends BaseService<FileResp, FileResp, FileQuery,
* @return 文件信息 * @return 文件信息
*/ */
default FileInfo upload(MultipartFile file) { default FileInfo upload(MultipartFile file) {
return upload(file, null); return upload(file, SysFileConstants.getDefaultFilePath(), null);
}
/**
* 上传到默认存储
*
* @param file 文件信息
* @return 文件信息
*/
default FileInfo upload(MultipartFile file, String path) {
return upload(file, path, null);
} }
/** /**
* 上传到指定存储 * 上传到指定存储
* *
* @param file 文件信息 * @param file 文件信息
* @param path 文件路径
* @param storageCode 存储编码 * @param storageCode 存储编码
* @return 文件信息 * @return 文件信息
*/ */
FileInfo upload(MultipartFile file, String storageCode); FileInfo upload(MultipartFile file, String path, String storageCode);
/** /**
* 根据存储 ID 列表查询 * 根据存储 ID 列表查询

View File

@@ -46,7 +46,6 @@ import top.continew.starter.core.util.URLUtils;
import top.continew.starter.core.validation.CheckUtils; import top.continew.starter.core.validation.CheckUtils;
import top.continew.starter.extension.crud.service.BaseServiceImpl; import top.continew.starter.extension.crud.service.BaseServiceImpl;
import java.time.LocalDate;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -80,7 +79,7 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
} }
@Override @Override
public FileInfo upload(MultipartFile file, String storageCode) { public FileInfo upload(MultipartFile file, String path, String storageCode) {
StorageDO storage; StorageDO storage;
if (StrUtil.isBlank(storageCode)) { if (StrUtil.isBlank(storageCode)) {
storage = storageService.getDefaultStorage(); storage = storageService.getDefaultStorage();
@@ -89,9 +88,6 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
storage = storageService.getByCode(storageCode); storage = storageService.getByCode(storageCode);
CheckUtils.throwIfNotExists(storage, "StorageDO", "Code", storageCode); CheckUtils.throwIfNotExists(storage, "StorageDO", "Code", storageCode);
} }
LocalDate today = LocalDate.now();
String path = today.getYear() + StringConstants.SLASH + today.getMonthValue() + StringConstants.SLASH + today
.getDayOfMonth() + StringConstants.SLASH;
UploadPretreatment uploadPretreatment = fileStorageService.of(file) UploadPretreatment uploadPretreatment = fileStorageService.of(file)
.setPlatform(storage.getCode()) .setPlatform(storage.getCode())
.setHashCalculatorMd5(true) .setHashCalculatorMd5(true)

View File

@@ -19,7 +19,6 @@ package top.continew.admin.system.service.impl;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.file.FileNameUtil; import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.io.resource.ResourceUtil; import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.UUID; import cn.hutool.core.lang.UUID;
@@ -48,19 +47,22 @@ import lombok.extern.slf4j.Slf4j;
import me.ahoo.cosid.IdGenerator; import me.ahoo.cosid.IdGenerator;
import me.ahoo.cosid.provider.DefaultIdGeneratorProvider; import me.ahoo.cosid.provider.DefaultIdGeneratorProvider;
import net.dreamlu.mica.core.result.R; import net.dreamlu.mica.core.result.R;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.auth.service.OnlineUserService; import top.continew.admin.auth.service.OnlineUserService;
import top.continew.admin.common.service.CommonUserService;
import top.continew.admin.common.constant.CacheConstants; import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.common.constant.SysConstants; import top.continew.admin.common.constant.SysConstants;
import top.continew.admin.common.constant.SysFileConstants;
import top.continew.admin.common.context.UserContext; import top.continew.admin.common.context.UserContext;
import top.continew.admin.common.context.UserContextHolder; import top.continew.admin.common.context.UserContextHolder;
import top.continew.admin.common.enums.DisEnableStatusEnum; import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.common.enums.GenderEnum; import top.continew.admin.common.enums.GenderEnum;
import top.continew.admin.common.service.CommonUserService;
import top.continew.admin.common.util.SecureUtils; import top.continew.admin.common.util.SecureUtils;
import top.continew.admin.system.enums.OptionCategoryEnum; import top.continew.admin.system.enums.OptionCategoryEnum;
import top.continew.admin.system.mapper.UserMapper; import top.continew.admin.system.mapper.UserMapper;
@@ -78,13 +80,13 @@ import top.continew.admin.system.service.*;
import top.continew.starter.cache.redisson.util.RedisUtils; import top.continew.starter.cache.redisson.util.RedisUtils;
import top.continew.starter.core.constant.StringConstants; import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.core.exception.BusinessException; import top.continew.starter.core.exception.BusinessException;
import top.continew.starter.core.util.SpringUtils;
import top.continew.starter.core.validation.CheckUtils; import top.continew.starter.core.validation.CheckUtils;
import top.continew.starter.extension.crud.model.query.PageQuery; import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.query.SortQuery; import top.continew.starter.extension.crud.model.query.SortQuery;
import top.continew.starter.extension.crud.model.resp.PageResp; import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.service.BaseServiceImpl; import top.continew.starter.extension.crud.service.BaseServiceImpl;
import top.continew.starter.web.util.FileUploadUtils; import top.continew.starter.web.util.FileUploadUtils;
import top.continew.starter.core.util.SpringUtils;
import java.io.IOException; import java.io.IOException;
import java.time.Duration; import java.time.Duration;
@@ -113,6 +115,8 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
private final OptionService optionService; private final OptionService optionService;
private final UserRoleService userRoleService; private final UserRoleService userRoleService;
private final RoleService roleService; private final RoleService roleService;
private final FileService fileService;
private final FileStorageService fileStorageService;
@Resource @Resource
private DeptService deptService; private DeptService deptService;
@@ -385,11 +389,11 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
String avatarImageType = FileNameUtil.extName(avatarFile.getOriginalFilename()); String avatarImageType = FileNameUtil.extName(avatarFile.getOriginalFilename());
CheckUtils.throwIf(!StrUtil.equalsAnyIgnoreCase(avatarImageType, avatarSupportSuffix), "头像仅支持 {} 格式的图片", String CheckUtils.throwIf(!StrUtil.equalsAnyIgnoreCase(avatarImageType, avatarSupportSuffix), "头像仅支持 {} 格式的图片", String
.join(StringConstants.CHINESE_COMMA, avatarSupportSuffix)); .join(StringConstants.CHINESE_COMMA, avatarSupportSuffix));
// 更新用户头像 FileInfo fileInfo = fileService.upload(avatarFile, SysFileConstants.USER_AVATAR_PATH);
String base64 = ImgUtil.toBase64DataUri(ImgUtil.scale(ImgUtil.toImage(avatarFile UserDO userInfo = getById(id);
.getBytes()), 100, 100, null), avatarImageType); fileStorageService.delete(userInfo.getAvatar());
baseMapper.lambdaUpdate().set(UserDO::getAvatar, base64).eq(UserDO::getId, id).update(); baseMapper.lambdaUpdate().set(UserDO::getAvatar, fileInfo.getUrl()).eq(UserDO::getId, id).update();
return base64; return fileInfo.getUrl();
} }
@Override @Override