refactor(system/file): 优化文件相关代码

This commit is contained in:
2025-03-13 21:13:38 +08:00
parent 27cf46409a
commit 74c4270323
6 changed files with 46 additions and 60 deletions

View File

@@ -1,48 +0,0 @@
/*
* 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

@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package top.continew.admin.system.config.container; package top.continew.admin.system.config.file;
import cn.crane4j.core.container.Container; import cn.crane4j.core.container.Container;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -29,19 +29,26 @@ import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/**
* 文件信息填充容器
*
* @author luoqiz
* @since 2025/3/12 18:11
*/
@Component @Component
@RequiredArgsConstructor @RequiredArgsConstructor
public class FileInfoContainer implements Container<Long> { public class FileInfoContainer implements Container<Long> {
private final FileService fileService; private final FileService fileService;
@Override
public String getNamespace() { public String getNamespace() {
return ContainerConstants.FILE_INFO; return ContainerConstants.FILE_INFO;
} }
@Override
public Map<Long, FileDO> get(Collection<Long> ids) { public Map<Long, FileDO> get(Collection<Long> ids) {
List<FileDO> list = fileService.listByIds(ids); List<FileDO> list = fileService.listByIds(ids);
return list.stream().collect(Collectors.toMap(FileDO::getId, Function.identity())); return list.stream().collect(Collectors.toMap(FileDO::getId, Function.identity()));
} }
} }

View File

@@ -18,15 +18,16 @@ 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;
import top.continew.admin.system.model.resp.file.FileResp; import top.continew.admin.system.model.resp.file.FileResp;
import top.continew.admin.system.model.resp.file.FileStatisticsResp; import top.continew.admin.system.model.resp.file.FileStatisticsResp;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.data.mp.service.IService; import top.continew.starter.data.mp.service.IService;
import top.continew.starter.extension.crud.service.BaseService; import top.continew.starter.extension.crud.service.BaseService;
import java.time.LocalDate;
import java.util.List; import java.util.List;
/** /**
@@ -44,13 +45,14 @@ public interface FileService extends BaseService<FileResp, FileResp, FileQuery,
* @return 文件信息 * @return 文件信息
*/ */
default FileInfo upload(MultipartFile file) { default FileInfo upload(MultipartFile file) {
return upload(file, SysFileConstants.getDefaultFilePath(), null); return upload(file, getDefaultFilePath(), null);
} }
/** /**
* 上传到默认存储 * 上传到默认存储
* *
* @param file 文件信息 * @param file 文件信息
* @param path 文件路径
* @return 文件信息 * @return 文件信息
*/ */
default FileInfo upload(MultipartFile file, String path) { default FileInfo upload(MultipartFile file, String path) {
@@ -81,4 +83,15 @@ public interface FileService extends BaseService<FileResp, FileResp, FileQuery,
* @return 资源统计信息 * @return 资源统计信息
*/ */
FileStatisticsResp statistics(); FileStatisticsResp statistics();
/**
* 获取默认文件路径
*
* @return 默认文件路径
*/
default String getDefaultFilePath() {
LocalDate today = LocalDate.now();
return today.getYear() + StringConstants.SLASH + today.getMonthValue() + StringConstants.SLASH + today
.getDayOfMonth() + StringConstants.SLASH;
}
} }

View File

@@ -57,7 +57,6 @@ 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.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;
@@ -122,6 +121,8 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
private DeptService deptService; private DeptService deptService;
@Value("${avatar.support-suffix}") @Value("${avatar.support-suffix}")
private String[] avatarSupportSuffix; private String[] avatarSupportSuffix;
@Value("${avatar.path}")
private String avatarPath;
@Override @Override
public PageResp<UserResp> page(UserQuery query, PageQuery pageQuery) { public PageResp<UserResp> page(UserQuery query, PageQuery pageQuery) {
@@ -389,11 +390,18 @@ 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); // 上传新头像
UserDO userInfo = getById(id); UserDO user = super.getById(id);
fileStorageService.delete(userInfo.getAvatar()); FileInfo fileInfo = fileService.upload(avatarFile, avatarPath);
baseMapper.lambdaUpdate().set(UserDO::getAvatar, fileInfo.getUrl()).eq(UserDO::getId, id).update(); // 更新用户头像
return fileInfo.getUrl(); String newAvatar = fileInfo.getUrl();
baseMapper.lambdaUpdate().set(UserDO::getAvatar, newAvatar).eq(UserDO::getId, id).update();
// 删除原头像
String oldAvatar = user.getAvatar();
if (StrUtil.isNotBlank(oldAvatar)) {
fileStorageService.delete(oldAvatar);
}
return newAvatar;
} }
@Override @Override

View File

@@ -263,8 +263,11 @@ spring.servlet:
max-file-size: 10MB max-file-size: 10MB
# 单次总上传文件大小限制 # 单次总上传文件大小限制
max-request-size: 20MB max-request-size: 20MB
## 头像支持格式配置 ## 头像配置
avatar: avatar:
# 存储路径
path: user/avatar/
# 支持的后缀
support-suffix: jpg,jpeg,png,gif support-suffix: jpg,jpeg,png,gif
--- ### Snail Job 配置 --- ### Snail Job 配置

View File

@@ -260,8 +260,11 @@ spring.servlet:
max-file-size: 10MB max-file-size: 10MB
# 单次总上传文件大小限制 # 单次总上传文件大小限制
max-request-size: 20MB max-request-size: 20MB
## 头像支持格式配置 ## 头像配置
avatar: avatar:
# 存储路径
path: user/avatar/
# 支持的后缀
support-suffix: jpg,jpeg,png,gif support-suffix: jpg,jpeg,png,gif
--- ### Snail Job 配置 --- ### Snail Job 配置