diff --git a/src/main/java/im/zhaojun/aliyun/service/AliyunServiceImpl.java b/src/main/java/im/zhaojun/aliyun/service/AliyunServiceImpl.java index fd01d33..5554abd 100644 --- a/src/main/java/im/zhaojun/aliyun/service/AliyunServiceImpl.java +++ b/src/main/java/im/zhaojun/aliyun/service/AliyunServiceImpl.java @@ -14,6 +14,7 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.util.Map; +import java.util.Objects; /** * @author zhaojun @@ -35,12 +36,18 @@ public class AliyunServiceImpl extends AbstractS3FileService implements FileServ super.domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue(); super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); - BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); - super.s3Client = AmazonS3ClientBuilder.standard() - .withCredentials(new AWSStaticCredentialsProvider(credentials)) - .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "oss")).build(); + if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) { + isInitialized = false; + } else { + BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); + + super.s3Client = AmazonS3ClientBuilder.standard() + .withCredentials(new AWSStaticCredentialsProvider(credentials)) + .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "oss")).build(); + isInitialized = testConnection(); + } + - isInitialized = testConnection(); } catch (Exception e) { log.debug(getStorageTypeEnum().getDescription() + "初始化异常, 已跳过"); } diff --git a/src/main/java/im/zhaojun/ftp/service/FtpServiceImpl.java b/src/main/java/im/zhaojun/ftp/service/FtpServiceImpl.java index f06091f..afd1001 100644 --- a/src/main/java/im/zhaojun/ftp/service/FtpServiceImpl.java +++ b/src/main/java/im/zhaojun/ftp/service/FtpServiceImpl.java @@ -14,13 +14,13 @@ import im.zhaojun.common.util.StringUtils; import org.apache.commons.net.ftp.FTPFile; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Objects; /** * @author zhaojun @@ -39,24 +39,28 @@ public class FtpServiceImpl extends AbstractFileService implements FileService { @Override public void init() { - try { - Map stringStorageConfigMap = - storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.FTP); - String host = stringStorageConfigMap.get(StorageConfigConstant.HOST_KEY).getValue(); - String port = stringStorageConfigMap.get(StorageConfigConstant.PORT_KEY).getValue(); - String username = stringStorageConfigMap.get(StorageConfigConstant.USERNAME_KEY).getValue(); - String password = stringStorageConfigMap.get(StorageConfigConstant.PASSWORD_KEY).getValue(); - domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue(); + try { + Map stringStorageConfigMap = + storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.FTP); + String host = stringStorageConfigMap.get(StorageConfigConstant.HOST_KEY).getValue(); + String port = stringStorageConfigMap.get(StorageConfigConstant.PORT_KEY).getValue(); + String username = stringStorageConfigMap.get(StorageConfigConstant.USERNAME_KEY).getValue(); + String password = stringStorageConfigMap.get(StorageConfigConstant.PASSWORD_KEY).getValue(); + domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue(); - ftp = new Ftp(host, Integer.parseInt(port), username, password); - isInitialized = testConnection(); - } catch (Exception e) { - log.debug(getStorageTypeEnum().getDescription() + "初始化异常, 已跳过"); - } + if (Objects.isNull(host) || Objects.isNull(port) || Objects.isNull(username) || Objects.isNull(password)) { + isInitialized = true; + } else { + ftp = new Ftp(host, Integer.parseInt(port), username, password); + isInitialized = testConnection(); + } + + } catch (Exception e) { + log.debug(getStorageTypeEnum().getDescription() + "初始化异常, 已跳过"); + } } @Override - @Cacheable public List fileList(String path) { FTPFile[] ftpFiles = ftp.lsFiles(path); @@ -78,7 +82,6 @@ public class FtpServiceImpl extends AbstractFileService implements FileService { } @Override - @Cacheable public String getDownloadUrl(String path) { return URLUtil.complateUrl(domain, path); } diff --git a/src/main/java/im/zhaojun/huawei/service/HuaweiServiceImpl.java b/src/main/java/im/zhaojun/huawei/service/HuaweiServiceImpl.java index 5a536c7..407b3fe 100644 --- a/src/main/java/im/zhaojun/huawei/service/HuaweiServiceImpl.java +++ b/src/main/java/im/zhaojun/huawei/service/HuaweiServiceImpl.java @@ -14,6 +14,7 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.util.Map; +import java.util.Objects; /** * @author zhaojun @@ -35,12 +36,16 @@ public class HuaweiServiceImpl extends AbstractS3FileService implements FileServ domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue(); basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); - BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); - s3Client = AmazonS3ClientBuilder.standard() - .withCredentials(new AWSStaticCredentialsProvider(credentials)) - .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "obs")).build(); + if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) { + isInitialized = false; + } else { + BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); + s3Client = AmazonS3ClientBuilder.standard() + .withCredentials(new AWSStaticCredentialsProvider(credentials)) + .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "obs")).build(); - isInitialized = testConnection(); + isInitialized = testConnection(); + } } catch (Exception e) { log.debug(getStorageTypeEnum().getDescription() + "初始化异常, 已跳过"); } diff --git a/src/main/java/im/zhaojun/local/service/LocalServiceImpl.java b/src/main/java/im/zhaojun/local/service/LocalServiceImpl.java index b869c52..4af7b4a 100644 --- a/src/main/java/im/zhaojun/local/service/LocalServiceImpl.java +++ b/src/main/java/im/zhaojun/local/service/LocalServiceImpl.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; +import java.util.Objects; /** * @author zhaojun @@ -45,7 +46,11 @@ public class LocalServiceImpl extends AbstractFileService implements FileService Map stringStorageConfigMap = storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.LOCAL); filePath = stringStorageConfigMap.get(StorageConfigConstant.FILE_PATH_KEY).getValue(); - isInitialized = testConnection(); + if (Objects.isNull(filePath)) { + isInitialized = false; + } else { + isInitialized = testConnection(); + } } catch (Exception e) { log.debug(getStorageTypeEnum().getDescription() + "初始化异常, 已跳过"); } diff --git a/src/main/java/im/zhaojun/minio/MinIOServiceImpl.java b/src/main/java/im/zhaojun/minio/MinIOServiceImpl.java index 19b570c..a9f8df8 100644 --- a/src/main/java/im/zhaojun/minio/MinIOServiceImpl.java +++ b/src/main/java/im/zhaojun/minio/MinIOServiceImpl.java @@ -14,6 +14,7 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.util.Map; +import java.util.Objects; /** * @author zhaojun @@ -33,12 +34,16 @@ public class MinIOServiceImpl extends AbstractS3FileService implements FileServi bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue(); basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); - BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); - s3Client = AmazonS3ClientBuilder.standard() - .withCredentials(new AWSStaticCredentialsProvider(credentials)) - .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "minio")).build(); + if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) { + isInitialized = false; + } else { + BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); + s3Client = AmazonS3ClientBuilder.standard() + .withCredentials(new AWSStaticCredentialsProvider(credentials)) + .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "minio")).build(); - isInitialized = testConnection(); + isInitialized = testConnection(); + } } catch (Exception e) { log.debug(getStorageTypeEnum().getDescription() + "初始化异常, 已跳过"); } diff --git a/src/main/java/im/zhaojun/qiniu/service/QiniuServiceImpl.java b/src/main/java/im/zhaojun/qiniu/service/QiniuServiceImpl.java index 9bd13e9..ca86480 100644 --- a/src/main/java/im/zhaojun/qiniu/service/QiniuServiceImpl.java +++ b/src/main/java/im/zhaojun/qiniu/service/QiniuServiceImpl.java @@ -11,10 +11,10 @@ import im.zhaojun.common.service.AbstractS3FileService; import im.zhaojun.common.service.FileService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.Map; +import java.util.Objects; /** * @author zhaojun @@ -36,12 +36,16 @@ public class QiniuServiceImpl extends AbstractS3FileService implements FileServi domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue(); basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); - BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); - s3Client = AmazonS3ClientBuilder.standard() - .withCredentials(new AWSStaticCredentialsProvider(credentials)) - .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "kodo")).build(); + if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) { + isInitialized = false; + } else { + BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); + s3Client = AmazonS3ClientBuilder.standard() + .withCredentials(new AWSStaticCredentialsProvider(credentials)) + .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "kodo")).build(); - isInitialized = testConnection(); + isInitialized = testConnection(); + } } catch (Exception e) { log.debug(getStorageTypeEnum().getDescription() + "初始化异常, 已跳过"); } diff --git a/src/main/java/im/zhaojun/tencent/TencentServiceImpl.java b/src/main/java/im/zhaojun/tencent/TencentServiceImpl.java index 038815b..519dee1 100644 --- a/src/main/java/im/zhaojun/tencent/TencentServiceImpl.java +++ b/src/main/java/im/zhaojun/tencent/TencentServiceImpl.java @@ -14,6 +14,7 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.util.Map; +import java.util.Objects; /** * @author zhaojun @@ -29,17 +30,21 @@ public class TencentServiceImpl extends AbstractS3FileService implements FileSer Map stringStorageConfigMap = storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.TENCENT); String secretId = stringStorageConfigMap.get(StorageConfigConstant.SECRET_ID_KEY).getValue(); String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue(); - String regionName = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue(); + String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue(); bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue(); domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue(); basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); - BasicAWSCredentials credentials = new BasicAWSCredentials(secretId, secretKey); - s3Client = AmazonS3ClientBuilder.standard() - .withCredentials(new AWSStaticCredentialsProvider(credentials)) - .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(regionName, "cos")).build(); + if (Objects.isNull(secretId) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) { + isInitialized = false; + } else { + BasicAWSCredentials credentials = new BasicAWSCredentials(secretId, secretKey); + s3Client = AmazonS3ClientBuilder.standard() + .withCredentials(new AWSStaticCredentialsProvider(credentials)) + .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "cos")).build(); - isInitialized = testConnection(); + isInitialized = testConnection(); + } } catch (Exception e) { log.debug(getStorageTypeEnum().getDescription() + "初始化异常, 已跳过"); } diff --git a/src/main/java/im/zhaojun/upyun/service/UpYunServiceImpl.java b/src/main/java/im/zhaojun/upyun/service/UpYunServiceImpl.java index 2948f3a..9b5339d 100644 --- a/src/main/java/im/zhaojun/upyun/service/UpYunServiceImpl.java +++ b/src/main/java/im/zhaojun/upyun/service/UpYunServiceImpl.java @@ -1,5 +1,6 @@ package im.zhaojun.upyun.service; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.URLUtil; import com.UpYun; import im.zhaojun.common.model.StorageConfig; @@ -13,7 +14,6 @@ import im.zhaojun.common.service.StorageConfigService; import im.zhaojun.common.util.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; /** * @author zhaojun @@ -51,15 +52,20 @@ public class UpYunServiceImpl extends AbstractFileService implements FileService String password = stringStorageConfigMap.get(StorageConfigConstant.PASSWORD_KEY).getValue(); domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue(); basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); - upYun = new UpYun(bucketName, username, password); - isInitialized = testConnection(); + basePath = ObjectUtil.defaultIfNull(basePath, ""); + + if (Objects.isNull(bucketName) || Objects.isNull(username) || Objects.isNull(password)) { + isInitialized = false; + } else { + upYun = new UpYun(bucketName, username, password); + isInitialized = testConnection(); + } } catch (Exception e) { log.debug(getStorageTypeEnum().getDescription() + "初始化异常, 已跳过"); } } @Override - @Cacheable public List fileList(String path) throws Exception { ArrayList fileItemList = new ArrayList<>(); String nextMark = null; @@ -94,7 +100,6 @@ public class UpYunServiceImpl extends AbstractFileService implements FileService } @Override - @Cacheable public String getDownloadUrl(String path) { return URLUtil.complateUrl(domain, path); }