mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-10-31 22:57:17 +08:00 
			
		
		
		
	refactor: 适配 ContiNew Starter Local Storage(存储模块-本地存储)
This commit is contained in:
		| @@ -58,6 +58,12 @@ | ||||
|             <artifactId>continew-starter-file-excel</artifactId> | ||||
|         </dependency> | ||||
|  | ||||
|         <!-- ContiNew Starter 存储模块 - 本地存储 --> | ||||
|         <dependency> | ||||
|             <groupId>top.charles7c.continew</groupId> | ||||
|             <artifactId>continew-starter-storage-local</artifactId> | ||||
|         </dependency> | ||||
|  | ||||
|         <!-- ContiNew Starter API 文档模块 --> | ||||
|         <dependency> | ||||
|             <groupId>top.charles7c.continew</groupId> | ||||
|   | ||||
| @@ -26,12 +26,8 @@ import org.springframework.http.converter.ByteArrayHttpMessageConverter; | ||||
| import org.springframework.http.converter.HttpMessageConverter; | ||||
| import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||||
| import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||||
| import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | ||||
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||||
|  | ||||
| import top.charles7c.continew.admin.common.config.properties.LocalStorageProperties; | ||||
| import top.charles7c.continew.starter.core.constant.StringConstants; | ||||
|  | ||||
| /** | ||||
|  * Web MVC 配置 | ||||
|  * | ||||
| @@ -43,23 +39,8 @@ import top.charles7c.continew.starter.core.constant.StringConstants; | ||||
| @RequiredArgsConstructor | ||||
| public class WebMvcConfiguration implements WebMvcConfigurer { | ||||
|  | ||||
|     private final LocalStorageProperties localStorageProperties; | ||||
|     private final MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter; | ||||
|  | ||||
|     /** | ||||
|      * 静态资源处理器配置 | ||||
|      */ | ||||
|     @Override | ||||
|     public void addResourceHandlers(ResourceHandlerRegistry registry) { | ||||
|         LocalStorageProperties.LocalStoragePath path = localStorageProperties.getPath(); | ||||
|         String avatarUtl = "file:" + path.getAvatar().replace(StringConstants.BACKSLASH, StringConstants.SLASH); | ||||
|         String fileUrl = "file:" + path.getFile().replace(StringConstants.BACKSLASH, StringConstants.SLASH); | ||||
|         registry.addResourceHandler(localStorageProperties.getFilePattern()).addResourceLocations(fileUrl) | ||||
|             .setCachePeriod(0); | ||||
|         registry.addResourceHandler(localStorageProperties.getAvatarPattern()).addResourceLocations(avatarUtl) | ||||
|             .setCachePeriod(0); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 解决 Jackson2ObjectMapperBuilderCustomizer 配置不生效的问题 | ||||
|      * <p> | ||||
|   | ||||
| @@ -1,87 +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.charles7c.continew.admin.common.config.properties; | ||||
|  | ||||
| import lombok.Data; | ||||
|  | ||||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||||
| import org.springframework.stereotype.Component; | ||||
|  | ||||
| import cn.hutool.system.OsInfo; | ||||
| import cn.hutool.system.SystemUtil; | ||||
|  | ||||
| /** | ||||
|  * 本地存储配置属性 | ||||
|  * | ||||
|  * @author Charles7c | ||||
|  * @since 2023/1/2 19:43 | ||||
|  */ | ||||
| @Data | ||||
| @Component | ||||
| @ConfigurationProperties(prefix = "local-storage") | ||||
| public class LocalStorageProperties { | ||||
|  | ||||
|     /** 文件模式 */ | ||||
|     private String filePattern; | ||||
|  | ||||
|     /** 头像模式 */ | ||||
|     private String avatarPattern; | ||||
|  | ||||
|     /** 文件大小限制 */ | ||||
|     private Long maxSizeInMb; | ||||
|  | ||||
|     /** 头像大小限制 */ | ||||
|     private Long avatarMaxSizeInMb; | ||||
|  | ||||
|     /** Windows 系统本地存储路径 */ | ||||
|     private LocalStoragePath windows; | ||||
|  | ||||
|     /** Linux 系统本地存储路径 */ | ||||
|     private LocalStoragePath linux; | ||||
|  | ||||
|     /** MAC 系统本地存储路径 */ | ||||
|     private LocalStoragePath mac; | ||||
|  | ||||
|     /** | ||||
|      * 获取存储路径 | ||||
|      *  | ||||
|      * @return / | ||||
|      */ | ||||
|     public LocalStoragePath getPath() { | ||||
|         OsInfo osInfo = SystemUtil.getOsInfo(); | ||||
|         if (osInfo.isWindows()) { | ||||
|             return windows; | ||||
|         } | ||||
|         if (osInfo.isMac()) { | ||||
|             return mac; | ||||
|         } | ||||
|         return linux; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 本地存储路径 | ||||
|      */ | ||||
|     @Data | ||||
|     public static class LocalStoragePath { | ||||
|  | ||||
|         /** 文件存储路径 */ | ||||
|         private String file; | ||||
|  | ||||
|         /** 头像存储路径 */ | ||||
|         private String avatar; | ||||
|     } | ||||
| } | ||||
| @@ -28,6 +28,7 @@ import org.springframework.cache.annotation.CacheConfig; | ||||
| import org.springframework.cache.annotation.Cacheable; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.transaction.annotation.Transactional; | ||||
| import org.springframework.util.unit.DataSize; | ||||
| import org.springframework.web.multipart.MultipartFile; | ||||
|  | ||||
| import cn.hutool.core.collection.CollUtil; | ||||
| @@ -36,7 +37,6 @@ import cn.hutool.core.io.file.FileNameUtil; | ||||
| import cn.hutool.core.util.ObjectUtil; | ||||
| import cn.hutool.core.util.StrUtil; | ||||
|  | ||||
| import top.charles7c.continew.admin.common.config.properties.LocalStorageProperties; | ||||
| import top.charles7c.continew.admin.common.constant.CacheConstants; | ||||
| import top.charles7c.continew.admin.common.constant.FileConstants; | ||||
| import top.charles7c.continew.admin.common.constant.SysConstants; | ||||
| @@ -61,6 +61,7 @@ import top.charles7c.continew.starter.core.util.FileUploadUtils; | ||||
| import top.charles7c.continew.starter.core.util.validate.CheckUtils; | ||||
| import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl; | ||||
| import top.charles7c.continew.starter.extension.crud.base.CommonUserService; | ||||
| import top.charles7c.continew.starter.storage.local.autoconfigure.LocalStorageProperties; | ||||
|  | ||||
| /** | ||||
|  * 用户业务实现 | ||||
| @@ -163,15 +164,16 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes | ||||
|     @Override | ||||
|     @Transactional(rollbackFor = Exception.class) | ||||
|     public String uploadAvatar(MultipartFile avatarFile, Long id) { | ||||
|         Long avatarMaxSizeInMb = localStorageProperties.getAvatarMaxSizeInMb(); | ||||
|         CheckUtils.throwIf(avatarFile.getSize() > avatarMaxSizeInMb * 1024 * 1024, "请上传小于 {}MB 的图片", avatarMaxSizeInMb); | ||||
|         LocalStorageProperties.LocalStorageMapping storageMapping = localStorageProperties.getMapping().get("AVATAR"); | ||||
|         DataSize maxFileSize = storageMapping.getMaxFileSize(); | ||||
|         CheckUtils.throwIf(avatarFile.getSize() > maxFileSize.toBytes(), "请上传小于 {}MB 的图片", maxFileSize.toMegabytes()); | ||||
|         String avatarImageType = FileNameUtil.extName(avatarFile.getOriginalFilename()); | ||||
|         String[] avatarSupportImgTypes = FileConstants.AVATAR_SUPPORTED_IMG_TYPES; | ||||
|         CheckUtils.throwIf(!StrUtil.equalsAnyIgnoreCase(avatarImageType, avatarSupportImgTypes), "头像仅支持 {} 格式的图片", | ||||
|             String.join(StringConstants.CHINESE_COMMA, avatarSupportImgTypes)); | ||||
|         // 上传新头像 | ||||
|         UserDO user = super.getById(id); | ||||
|         String avatarPath = localStorageProperties.getPath().getAvatar(); | ||||
|         String avatarPath = storageMapping.getLocation(); | ||||
|         File newAvatarFile = FileUploadUtils.upload(avatarFile, avatarPath, false); | ||||
|         CheckUtils.throwIfNull(newAvatarFile, "上传头像失败"); | ||||
|         assert null != newAvatarFile; | ||||
|   | ||||
| @@ -33,6 +33,7 @@ import io.swagger.v3.oas.annotations.enums.ParameterIn; | ||||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||||
|  | ||||
| import org.springframework.cache.annotation.Cacheable; | ||||
| import org.springframework.util.unit.DataSize; | ||||
| import org.springframework.validation.annotation.Validated; | ||||
| import org.springframework.web.bind.annotation.*; | ||||
| import org.springframework.web.multipart.MultipartFile; | ||||
| @@ -42,7 +43,6 @@ import cn.hutool.core.lang.tree.Tree; | ||||
| import cn.hutool.core.util.ClassUtil; | ||||
| import cn.hutool.core.util.StrUtil; | ||||
|  | ||||
| import top.charles7c.continew.admin.common.config.properties.LocalStorageProperties; | ||||
| import top.charles7c.continew.admin.common.constant.CacheConstants; | ||||
| import top.charles7c.continew.admin.common.model.resp.LabelValueResp; | ||||
| import top.charles7c.continew.admin.system.model.query.DeptQuery; | ||||
| @@ -59,6 +59,7 @@ import top.charles7c.continew.starter.data.mybatis.plus.enums.IBaseEnum; | ||||
| import top.charles7c.continew.starter.extension.crud.model.query.SortQuery; | ||||
| import top.charles7c.continew.starter.extension.crud.model.resp.R; | ||||
| import top.charles7c.continew.starter.log.common.annotation.Log; | ||||
| import top.charles7c.continew.starter.storage.local.autoconfigure.LocalStorageProperties; | ||||
|  | ||||
| /** | ||||
|  * 公共 API | ||||
| @@ -86,9 +87,10 @@ public class CommonController { | ||||
|     @PostMapping("/file") | ||||
|     public R<String> upload(@NotNull(message = "文件不能为空") MultipartFile file) { | ||||
|         ValidationUtils.throwIf(file::isEmpty, "文件不能为空"); | ||||
|         Long maxSizeInMb = localStorageProperties.getMaxSizeInMb(); | ||||
|         CheckUtils.throwIf(file.getSize() > maxSizeInMb * 1024 * 1024, "请上传小于 {}MB 的文件", maxSizeInMb); | ||||
|         String filePath = localStorageProperties.getPath().getFile(); | ||||
|         LocalStorageProperties.LocalStorageMapping storageMapping = localStorageProperties.getMapping().get("FILE"); | ||||
|         DataSize maxFileSize = storageMapping.getMaxFileSize(); | ||||
|         CheckUtils.throwIf(file.getSize() > maxFileSize.toBytes(), "请上传小于 {}MB 的文件", maxFileSize.toMegabytes()); | ||||
|         String filePath = storageMapping.getLocation(); | ||||
|         File newFile = FileUploadUtils.upload(file, filePath, false); | ||||
|         CheckUtils.throwIfNull(newFile, "上传文件失败"); | ||||
|         assert null != newFile; | ||||
|   | ||||
| @@ -208,27 +208,18 @@ spring.servlet: | ||||
|     max-request-size: 20MB | ||||
|  | ||||
| --- ### 本地存储配置 | ||||
| local-storage: | ||||
|   # 文件模式 | ||||
|   filePattern: /file/** | ||||
|   # 头像模式 | ||||
|   avatarPattern: /avatar/** | ||||
|   # 文件上传大小限制 | ||||
|   maxSizeInMb: 10 | ||||
|   # 头像上传大小限制 | ||||
|   avatarMaxSizeInMb: 5 | ||||
|   ## Windows 系统本地存储配置 | ||||
|   windows: | ||||
|     file: C:\${project.app-name}\data\file\ | ||||
|     avatar: C:\${project.app-name}\data\avatar\ | ||||
|   ## Linux 系统本地存储配置 | ||||
|   linux: | ||||
|     file: ./data/file/ | ||||
|     avatar: ./data/avatar/ | ||||
|   ## Mac 系统本地存储配置 | ||||
|   mac: | ||||
|     file: ~/data/file/ | ||||
|     avatar: ~/data/avatar/ | ||||
| continew-starter.storage: | ||||
|   local: | ||||
|     enabled: true | ||||
|     mapping: | ||||
|       FILE: | ||||
|         path-pattern: /file/** | ||||
|         location: C:\${project.app-name}\data\file\ | ||||
|         max-file-size: 10MB | ||||
|       AVATAR: | ||||
|         path-pattern: /avatar/** | ||||
|         location: C:\${project.app-name}\data\avatar\ | ||||
|         max-file-size: 5MB | ||||
|  | ||||
| --- ### 跨域配置 | ||||
| cors: | ||||
|   | ||||
| @@ -207,27 +207,18 @@ spring.servlet: | ||||
|     max-request-size: 20MB | ||||
|  | ||||
| --- ### 本地存储配置 | ||||
| local-storage: | ||||
|   # 文件模式 | ||||
|   filePattern: /file/** | ||||
|   # 头像模式 | ||||
|   avatarPattern: /avatar/** | ||||
|   # 文件上传大小限制 | ||||
|   maxSizeInMb: 10 | ||||
|   # 头像上传大小限制 | ||||
|   avatarMaxSizeInMb: 5 | ||||
|   ## Windows 系统本地存储配置 | ||||
|   windows: | ||||
|     file: C:\${project.app-name}\data\file\ | ||||
|     avatar: C:\${project.app-name}\data\avatar\ | ||||
|   ## Linux 系统本地存储配置 | ||||
|   linux: | ||||
|     file: ../data/file/ | ||||
|     avatar: ../data/avatar/ | ||||
|   ## Mac 系统本地存储配置 | ||||
|   mac: | ||||
|     file: ~/data/file/ | ||||
|     avatar: ~/data/avatar/ | ||||
| continew-starter.storage: | ||||
|   local: | ||||
|     enabled: true | ||||
|     mapping: | ||||
|       FILE: | ||||
|         path-pattern: /file/** | ||||
|         location: ../data/file/ | ||||
|         max-file-size: 10MB | ||||
|       AVATAR: | ||||
|         path-pattern: /avatar/** | ||||
|         location: ../data/avatar/ | ||||
|         max-file-size: 5MB | ||||
|  | ||||
| --- ### 跨域配置 | ||||
| cors: | ||||
|   | ||||
| @@ -26,8 +26,7 @@ project: | ||||
| logging: | ||||
|   config: classpath:logback-spring.xml | ||||
| ## 日志配置 | ||||
| continew-starter: | ||||
|   log: | ||||
| continew-starter.log: | ||||
|   enabled: true | ||||
|   include: | ||||
|     - DESCRIPTION | ||||
|   | ||||
		Reference in New Issue
	
	Block a user