mirror of https://gitee.com/stylefeng/roses
【8.3.0】【config】更新文件操作类,实时获取最新配置
parent
ef2e71c373
commit
1e87964c7d
|
@ -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.FileException;
|
||||||
import cn.stylefeng.roses.kernel.file.api.exception.enums.FileExceptionEnum;
|
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.expander.FileConfigExpander;
|
||||||
import cn.stylefeng.roses.kernel.file.api.pojo.props.LocalFileProperties;
|
|
||||||
import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
|
import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -49,30 +48,11 @@ import java.io.InputStream;
|
||||||
*/
|
*/
|
||||||
public class LocalFileOperator implements FileOperatorApi {
|
public class LocalFileOperator implements FileOperatorApi {
|
||||||
|
|
||||||
private final LocalFileProperties localFileProperties;
|
public LocalFileOperator() {
|
||||||
|
|
||||||
private String currentSavePath = "";
|
|
||||||
|
|
||||||
public LocalFileOperator(LocalFileProperties localFileProperties) {
|
|
||||||
this.localFileProperties = localFileProperties;
|
|
||||||
initClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initClient() {
|
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
|
@Override
|
||||||
|
@ -88,7 +68,7 @@ public class LocalFileOperator implements FileOperatorApi {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean doesBucketExist(String bucketName) {
|
public boolean doesBucketExist(String bucketName) {
|
||||||
String absolutePath = currentSavePath + File.separator + bucketName;
|
String absolutePath = this.getCurrentSavePath() + File.separator + bucketName;
|
||||||
return FileUtil.exist(absolutePath);
|
return FileUtil.exist(absolutePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,7 +79,7 @@ public class LocalFileOperator implements FileOperatorApi {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isExistingFile(String bucketName, String key) {
|
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);
|
return FileUtil.exist(absoluteFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,13 +87,13 @@ public class LocalFileOperator implements FileOperatorApi {
|
||||||
public void storageFile(String bucketName, String key, byte[] bytes) {
|
public void storageFile(String bucketName, String key, byte[] bytes) {
|
||||||
|
|
||||||
// 判断bucket存在不存在
|
// 判断bucket存在不存在
|
||||||
String bucketPath = currentSavePath + File.separator + bucketName;
|
String bucketPath = this.getCurrentSavePath() + File.separator + bucketName;
|
||||||
if (!FileUtil.exist(bucketPath)) {
|
if (!FileUtil.exist(bucketPath)) {
|
||||||
FileUtil.mkdir(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);
|
FileUtil.writeBytes(bytes, absoluteFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,13 +101,13 @@ public class LocalFileOperator implements FileOperatorApi {
|
||||||
public void storageFile(String bucketName, String key, InputStream inputStream) {
|
public void storageFile(String bucketName, String key, InputStream inputStream) {
|
||||||
|
|
||||||
// 判断bucket存在不存在
|
// 判断bucket存在不存在
|
||||||
String bucketPath = currentSavePath + File.separator + bucketName;
|
String bucketPath = this.getCurrentSavePath() + File.separator + bucketName;
|
||||||
if (!FileUtil.exist(bucketPath)) {
|
if (!FileUtil.exist(bucketPath)) {
|
||||||
FileUtil.mkdir(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);
|
FileUtil.writeFromStream(inputStream, absoluteFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +115,7 @@ public class LocalFileOperator implements FileOperatorApi {
|
||||||
public byte[] getFileBytes(String bucketName, String key) {
|
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)) {
|
if (!FileUtil.exist(absoluteFile)) {
|
||||||
// 组装返回信息
|
// 组装返回信息
|
||||||
String errorMessage = StrUtil.format("bucket={},key={}", bucketName, key);
|
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) {
|
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)) {
|
if (!FileUtil.exist(originFile)) {
|
||||||
// 组装返回信息
|
// 组装返回信息
|
||||||
String errorMessage = StrUtil.format("bucket={},key={}", originBucketName, originFileKey);
|
String errorMessage = StrUtil.format("bucket={},key={}", originBucketName, originFileKey);
|
||||||
|
@ -162,7 +142,7 @@ public class LocalFileOperator implements FileOperatorApi {
|
||||||
} else {
|
} 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);
|
FileUtil.copy(originFile, destFile, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -191,7 +171,7 @@ public class LocalFileOperator implements FileOperatorApi {
|
||||||
public void deleteFile(String bucketName, String key) {
|
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)) {
|
if (!FileUtil.exist(file)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -205,5 +185,19 @@ public class LocalFileOperator implements FileOperatorApi {
|
||||||
public FileLocationEnum getFileLocationEnum() {
|
public FileLocationEnum getFileLocationEnum() {
|
||||||
return FileLocationEnum.LOCAL;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,8 +25,6 @@
|
||||||
package cn.stylefeng.roses.kernel.file.starter;
|
package cn.stylefeng.roses.kernel.file.starter;
|
||||||
|
|
||||||
import cn.stylefeng.roses.kernel.file.api.FileOperatorApi;
|
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 cn.stylefeng.roses.kernel.file.local.LocalFileOperator;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
@ -50,14 +48,7 @@ public class ProjectFileAutoConfiguration {
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(FileOperatorApi.class)
|
@ConditionalOnMissingBean(FileOperatorApi.class)
|
||||||
public FileOperatorApi fileOperatorApi() {
|
public FileOperatorApi fileOperatorApi() {
|
||||||
|
return new LocalFileOperator();
|
||||||
LocalFileProperties localFileProperties = new LocalFileProperties();
|
|
||||||
|
|
||||||
// 从系统配置表中读取配置
|
|
||||||
localFileProperties.setLocalFileSavePathLinux(FileConfigExpander.getLocalFileSavePathLinux());
|
|
||||||
localFileProperties.setLocalFileSavePathWin(FileConfigExpander.getLocalFileSavePathWindows());
|
|
||||||
|
|
||||||
return new LocalFileOperator(localFileProperties);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue