mirror of
https://github.com/zfile-dev/zfile.git
synced 2025-04-19 05:34:52 +00:00
✨ 初始化存储引擎后, 增加测试连接机制
This commit is contained in:
@@ -81,7 +81,7 @@ public class AliyunServiceImpl implements FileService {
|
||||
AccessControlList bucketAcl = ossClient.getBucketAcl(bucketName);
|
||||
CannedAccessControlList cannedAcl = bucketAcl.getCannedACL();
|
||||
isPrivate = "Private".equals(cannedAcl.name());
|
||||
isInitialized = true;
|
||||
isInitialized = testConnection();
|
||||
} catch (Exception e) {
|
||||
log.debug(StorageTypeEnum.ALIYUN.getDescription() + "初始化异常, 已跳过");
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package im.zhaojun.common.aspect;
|
||||
|
||||
import im.zhaojun.common.exception.StorageStrategyUninitializedException;
|
||||
import im.zhaojun.common.model.enums.StorageTypeEnum;
|
||||
import im.zhaojun.common.service.FileService;
|
||||
import im.zhaojun.common.service.SystemConfigService;
|
||||
import im.zhaojun.common.util.SpringContextHolder;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
@@ -18,10 +18,9 @@ public class StorageStrategyInitCheckAspect {
|
||||
@Before("@annotation(im.zhaojun.common.annotation.CheckStorageStrategyInit)")
|
||||
public void logStart() {
|
||||
SystemConfigService systemConfigService = SpringContextHolder.getBean(SystemConfigService.class);
|
||||
StorageTypeEnum currentStorageStrategy = systemConfigService.getCurrentStorageStrategy();
|
||||
|
||||
if (currentStorageStrategy == null) {
|
||||
throw new StorageStrategyUninitializedException("存储策略未初始化");
|
||||
FileService currentFileService = systemConfigService.getCurrentFileService();
|
||||
if (currentFileService == null || !currentFileService.getIsInitialized()) {
|
||||
throw new StorageStrategyUninitializedException("存储策略异常, 请联系管理员!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -104,4 +104,14 @@ public interface FileService {
|
||||
* @return 初始化成功与否
|
||||
*/
|
||||
boolean getIsInitialized();
|
||||
|
||||
default boolean testConnection() {
|
||||
boolean flag = true;
|
||||
try {
|
||||
fileList("/");
|
||||
} catch (Exception e) {
|
||||
flag = false;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ public class FtpServiceImpl implements FileService {
|
||||
domain = stringStorageConfigMap.get(DOMAIN_KEY).getValue();
|
||||
|
||||
ftp = new Ftp(host, Integer.parseInt(port), username, password);
|
||||
isInitialized = true;
|
||||
isInitialized = testConnection();
|
||||
} catch (Exception e) {
|
||||
log.debug(StorageTypeEnum.FTP.getDescription() + "初始化异常, 已跳过");
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ public class HuaweiServiceImpl implements FileService {
|
||||
bucketName = stringStorageConfigMap.get(BUCKET_NAME_KEY).getValue();
|
||||
domain = stringStorageConfigMap.get(DOMAIN_KEY).getValue();
|
||||
obsClient = new ObsClient(accessKey, secretKey, endPoint);
|
||||
isInitialized = true;
|
||||
isInitialized = testConnection();
|
||||
} catch (Exception e) {
|
||||
log.debug(StorageTypeEnum.HUAWEI.getDescription() + "初始化异常, 已跳过");
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ public class LocalServiceImpl implements FileService {
|
||||
Map<String, StorageConfig> stringStorageConfigMap =
|
||||
storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.LOCAL);
|
||||
filePath = stringStorageConfigMap.get(FILE_PATH_KEY).getValue();
|
||||
isInitialized = true;
|
||||
isInitialized = testConnection();
|
||||
} catch (Exception e) {
|
||||
log.debug(StorageTypeEnum.LOCAL.getDescription() + "初始化异常, 已跳过");
|
||||
}
|
||||
|
||||
@@ -68,13 +68,13 @@ public class MinIOServiceImpl implements FileService {
|
||||
minioClient = new MinioClient(endPoint, accessKey, secretKey);
|
||||
basePath = stringStorageConfigMap.get(BASE_PATH).getValue();
|
||||
basePath = basePath == null ? "" : basePath;
|
||||
isInitialized = true;
|
||||
isInitialized = testConnection();
|
||||
} catch (Exception e) {
|
||||
log.debug(StorageTypeEnum.MINIO.getDescription() + "初始化异常, 已跳过");
|
||||
}
|
||||
}
|
||||
|
||||
@Cacheable
|
||||
// @Cacheable
|
||||
@Override
|
||||
public List<FileItemDTO> fileList(String path) throws Exception {
|
||||
path = StringUtils.removeFirstSeparator(path);
|
||||
|
||||
@@ -78,7 +78,7 @@ public class QiniuServiceImpl implements FileService {
|
||||
domain = stringStorageConfigMap.get(DOMAIN_KEY).getValue();
|
||||
|
||||
isPrivate = bucketManager.getBucketInfo(bucketName).getPrivate() == 1;
|
||||
isInitialized = true;
|
||||
isInitialized = testConnection();
|
||||
} catch (Exception e) {
|
||||
log.debug(StorageTypeEnum.QINIU.getDescription() + "初始化异常, 已跳过");
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ public class TencentServiceImpl implements FileService {
|
||||
Region region = new Region(regionName);
|
||||
ClientConfig clientConfig = new ClientConfig(region);
|
||||
cosClient = new COSClient(cred, clientConfig);
|
||||
isInitialized = true;
|
||||
isInitialized = testConnection();
|
||||
} catch (Exception e) {
|
||||
log.debug(StorageTypeEnum.TENCENT.getDescription() + "初始化异常, 已跳过");
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class UpYunServiceImpl implements FileService {
|
||||
domain = stringStorageConfigMap.get(DOMAIN_KEY).getValue();
|
||||
basePath = stringStorageConfigMap.get(BASE_PATH).getValue();
|
||||
upYun = new UpYun(bucketName, username, password);
|
||||
isInitialized = true;
|
||||
isInitialized = testConnection();
|
||||
} catch (Exception e) {
|
||||
log.debug(StorageTypeEnum.UPYUN.getDescription() + "初始化异常, 已跳过");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user