diff --git a/src/main/java/im/zhaojun/aliyun/service/AliyunServiceImpl.java b/src/main/java/im/zhaojun/aliyun/service/AliyunServiceImpl.java index d7c9037..96be582 100644 --- a/src/main/java/im/zhaojun/aliyun/service/AliyunServiceImpl.java +++ b/src/main/java/im/zhaojun/aliyun/service/AliyunServiceImpl.java @@ -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() + "初始化异常, 已跳过"); } diff --git a/src/main/java/im/zhaojun/common/aspect/StorageStrategyInitCheckAspect.java b/src/main/java/im/zhaojun/common/aspect/StorageStrategyInitCheckAspect.java index befbdd4..b3ebe7d 100644 --- a/src/main/java/im/zhaojun/common/aspect/StorageStrategyInitCheckAspect.java +++ b/src/main/java/im/zhaojun/common/aspect/StorageStrategyInitCheckAspect.java @@ -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("存储策略异常, 请联系管理员!"); } } diff --git a/src/main/java/im/zhaojun/common/service/FileService.java b/src/main/java/im/zhaojun/common/service/FileService.java index e11c00c..e1b6c06 100644 --- a/src/main/java/im/zhaojun/common/service/FileService.java +++ b/src/main/java/im/zhaojun/common/service/FileService.java @@ -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; + } } diff --git a/src/main/java/im/zhaojun/ftp/service/FtpServiceImpl.java b/src/main/java/im/zhaojun/ftp/service/FtpServiceImpl.java index 3058ea8..a389070 100644 --- a/src/main/java/im/zhaojun/ftp/service/FtpServiceImpl.java +++ b/src/main/java/im/zhaojun/ftp/service/FtpServiceImpl.java @@ -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() + "初始化异常, 已跳过"); } diff --git a/src/main/java/im/zhaojun/huawei/service/HuaweiServiceImpl.java b/src/main/java/im/zhaojun/huawei/service/HuaweiServiceImpl.java index 52cb3a1..616c2d5 100644 --- a/src/main/java/im/zhaojun/huawei/service/HuaweiServiceImpl.java +++ b/src/main/java/im/zhaojun/huawei/service/HuaweiServiceImpl.java @@ -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() + "初始化异常, 已跳过"); } diff --git a/src/main/java/im/zhaojun/local/service/LocalServiceImpl.java b/src/main/java/im/zhaojun/local/service/LocalServiceImpl.java index d70b16c..a4fb2bd 100644 --- a/src/main/java/im/zhaojun/local/service/LocalServiceImpl.java +++ b/src/main/java/im/zhaojun/local/service/LocalServiceImpl.java @@ -47,7 +47,7 @@ public class LocalServiceImpl implements FileService { Map stringStorageConfigMap = storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.LOCAL); filePath = stringStorageConfigMap.get(FILE_PATH_KEY).getValue(); - isInitialized = true; + isInitialized = testConnection(); } catch (Exception e) { log.debug(StorageTypeEnum.LOCAL.getDescription() + "初始化异常, 已跳过"); } diff --git a/src/main/java/im/zhaojun/minio/MinIOServiceImpl.java b/src/main/java/im/zhaojun/minio/MinIOServiceImpl.java index 2e94b79..ce796be 100644 --- a/src/main/java/im/zhaojun/minio/MinIOServiceImpl.java +++ b/src/main/java/im/zhaojun/minio/MinIOServiceImpl.java @@ -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 fileList(String path) throws Exception { path = StringUtils.removeFirstSeparator(path); diff --git a/src/main/java/im/zhaojun/qiniu/service/QiniuServiceImpl.java b/src/main/java/im/zhaojun/qiniu/service/QiniuServiceImpl.java index 7616b2e..ddc2643 100644 --- a/src/main/java/im/zhaojun/qiniu/service/QiniuServiceImpl.java +++ b/src/main/java/im/zhaojun/qiniu/service/QiniuServiceImpl.java @@ -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() + "初始化异常, 已跳过"); } diff --git a/src/main/java/im/zhaojun/tencent/TencentServiceImpl.java b/src/main/java/im/zhaojun/tencent/TencentServiceImpl.java index 69b4155..81bb10f 100644 --- a/src/main/java/im/zhaojun/tencent/TencentServiceImpl.java +++ b/src/main/java/im/zhaojun/tencent/TencentServiceImpl.java @@ -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() + "初始化异常, 已跳过"); } diff --git a/src/main/java/im/zhaojun/upyun/service/UpYunServiceImpl.java b/src/main/java/im/zhaojun/upyun/service/UpYunServiceImpl.java index 9e4dd4e..c6c0d86 100644 --- a/src/main/java/im/zhaojun/upyun/service/UpYunServiceImpl.java +++ b/src/main/java/im/zhaojun/upyun/service/UpYunServiceImpl.java @@ -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() + "初始化异常, 已跳过"); }