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

@@ -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.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.query.FileQuery;
import top.continew.admin.system.model.req.FileReq;
@@ -43,17 +44,28 @@ public interface FileService extends BaseService<FileResp, FileResp, FileQuery,
* @return 文件信息
*/
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 path 文件路径
* @param storageCode 存储编码
* @return 文件信息
*/
FileInfo upload(MultipartFile file, String storageCode);
FileInfo upload(MultipartFile file, String path, String storageCode);
/**
* 根据存储 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.extension.crud.service.BaseServiceImpl;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -80,7 +79,7 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
}
@Override
public FileInfo upload(MultipartFile file, String storageCode) {
public FileInfo upload(MultipartFile file, String path, String storageCode) {
StorageDO storage;
if (StrUtil.isBlank(storageCode)) {
storage = storageService.getDefaultStorage();
@@ -89,9 +88,6 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
storage = storageService.getByCode(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)
.setPlatform(storage.getCode())
.setHashCalculatorMd5(true)

View File

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