feat(storage): 新增存储模块 - 本地存储

This commit is contained in:
2023-12-17 23:50:21 +08:00
parent c4459d1b8d
commit cd6826a0ab
7 changed files with 196 additions and 0 deletions

View File

@@ -257,6 +257,13 @@
<version>${revision}</version>
</dependency>
<!-- 存储模块 - 本地存储 -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-storage-local</artifactId>
<version>${revision}</version>
</dependency>
<!-- 日志模块 - HttpTraceProSpring Boot Actuator HttpTrace 定制增强版) -->
<dependency>
<groupId>top.charles7c.continew</groupId>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-storage</artifactId>
<version>${revision}</version>
</parent>
<artifactId>continew-starter-storage-local</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>ContiNew Starter 存储模块 - 本地存储</description>
</project>

View File

@@ -0,0 +1,74 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.starter.storage.local.autoconfigure;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.starter.core.constant.StringConstants;
import java.util.Map;
/**
* 本地文件自动配置
*
* @author Charles7c
* @since 1.1.0
*/
@Slf4j
@EnableWebMvc
@AutoConfiguration
@RequiredArgsConstructor
@EnableConfigurationProperties(LocalStorageProperties.class)
@ConditionalOnProperty(name = "continew-starter.storage.local.enabled", havingValue = "true")
public class LocalStorageAutoConfiguration implements WebMvcConfigurer {
private final LocalStorageProperties properties;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
Map<String, LocalStorageProperties.LocalStorageMapping> mappingMap = properties.getMapping();
for (Map.Entry<String, LocalStorageProperties.LocalStorageMapping> mappingEntry : mappingMap.entrySet()) {
LocalStorageProperties.LocalStorageMapping mapping = mappingEntry.getValue();
String pathPattern = mapping.getPathPattern();
String location = mapping.getLocation();
if (StrUtil.isBlank(location)) {
throw new IllegalArgumentException(String.format("Path pattern [%s] location is null.", pathPattern));
}
registry.addResourceHandler(pathPattern)
.addResourceLocations(!location.startsWith("file:") ? String.format("file:%s", this.format(location)) : this.format(location))
.setCachePeriod(0);
}
}
private String format(String location) {
return location.replace(StringConstants.BACKSLASH, StringConstants.SLASH);
}
@PostConstruct
public void postConstruct() {
log.info("[ContiNew Starter] - Auto Configuration 'Storage-Local' completed initialization.");
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.starter.storage.local.autoconfigure;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.unit.DataSize;
import java.util.HashMap;
import java.util.Map;
/**
* 本地存储配置属性
*
* @author Charles7c
* @since 1.1.0
*/
@Data
@ConfigurationProperties(prefix = "continew-starter.storage.local")
public class LocalStorageProperties {
/**
* 是否启用本地存储
*/
private boolean enabled = false;
/**
* 存储映射
*/
private Map<String, LocalStorageMapping> mapping = new HashMap<>();
/**
* 本地存储映射
*/
@Data
public static class LocalStorageMapping {
/**
* 路径模式
*/
private String pathPattern;
/**
* 资源路径
*/
private String location;
/**
* 单文件上传大小限制
*/
private DataSize maxFileSize = DataSize.ofMegabytes(1);
}
}

View File

@@ -0,0 +1 @@
top.charles7c.continew.starter.storage.local.autoconfigure.LocalStorageAutoConfiguration

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter</artifactId>
<version>${revision}</version>
</parent>
<artifactId>continew-starter-storage</artifactId>
<packaging>pom</packaging>
<name>${project.artifactId}</name>
<description>ContiNew Starter 存储模块</description>
<modules>
<module>continew-starter-storage-local</module>
</modules>
<dependencies>
<!-- 核心模块 -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-core</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -73,6 +73,7 @@
<module>continew-starter-json</module>
<module>continew-starter-api-doc</module>
<module>continew-starter-log</module>
<module>continew-starter-storage</module>
<module>continew-starter-file</module>
<module>continew-starter-captcha</module>
<module>continew-starter-cache</module>