From 1e87964c7d006c0500dac4218b642907cfcc6972 Mon Sep 17 00:00:00 2001 From: stylefeng Date: Thu, 29 Aug 2024 23:51:12 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=908.3.0=E3=80=91=E3=80=90config=E3=80=91?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E4=BB=B6=E6=93=8D=E4=BD=9C=E7=B1=BB?= =?UTF-8?q?=EF=BC=8C=E5=AE=9E=E6=97=B6=E8=8E=B7=E5=8F=96=E6=9C=80=E6=96=B0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kernel/file/local/LocalFileOperator.java | 58 +++++++++---------- .../starter/ProjectFileAutoConfiguration.java | 11 +--- 2 files changed, 27 insertions(+), 42 deletions(-) diff --git a/kernel-d-file/file-sdk-local/src/main/java/cn/stylefeng/roses/kernel/file/local/LocalFileOperator.java b/kernel-d-file/file-sdk-local/src/main/java/cn/stylefeng/roses/kernel/file/local/LocalFileOperator.java index cd394725c..5688d9d57 100644 --- a/kernel-d-file/file-sdk-local/src/main/java/cn/stylefeng/roses/kernel/file/local/LocalFileOperator.java +++ b/kernel-d-file/file-sdk-local/src/main/java/cn/stylefeng/roses/kernel/file/local/LocalFileOperator.java @@ -35,7 +35,6 @@ import cn.stylefeng.roses.kernel.file.api.enums.FileLocationEnum; import cn.stylefeng.roses.kernel.file.api.exception.FileException; import cn.stylefeng.roses.kernel.file.api.exception.enums.FileExceptionEnum; import cn.stylefeng.roses.kernel.file.api.expander.FileConfigExpander; -import cn.stylefeng.roses.kernel.file.api.pojo.props.LocalFileProperties; import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil; import java.io.File; @@ -49,30 +48,11 @@ import java.io.InputStream; */ public class LocalFileOperator implements FileOperatorApi { - private final LocalFileProperties localFileProperties; - - private String currentSavePath = ""; - - public LocalFileOperator(LocalFileProperties localFileProperties) { - this.localFileProperties = localFileProperties; - initClient(); + public LocalFileOperator() { } @Override public void initClient() { - if (SystemUtil.getOsInfo().isWindows()) { - String savePathWindows = localFileProperties.getLocalFileSavePathWin(); - if (!FileUtil.exist(savePathWindows)) { - FileUtil.mkdir(savePathWindows); - } - currentSavePath = savePathWindows; - } else { - String savePathLinux = localFileProperties.getLocalFileSavePathLinux(); - if (!FileUtil.exist(savePathLinux)) { - FileUtil.mkdir(savePathLinux); - } - currentSavePath = savePathLinux; - } } @Override @@ -88,7 +68,7 @@ public class LocalFileOperator implements FileOperatorApi { @Override public boolean doesBucketExist(String bucketName) { - String absolutePath = currentSavePath + File.separator + bucketName; + String absolutePath = this.getCurrentSavePath() + File.separator + bucketName; return FileUtil.exist(absolutePath); } @@ -99,7 +79,7 @@ public class LocalFileOperator implements FileOperatorApi { @Override public boolean isExistingFile(String bucketName, String key) { - String absoluteFile = currentSavePath + File.separator + bucketName + File.separator + key; + String absoluteFile = this.getCurrentSavePath() + File.separator + bucketName + File.separator + key; return FileUtil.exist(absoluteFile); } @@ -107,13 +87,13 @@ public class LocalFileOperator implements FileOperatorApi { public void storageFile(String bucketName, String key, byte[] bytes) { // 判断bucket存在不存在 - String bucketPath = currentSavePath + File.separator + bucketName; + String bucketPath = this.getCurrentSavePath() + File.separator + bucketName; if (!FileUtil.exist(bucketPath)) { FileUtil.mkdir(bucketPath); } // 存储文件 - String absoluteFile = currentSavePath + File.separator + bucketName + File.separator + key; + String absoluteFile = this.getCurrentSavePath() + File.separator + bucketName + File.separator + key; FileUtil.writeBytes(bytes, absoluteFile); } @@ -121,13 +101,13 @@ public class LocalFileOperator implements FileOperatorApi { public void storageFile(String bucketName, String key, InputStream inputStream) { // 判断bucket存在不存在 - String bucketPath = currentSavePath + File.separator + bucketName; + String bucketPath = this.getCurrentSavePath() + File.separator + bucketName; if (!FileUtil.exist(bucketPath)) { FileUtil.mkdir(bucketPath); } // 存储文件 - String absoluteFile = currentSavePath + File.separator + bucketName + File.separator + key; + String absoluteFile = this.getCurrentSavePath() + File.separator + bucketName + File.separator + key; FileUtil.writeFromStream(inputStream, absoluteFile); } @@ -135,7 +115,7 @@ public class LocalFileOperator implements FileOperatorApi { public byte[] getFileBytes(String bucketName, String key) { // 判断文件存在不存在 - String absoluteFile = currentSavePath + File.separator + bucketName + File.separator + key; + String absoluteFile = this.getCurrentSavePath() + File.separator + bucketName + File.separator + key; if (!FileUtil.exist(absoluteFile)) { // 组装返回信息 String errorMessage = StrUtil.format("bucket={},key={}", bucketName, key); @@ -154,7 +134,7 @@ public class LocalFileOperator implements FileOperatorApi { public void copyFile(String originBucketName, String originFileKey, String newBucketName, String newFileKey) { // 判断文件存在不存在 - String originFile = currentSavePath + File.separator + originBucketName + File.separator + originFileKey; + String originFile = this.getCurrentSavePath() + File.separator + originBucketName + File.separator + originFileKey; if (!FileUtil.exist(originFile)) { // 组装返回信息 String errorMessage = StrUtil.format("bucket={},key={}", originBucketName, originFileKey); @@ -162,7 +142,7 @@ public class LocalFileOperator implements FileOperatorApi { } else { // 拷贝文件 - String destFile = currentSavePath + File.separator + newBucketName + File.separator + newFileKey; + String destFile = this.getCurrentSavePath() + File.separator + newBucketName + File.separator + newFileKey; FileUtil.copy(originFile, destFile, true); } } @@ -191,7 +171,7 @@ public class LocalFileOperator implements FileOperatorApi { public void deleteFile(String bucketName, String key) { // 判断文件存在不存在 - String file = currentSavePath + File.separator + bucketName + File.separator + key; + String file = this.getCurrentSavePath() + File.separator + bucketName + File.separator + key; if (!FileUtil.exist(file)) { return; } @@ -205,5 +185,19 @@ public class LocalFileOperator implements FileOperatorApi { public FileLocationEnum getFileLocationEnum() { return FileLocationEnum.LOCAL; } - + + /** + * 获取文件当前存储路径 + * + * @author fengshuonan + * @since 2024/8/29 23:46 + */ + private String getCurrentSavePath() { + if (SystemUtil.getOsInfo().isWindows()) { + return FileConfigExpander.getLocalFileSavePathWindows(); + } else { + return FileConfigExpander.getLocalFileSavePathLinux(); + } + } + } diff --git a/kernel-d-file/file-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/file/starter/ProjectFileAutoConfiguration.java b/kernel-d-file/file-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/file/starter/ProjectFileAutoConfiguration.java index 849344c81..95e3574ac 100644 --- a/kernel-d-file/file-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/file/starter/ProjectFileAutoConfiguration.java +++ b/kernel-d-file/file-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/file/starter/ProjectFileAutoConfiguration.java @@ -25,8 +25,6 @@ package cn.stylefeng.roses.kernel.file.starter; import cn.stylefeng.roses.kernel.file.api.FileOperatorApi; -import cn.stylefeng.roses.kernel.file.api.expander.FileConfigExpander; -import cn.stylefeng.roses.kernel.file.api.pojo.props.LocalFileProperties; import cn.stylefeng.roses.kernel.file.local.LocalFileOperator; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; @@ -50,14 +48,7 @@ public class ProjectFileAutoConfiguration { @Bean @ConditionalOnMissingBean(FileOperatorApi.class) public FileOperatorApi fileOperatorApi() { - - LocalFileProperties localFileProperties = new LocalFileProperties(); - - // 从系统配置表中读取配置 - localFileProperties.setLocalFileSavePathLinux(FileConfigExpander.getLocalFileSavePathLinux()); - localFileProperties.setLocalFileSavePathWin(FileConfigExpander.getLocalFileSavePathWindows()); - - return new LocalFileOperator(localFileProperties); + return new LocalFileOperator(); } }