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

This commit is contained in:
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>
</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>

View File

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

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