Skip to content

Commit a35db3d

Browse files
committed
refactor: 头像存储到文件管理中
1 parent de9c9c5 commit a35db3d

File tree

6 files changed

+127
-15
lines changed

6 files changed

+127
-15
lines changed

continew-common/src/main/java/top/continew/admin/common/constant/ContainerConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public class ContainerConstants {
2929
*/
3030
public static final String USER_NICKNAME = "UserNickname";
3131

32+
/**
33+
* 文件信息
34+
*/
35+
public static final String FILE_INFO = "FileInfo";
36+
3237
/**
3338
* 用户角色 ID 列表
3439
*/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.admin.common.constant;
18+
19+
import top.continew.starter.core.constant.StringConstants;
20+
21+
import java.time.LocalDate;
22+
23+
/**
24+
* 系统文件相关常量
25+
*
26+
* @author luoqiz
27+
* @since 2025/3/12 18:11
28+
*/
29+
public class SysFileConstants {
30+
31+
/**
32+
* 用户头像路径前缀
33+
*/
34+
public static final String USER_AVATAR_PATH = "user/avatar/";
35+
36+
/**
37+
* 默认文件路径
38+
*/
39+
public static String getDefaultFilePath() {
40+
LocalDate today = LocalDate.now();
41+
String path = today.getYear() + StringConstants.SLASH + today.getMonthValue() + StringConstants.SLASH + today
42+
.getDayOfMonth() + StringConstants.SLASH;
43+
return path;
44+
}
45+
46+
private SysFileConstants() {
47+
}
48+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.admin.system.config.container;
18+
19+
import cn.crane4j.core.container.Container;
20+
import lombok.RequiredArgsConstructor;
21+
import org.springframework.stereotype.Component;
22+
import top.continew.admin.common.constant.ContainerConstants;
23+
import top.continew.admin.system.model.entity.FileDO;
24+
import top.continew.admin.system.service.FileService;
25+
26+
import java.util.Collection;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.function.Function;
30+
import java.util.stream.Collectors;
31+
32+
@Component
33+
@RequiredArgsConstructor
34+
public class FileInfoContainer implements Container<Long> {
35+
36+
private final FileService fileService;
37+
38+
public String getNamespace() {
39+
return ContainerConstants.FILE_INFO;
40+
}
41+
42+
public Map<Long, FileDO> get(Collection<Long> ids) {
43+
List<FileDO> list = fileService.listByIds(ids);
44+
return list.stream().collect(Collectors.toMap(FileDO::getId, Function.identity()));
45+
}
46+
47+
}

continew-module-system/src/main/java/top/continew/admin/system/service/FileService.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.dromara.x.file.storage.core.FileInfo;
2020
import org.springframework.web.multipart.MultipartFile;
21+
import top.continew.admin.common.constant.SysFileConstants;
2122
import top.continew.admin.system.model.entity.FileDO;
2223
import top.continew.admin.system.model.query.FileQuery;
2324
import top.continew.admin.system.model.req.FileReq;
@@ -43,17 +44,28 @@ public interface FileService extends BaseService<FileResp, FileResp, FileQuery,
4344
* @return 文件信息
4445
*/
4546
default FileInfo upload(MultipartFile file) {
46-
return upload(file, null);
47+
return upload(file, SysFileConstants.getDefaultFilePath(), null);
48+
}
49+
50+
/**
51+
* 上传到默认存储
52+
*
53+
* @param file 文件信息
54+
* @return 文件信息
55+
*/
56+
default FileInfo upload(MultipartFile file, String path) {
57+
return upload(file, path, null);
4758
}
4859

4960
/**
5061
* 上传到指定存储
5162
*
5263
* @param file 文件信息
64+
* @param path 文件路径
5365
* @param storageCode 存储编码
5466
* @return 文件信息
5567
*/
56-
FileInfo upload(MultipartFile file, String storageCode);
68+
FileInfo upload(MultipartFile file, String path, String storageCode);
5769

5870
/**
5971
* 根据存储 ID 列表查询

continew-module-system/src/main/java/top/continew/admin/system/service/impl/FileServiceImpl.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import top.continew.starter.core.validation.CheckUtils;
4747
import top.continew.starter.extension.crud.service.BaseServiceImpl;
4848

49-
import java.time.LocalDate;
5049
import java.util.List;
5150
import java.util.Map;
5251
import java.util.stream.Collectors;
@@ -80,7 +79,7 @@ protected void beforeDelete(List<Long> ids) {
8079
}
8180

8281
@Override
83-
public FileInfo upload(MultipartFile file, String storageCode) {
82+
public FileInfo upload(MultipartFile file, String path, String storageCode) {
8483
StorageDO storage;
8584
if (StrUtil.isBlank(storageCode)) {
8685
storage = storageService.getDefaultStorage();
@@ -89,9 +88,6 @@ public FileInfo upload(MultipartFile file, String storageCode) {
8988
storage = storageService.getByCode(storageCode);
9089
CheckUtils.throwIfNotExists(storage, "StorageDO", "Code", storageCode);
9190
}
92-
LocalDate today = LocalDate.now();
93-
String path = today.getYear() + StringConstants.SLASH + today.getMonthValue() + StringConstants.SLASH + today
94-
.getDayOfMonth() + StringConstants.SLASH;
9591
UploadPretreatment uploadPretreatment = fileStorageService.of(file)
9692
.setPlatform(storage.getCode())
9793
.setHashCalculatorMd5(true)

continew-module-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import cn.dev33.satoken.stp.StpUtil;
2020
import cn.hutool.core.bean.BeanUtil;
2121
import cn.hutool.core.collection.CollUtil;
22-
import cn.hutool.core.img.ImgUtil;
2322
import cn.hutool.core.io.file.FileNameUtil;
2423
import cn.hutool.core.io.resource.ResourceUtil;
2524
import cn.hutool.core.lang.UUID;
@@ -48,19 +47,22 @@
4847
import me.ahoo.cosid.IdGenerator;
4948
import me.ahoo.cosid.provider.DefaultIdGeneratorProvider;
5049
import net.dreamlu.mica.core.result.R;
50+
import org.dromara.x.file.storage.core.FileInfo;
51+
import org.dromara.x.file.storage.core.FileStorageService;
5152
import org.springframework.beans.factory.annotation.Value;
5253
import org.springframework.security.crypto.password.PasswordEncoder;
5354
import org.springframework.stereotype.Service;
5455
import org.springframework.transaction.annotation.Transactional;
5556
import org.springframework.web.multipart.MultipartFile;
5657
import top.continew.admin.auth.service.OnlineUserService;
57-
import top.continew.admin.common.service.CommonUserService;
5858
import top.continew.admin.common.constant.CacheConstants;
5959
import top.continew.admin.common.constant.SysConstants;
60+
import top.continew.admin.common.constant.SysFileConstants;
6061
import top.continew.admin.common.context.UserContext;
6162
import top.continew.admin.common.context.UserContextHolder;
6263
import top.continew.admin.common.enums.DisEnableStatusEnum;
6364
import top.continew.admin.common.enums.GenderEnum;
65+
import top.continew.admin.common.service.CommonUserService;
6466
import top.continew.admin.common.util.SecureUtils;
6567
import top.continew.admin.system.enums.OptionCategoryEnum;
6668
import top.continew.admin.system.mapper.UserMapper;
@@ -78,13 +80,13 @@
7880
import top.continew.starter.cache.redisson.util.RedisUtils;
7981
import top.continew.starter.core.constant.StringConstants;
8082
import top.continew.starter.core.exception.BusinessException;
83+
import top.continew.starter.core.util.SpringUtils;
8184
import top.continew.starter.core.validation.CheckUtils;
8285
import top.continew.starter.extension.crud.model.query.PageQuery;
8386
import top.continew.starter.extension.crud.model.query.SortQuery;
8487
import top.continew.starter.extension.crud.model.resp.PageResp;
8588
import top.continew.starter.extension.crud.service.BaseServiceImpl;
8689
import top.continew.starter.web.util.FileUploadUtils;
87-
import top.continew.starter.core.util.SpringUtils;
8890

8991
import java.io.IOException;
9092
import java.time.Duration;
@@ -113,6 +115,8 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
113115
private final OptionService optionService;
114116
private final UserRoleService userRoleService;
115117
private final RoleService roleService;
118+
private final FileService fileService;
119+
private final FileStorageService fileStorageService;
116120

117121
@Resource
118122
private DeptService deptService;
@@ -385,11 +389,11 @@ public String updateAvatar(MultipartFile avatarFile, Long id) throws IOException
385389
String avatarImageType = FileNameUtil.extName(avatarFile.getOriginalFilename());
386390
CheckUtils.throwIf(!StrUtil.equalsAnyIgnoreCase(avatarImageType, avatarSupportSuffix), "头像仅支持 {} 格式的图片", String
387391
.join(StringConstants.CHINESE_COMMA, avatarSupportSuffix));
388-
// 更新用户头像
389-
String base64 = ImgUtil.toBase64DataUri(ImgUtil.scale(ImgUtil.toImage(avatarFile
390-
.getBytes()), 100, 100, null), avatarImageType);
391-
baseMapper.lambdaUpdate().set(UserDO::getAvatar, base64).eq(UserDO::getId, id).update();
392-
return base64;
392+
FileInfo fileInfo = fileService.upload(avatarFile, SysFileConstants.USER_AVATAR_PATH);
393+
UserDO userInfo = getById(id);
394+
fileStorageService.delete(userInfo.getAvatar());
395+
baseMapper.lambdaUpdate().set(UserDO::getAvatar, fileInfo.getUrl()).eq(UserDO::getId, id).update();
396+
return fileInfo.getUrl();
393397
}
394398

395399
@Override

0 commit comments

Comments
 (0)