refactor: 适配 ContiNew Starter Local Storage(存储模块-本地存储)

This commit is contained in:
Charles7c 2023-12-17 23:51:09 +08:00
parent 4ed4ddd4f0
commit e3cf2b7e40
8 changed files with 55 additions and 170 deletions

View File

@ -58,6 +58,12 @@
<artifactId>continew-starter-file-excel</artifactId> <artifactId>continew-starter-file-excel</artifactId>
</dependency> </dependency>
<!-- ContiNew Starter 存储模块 - 本地存储 -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-storage-local</artifactId>
</dependency>
<!-- ContiNew Starter API 文档模块 --> <!-- ContiNew Starter API 文档模块 -->
<dependency> <dependency>
<groupId>top.charles7c.continew</groupId> <groupId>top.charles7c.continew</groupId>

View File

@ -26,12 +26,8 @@ import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 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 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 配置 * Web MVC 配置
* *
@ -43,23 +39,8 @@ import top.charles7c.continew.starter.core.constant.StringConstants;
@RequiredArgsConstructor @RequiredArgsConstructor
public class WebMvcConfiguration implements WebMvcConfigurer { public class WebMvcConfiguration implements WebMvcConfigurer {
private final LocalStorageProperties localStorageProperties;
private final MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter; 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 配置不生效的问题 * 解决 Jackson2ObjectMapperBuilderCustomizer 配置不生效的问题
* <p> * <p>

View File

@ -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;
}
}

View File

@ -28,6 +28,7 @@ import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
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.util.unit.DataSize;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import cn.hutool.core.collection.CollUtil; 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.ObjectUtil;
import cn.hutool.core.util.StrUtil; 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.CacheConstants;
import top.charles7c.continew.admin.common.constant.FileConstants; import top.charles7c.continew.admin.common.constant.FileConstants;
import top.charles7c.continew.admin.common.constant.SysConstants; 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.core.util.validate.CheckUtils;
import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl; import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;
import top.charles7c.continew.starter.extension.crud.base.CommonUserService; 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 @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public String uploadAvatar(MultipartFile avatarFile, Long id) { public String uploadAvatar(MultipartFile avatarFile, Long id) {
Long avatarMaxSizeInMb = localStorageProperties.getAvatarMaxSizeInMb(); LocalStorageProperties.LocalStorageMapping storageMapping = localStorageProperties.getMapping().get("AVATAR");
CheckUtils.throwIf(avatarFile.getSize() > avatarMaxSizeInMb * 1024 * 1024, "请上传小于 {}MB 的图片", avatarMaxSizeInMb); DataSize maxFileSize = storageMapping.getMaxFileSize();
CheckUtils.throwIf(avatarFile.getSize() > maxFileSize.toBytes(), "请上传小于 {}MB 的图片", maxFileSize.toMegabytes());
String avatarImageType = FileNameUtil.extName(avatarFile.getOriginalFilename()); String avatarImageType = FileNameUtil.extName(avatarFile.getOriginalFilename());
String[] avatarSupportImgTypes = FileConstants.AVATAR_SUPPORTED_IMG_TYPES; String[] avatarSupportImgTypes = FileConstants.AVATAR_SUPPORTED_IMG_TYPES;
CheckUtils.throwIf(!StrUtil.equalsAnyIgnoreCase(avatarImageType, avatarSupportImgTypes), "头像仅支持 {} 格式的图片", CheckUtils.throwIf(!StrUtil.equalsAnyIgnoreCase(avatarImageType, avatarSupportImgTypes), "头像仅支持 {} 格式的图片",
String.join(StringConstants.CHINESE_COMMA, avatarSupportImgTypes)); String.join(StringConstants.CHINESE_COMMA, avatarSupportImgTypes));
// 上传新头像 // 上传新头像
UserDO user = super.getById(id); UserDO user = super.getById(id);
String avatarPath = localStorageProperties.getPath().getAvatar(); String avatarPath = storageMapping.getLocation();
File newAvatarFile = FileUploadUtils.upload(avatarFile, avatarPath, false); File newAvatarFile = FileUploadUtils.upload(avatarFile, avatarPath, false);
CheckUtils.throwIfNull(newAvatarFile, "上传头像失败"); CheckUtils.throwIfNull(newAvatarFile, "上传头像失败");
assert null != newAvatarFile; assert null != newAvatarFile;

View File

@ -33,6 +33,7 @@ import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.unit.DataSize;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; 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.ClassUtil;
import cn.hutool.core.util.StrUtil; 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.CacheConstants;
import top.charles7c.continew.admin.common.model.resp.LabelValueResp; import top.charles7c.continew.admin.common.model.resp.LabelValueResp;
import top.charles7c.continew.admin.system.model.query.DeptQuery; 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.query.SortQuery;
import top.charles7c.continew.starter.extension.crud.model.resp.R; import top.charles7c.continew.starter.extension.crud.model.resp.R;
import top.charles7c.continew.starter.log.common.annotation.Log; import top.charles7c.continew.starter.log.common.annotation.Log;
import top.charles7c.continew.starter.storage.local.autoconfigure.LocalStorageProperties;
/** /**
* 公共 API * 公共 API
@ -86,9 +87,10 @@ public class CommonController {
@PostMapping("/file") @PostMapping("/file")
public R<String> upload(@NotNull(message = "文件不能为空") MultipartFile file) { public R<String> upload(@NotNull(message = "文件不能为空") MultipartFile file) {
ValidationUtils.throwIf(file::isEmpty, "文件不能为空"); ValidationUtils.throwIf(file::isEmpty, "文件不能为空");
Long maxSizeInMb = localStorageProperties.getMaxSizeInMb(); LocalStorageProperties.LocalStorageMapping storageMapping = localStorageProperties.getMapping().get("FILE");
CheckUtils.throwIf(file.getSize() > maxSizeInMb * 1024 * 1024, "请上传小于 {}MB 的文件", maxSizeInMb); DataSize maxFileSize = storageMapping.getMaxFileSize();
String filePath = localStorageProperties.getPath().getFile(); CheckUtils.throwIf(file.getSize() > maxFileSize.toBytes(), "请上传小于 {}MB 的文件", maxFileSize.toMegabytes());
String filePath = storageMapping.getLocation();
File newFile = FileUploadUtils.upload(file, filePath, false); File newFile = FileUploadUtils.upload(file, filePath, false);
CheckUtils.throwIfNull(newFile, "上传文件失败"); CheckUtils.throwIfNull(newFile, "上传文件失败");
assert null != newFile; assert null != newFile;

View File

@ -208,27 +208,18 @@ spring.servlet:
max-request-size: 20MB max-request-size: 20MB
--- ### 本地存储配置 --- ### 本地存储配置
local-storage: continew-starter.storage:
# 文件模式 local:
filePattern: /file/** enabled: true
# 头像模式 mapping:
avatarPattern: /avatar/** FILE:
# 文件上传大小限制 path-pattern: /file/**
maxSizeInMb: 10 location: C:\${project.app-name}\data\file\
# 头像上传大小限制 max-file-size: 10MB
avatarMaxSizeInMb: 5 AVATAR:
## Windows 系统本地存储配置 path-pattern: /avatar/**
windows: location: C:\${project.app-name}\data\avatar\
file: C:\${project.app-name}\data\file\ max-file-size: 5MB
avatar: C:\${project.app-name}\data\avatar\
## Linux 系统本地存储配置
linux:
file: ./data/file/
avatar: ./data/avatar/
## Mac 系统本地存储配置
mac:
file: ~/data/file/
avatar: ~/data/avatar/
--- ### 跨域配置 --- ### 跨域配置
cors: cors:

View File

@ -207,27 +207,18 @@ spring.servlet:
max-request-size: 20MB max-request-size: 20MB
--- ### 本地存储配置 --- ### 本地存储配置
local-storage: continew-starter.storage:
# 文件模式 local:
filePattern: /file/** enabled: true
# 头像模式 mapping:
avatarPattern: /avatar/** FILE:
# 文件上传大小限制 path-pattern: /file/**
maxSizeInMb: 10 location: ../data/file/
# 头像上传大小限制 max-file-size: 10MB
avatarMaxSizeInMb: 5 AVATAR:
## Windows 系统本地存储配置 path-pattern: /avatar/**
windows: location: ../data/avatar/
file: C:\${project.app-name}\data\file\ max-file-size: 5MB
avatar: C:\${project.app-name}\data\avatar\
## Linux 系统本地存储配置
linux:
file: ../data/file/
avatar: ../data/avatar/
## Mac 系统本地存储配置
mac:
file: ~/data/file/
avatar: ~/data/avatar/
--- ### 跨域配置 --- ### 跨域配置
cors: cors:

View File

@ -26,20 +26,19 @@ project:
logging: logging:
config: classpath:logback-spring.xml config: classpath:logback-spring.xml
## 日志配置 ## 日志配置
continew-starter: continew-starter.log:
log: enabled: true
enabled: true include:
include: - DESCRIPTION
- DESCRIPTION - MODULE
- MODULE - REQUEST_HEADERS
- REQUEST_HEADERS - REQUEST_BODY
- REQUEST_BODY - IP_ADDRESS
- IP_ADDRESS - BROWSER
- BROWSER - OS
- OS - RESPONSE_HEADERS
- RESPONSE_HEADERS - RESPONSE_BODY
- RESPONSE_BODY - TIME_TAKEN
- TIME_TAKEN
--- ### 接口文档配置 --- ### 接口文档配置
springdoc: springdoc: