【8.3.0】【config】更新文件操作类,实时获取最新配置

master
stylefeng 2024-08-29 23:51:12 +08:00
parent ef2e71c373
commit 1e87964c7d
2 changed files with 27 additions and 42 deletions

View File

@ -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;
}
@ -206,4 +186,18 @@ public class LocalFileOperator implements FileOperatorApi {
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();
}
}
}

View File

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