diff --git a/continew-starter-dependencies/pom.xml b/continew-starter-dependencies/pom.xml index 4fe1fa92..74f7eb5b 100644 --- a/continew-starter-dependencies/pom.xml +++ b/continew-starter-dependencies/pom.xml @@ -257,6 +257,13 @@ ${revision} + + + top.charles7c.continew + continew-starter-storage-local + ${revision} + + top.charles7c.continew diff --git a/continew-starter-storage/continew-starter-storage-local/pom.xml b/continew-starter-storage/continew-starter-storage-local/pom.xml new file mode 100644 index 00000000..c1344c67 --- /dev/null +++ b/continew-starter-storage/continew-starter-storage-local/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + top.charles7c.continew + continew-starter-storage + ${revision} + + + continew-starter-storage-local + jar + + ${project.artifactId} + ContiNew Starter 存储模块 - 本地存储 + \ No newline at end of file diff --git a/continew-starter-storage/continew-starter-storage-local/src/main/java/top/charles7c/continew/starter/storage/local/autoconfigure/LocalStorageAutoConfiguration.java b/continew-starter-storage/continew-starter-storage-local/src/main/java/top/charles7c/continew/starter/storage/local/autoconfigure/LocalStorageAutoConfiguration.java new file mode 100644 index 00000000..b8f8e225 --- /dev/null +++ b/continew-starter-storage/continew-starter-storage-local/src/main/java/top/charles7c/continew/starter/storage/local/autoconfigure/LocalStorageAutoConfiguration.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * 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 + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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 mappingMap = properties.getMapping(); + for (Map.Entry 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."); + } +} diff --git a/continew-starter-storage/continew-starter-storage-local/src/main/java/top/charles7c/continew/starter/storage/local/autoconfigure/LocalStorageProperties.java b/continew-starter-storage/continew-starter-storage-local/src/main/java/top/charles7c/continew/starter/storage/local/autoconfigure/LocalStorageProperties.java new file mode 100644 index 00000000..00367e7c --- /dev/null +++ b/continew-starter-storage/continew-starter-storage-local/src/main/java/top/charles7c/continew/starter/storage/local/autoconfigure/LocalStorageProperties.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * 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 + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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 mapping = new HashMap<>(); + + /** + * 本地存储映射 + */ + @Data + public static class LocalStorageMapping { + + /** + * 路径模式 + */ + private String pathPattern; + + /** + * 资源路径 + */ + private String location; + + /** + * 单文件上传大小限制 + */ + private DataSize maxFileSize = DataSize.ofMegabytes(1); + } +} diff --git a/continew-starter-storage/continew-starter-storage-local/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-storage/continew-starter-storage-local/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..198f26c8 --- /dev/null +++ b/continew-starter-storage/continew-starter-storage-local/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +top.charles7c.continew.starter.storage.local.autoconfigure.LocalStorageAutoConfiguration \ No newline at end of file diff --git a/continew-starter-storage/pom.xml b/continew-starter-storage/pom.xml new file mode 100644 index 00000000..acc1996c --- /dev/null +++ b/continew-starter-storage/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + top.charles7c.continew + continew-starter + ${revision} + + + continew-starter-storage + pom + + ${project.artifactId} + ContiNew Starter 存储模块 + + + continew-starter-storage-local + + + + + + top.charles7c.continew + continew-starter-core + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 047360ea..75b061db 100644 --- a/pom.xml +++ b/pom.xml @@ -73,6 +73,7 @@ continew-starter-json continew-starter-api-doc continew-starter-log + continew-starter-storage continew-starter-file continew-starter-captcha continew-starter-cache