From e7790ac25660dbcf664420562be0614e014f727d Mon Sep 17 00:00:00 2001 From: zhaojun1998 Date: Fri, 26 Jun 2020 12:35:34 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=94=B9=E8=BF=9B=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=9C=BA=E5=88=B6,=20=E7=BB=99=E4=BA=88?= =?UTF-8?q?=E6=9B=B4=E8=AF=A6=E7=BB=86=E7=9A=84=E6=8F=90=E7=A4=BA=E4=BF=A1?= =?UTF-8?q?=E6=81=AF,=20=E4=BE=BF=E4=BA=8E=E6=8E=92=E6=9F=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhaojun/zfile/context/DriveContext.java | 15 +++++- .../exception/GlobleExceptionHandler.java | 13 +++-- .../exception/InitializeDriveException.java | 29 +++++++++++ .../zfile/exception/InitializeException.java | 29 ----------- .../exception/InvalidDriveException.java | 27 +++++++++++ .../zfile/service/DriveConfigService.java | 4 +- .../service/base/AbstractBaseFileService.java | 8 ++-- .../zfile/service/impl/AliyunServiceImpl.java | 42 ++++++++-------- .../zfile/service/impl/FtpServiceImpl.java | 39 ++++++++------- .../zfile/service/impl/HuaweiServiceImpl.java | 42 ++++++++-------- .../zfile/service/impl/LocalServiceImpl.java | 29 ++++++----- .../zfile/service/impl/MinIOServiceImpl.java | 42 ++++++++-------- .../impl/OneDriveChinaServiceImpl.java | 28 +++++------ .../service/impl/OneDriveServiceImpl.java | 28 +++++------ .../zfile/service/impl/QiniuServiceImpl.java | 42 ++++++++-------- .../zfile/service/impl/S3ServiceImpl.java | 48 +++++++++---------- .../service/impl/TencentServiceImpl.java | 42 ++++++++-------- .../zfile/service/impl/UpYunServiceImpl.java | 34 ++++++------- 18 files changed, 275 insertions(+), 266 deletions(-) create mode 100644 src/main/java/im/zhaojun/zfile/exception/InitializeDriveException.java delete mode 100644 src/main/java/im/zhaojun/zfile/exception/InitializeException.java create mode 100644 src/main/java/im/zhaojun/zfile/exception/InvalidDriveException.java diff --git a/src/main/java/im/zhaojun/zfile/context/DriveContext.java b/src/main/java/im/zhaojun/zfile/context/DriveContext.java index 948e8e2..b4274bf 100644 --- a/src/main/java/im/zhaojun/zfile/context/DriveContext.java +++ b/src/main/java/im/zhaojun/zfile/context/DriveContext.java @@ -1,10 +1,12 @@ package im.zhaojun.zfile.context; +import im.zhaojun.zfile.exception.InvalidDriveException; import im.zhaojun.zfile.model.entity.DriveConfig; import im.zhaojun.zfile.model.enums.StorageTypeEnum; import im.zhaojun.zfile.service.DriveConfigService; import im.zhaojun.zfile.service.base.AbstractBaseFileService; import im.zhaojun.zfile.util.SpringContextHolder; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -24,6 +26,7 @@ import java.util.concurrent.ConcurrentHashMap; */ @Component @DependsOn("springContextHolder") +@Slf4j public class DriveContext implements ApplicationContextAware { /** @@ -43,7 +46,11 @@ public class DriveContext implements ApplicationContextAware { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { List list = driveConfigService.list(); for (DriveConfig driveConfig : list) { - init(driveConfig.getId()); + try { + init(driveConfig.getId()); + } catch (Exception e) { + log.debug(driveConfig.getName() + " 初始化异常, 已跳过", e); + } } } @@ -72,7 +79,11 @@ public class DriveContext implements ApplicationContextAware { * @return 驱动器对应的 Service */ public AbstractBaseFileService get(Integer driveId) { - return drivesServiceMap.get(driveId); + AbstractBaseFileService abstractBaseFileService = drivesServiceMap.get(driveId); + if (abstractBaseFileService == null) { + throw new InvalidDriveException("此驱动器不存在或初始化失败, 请检查后台参数配置"); + } + return abstractBaseFileService; } diff --git a/src/main/java/im/zhaojun/zfile/exception/GlobleExceptionHandler.java b/src/main/java/im/zhaojun/zfile/exception/GlobleExceptionHandler.java index caf8d98..91a52da 100644 --- a/src/main/java/im/zhaojun/zfile/exception/GlobleExceptionHandler.java +++ b/src/main/java/im/zhaojun/zfile/exception/GlobleExceptionHandler.java @@ -40,6 +40,7 @@ public class GlobleExceptionHandler { return ResultBean.error("文件不存在"); } + /** * 捕获 ClientAbortException 异常, 不做任何处理, 防止出现大量堆栈日志输出, 此异常不影响功能. */ @@ -52,6 +53,7 @@ public class GlobleExceptionHandler { // } } + /** * 文件预览异常 */ @@ -66,10 +68,10 @@ public class GlobleExceptionHandler { /** * 初始化异常 */ - @ExceptionHandler({InitializeException.class}) + @ExceptionHandler({InitializeDriveException.class}) @ResponseBody @ResponseStatus - public ResultBean initializeException(InitializeException ex) { + public ResultBean initializeException(InitializeDriveException ex) { return ResultBean.error(ex.getMessage()); } @@ -83,7 +85,12 @@ public class GlobleExceptionHandler { if (log.isDebugEnabled()) { log.debug(e.getMessage(), e); } - return ResultBean.error("系统异常, 请联系管理员"); + + if (e.getClass() == Exception.class) { + return ResultBean.error("系统异常, 请联系管理员"); + } else { + return ResultBean.error(e.getMessage()); + } } } diff --git a/src/main/java/im/zhaojun/zfile/exception/InitializeDriveException.java b/src/main/java/im/zhaojun/zfile/exception/InitializeDriveException.java new file mode 100644 index 0000000..07a62b0 --- /dev/null +++ b/src/main/java/im/zhaojun/zfile/exception/InitializeDriveException.java @@ -0,0 +1,29 @@ +package im.zhaojun.zfile.exception; + +/** + * 对象存储初始化异常 + * @author zhaojun + */ +public class InitializeDriveException extends RuntimeException { + + private static final long serialVersionUID = -1920550904063819880L; + + public InitializeDriveException() { + } + + public InitializeDriveException(String message) { + super(message); + } + + public InitializeDriveException(String message, Throwable cause) { + super(message, cause); + } + + public InitializeDriveException(Throwable cause) { + super(cause); + } + + public InitializeDriveException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/im/zhaojun/zfile/exception/InitializeException.java b/src/main/java/im/zhaojun/zfile/exception/InitializeException.java deleted file mode 100644 index 383aea3..0000000 --- a/src/main/java/im/zhaojun/zfile/exception/InitializeException.java +++ /dev/null @@ -1,29 +0,0 @@ -package im.zhaojun.zfile.exception; - -/** - * 对象存储初始化异常 - * @author zhaojun - */ -public class InitializeException extends RuntimeException { - - private static final long serialVersionUID = -1920550904063819880L; - - public InitializeException() { - } - - public InitializeException(String message) { - super(message); - } - - public InitializeException(String message, Throwable cause) { - super(message, cause); - } - - public InitializeException(Throwable cause) { - super(cause); - } - - public InitializeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } -} diff --git a/src/main/java/im/zhaojun/zfile/exception/InvalidDriveException.java b/src/main/java/im/zhaojun/zfile/exception/InvalidDriveException.java new file mode 100644 index 0000000..125f821 --- /dev/null +++ b/src/main/java/im/zhaojun/zfile/exception/InvalidDriveException.java @@ -0,0 +1,27 @@ +package im.zhaojun.zfile.exception; + +/** + * 无效的驱动器异常 + * @author zhaojun + */ +public class InvalidDriveException extends RuntimeException { + + public InvalidDriveException() { + } + + public InvalidDriveException(String message) { + super(message); + } + + public InvalidDriveException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidDriveException(Throwable cause) { + super(cause); + } + + public InvalidDriveException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/service/DriveConfigService.java b/src/main/java/im/zhaojun/zfile/service/DriveConfigService.java index 9a66f3e..5386b0e 100644 --- a/src/main/java/im/zhaojun/zfile/service/DriveConfigService.java +++ b/src/main/java/im/zhaojun/zfile/service/DriveConfigService.java @@ -3,7 +3,7 @@ package im.zhaojun.zfile.service; import im.zhaojun.zfile.cache.ZFileCache; import im.zhaojun.zfile.context.DriveContext; import im.zhaojun.zfile.context.StorageTypeContext; -import im.zhaojun.zfile.exception.InitializeException; +import im.zhaojun.zfile.exception.InitializeDriveException; import im.zhaojun.zfile.model.constant.StorageConfigConstant; import im.zhaojun.zfile.model.dto.CacheInfoDTO; import im.zhaojun.zfile.model.dto.DriveConfigDTO; @@ -185,7 +185,7 @@ public class DriveConfigService { AbstractBaseFileService driveService = driveContext.get(driveConfig.getId()); if (driveService.getIsUnInitialized()) { - throw new InitializeException("初始化异常, 请检查配置是否正确."); + throw new InitializeDriveException("初始化异常, 请检查配置是否正确."); } } diff --git a/src/main/java/im/zhaojun/zfile/service/base/AbstractBaseFileService.java b/src/main/java/im/zhaojun/zfile/service/base/AbstractBaseFileService.java index e0168e2..b4e0486 100644 --- a/src/main/java/im/zhaojun/zfile/service/base/AbstractBaseFileService.java +++ b/src/main/java/im/zhaojun/zfile/service/base/AbstractBaseFileService.java @@ -1,6 +1,7 @@ package im.zhaojun.zfile.service.base; import im.zhaojun.zfile.cache.ZFileCache; +import im.zhaojun.zfile.exception.InitializeDriveException; import im.zhaojun.zfile.model.dto.FileItemDTO; import im.zhaojun.zfile.model.entity.StorageConfig; import im.zhaojun.zfile.model.enums.StorageTypeEnum; @@ -78,15 +79,12 @@ public abstract class AbstractBaseFileService implements BaseFileService { * * @return 连接结果 */ - protected boolean testConnection() { - boolean flag = true; + protected void testConnection() { try { fileList("/"); } catch (Exception e) { - log.debug(getStorageTypeEnum().getDescription() + " 初始化异常", e); - flag = false; + throw new InitializeDriveException("初始化异常, 错误信息为: " + e.getMessage(), e); } - return flag; } diff --git a/src/main/java/im/zhaojun/zfile/service/impl/AliyunServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/AliyunServiceImpl.java index bd73e93..08754ca 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/AliyunServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/AliyunServiceImpl.java @@ -32,32 +32,28 @@ public class AliyunServiceImpl extends AbstractS3BaseFileService implements Base @Override public void init(Integer driveId) { - try { - this.driveId = driveId; - Map stringStorageConfigMap = - storageConfigService.selectStorageConfigMapByDriveId(driveId); - String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue(); - String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue(); - String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue(); + this.driveId = driveId; + Map stringStorageConfigMap = + storageConfigService.selectStorageConfigMapByDriveId(driveId); + String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue(); + String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue(); + String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue(); - super.domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue(); - super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); - super.bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue(); - super.isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true); + super.domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue(); + super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); + super.bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue(); + super.isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true); - if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) { - log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); - isInitialized = false; - } else { - BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); + if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) { + log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); + 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(); - } - } catch (Exception e) { - log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过", e); + super.s3Client = AmazonS3ClientBuilder.standard() + .withCredentials(new AWSStaticCredentialsProvider(credentials)) + .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "oss")).build(); + testConnection(); } } diff --git a/src/main/java/im/zhaojun/zfile/service/impl/FtpServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/FtpServiceImpl.java index c4ef898..bc99a20 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/FtpServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/FtpServiceImpl.java @@ -11,6 +11,7 @@ import im.zhaojun.zfile.service.StorageConfigService; import im.zhaojun.zfile.service.base.AbstractBaseFileService; import im.zhaojun.zfile.service.base.BaseFileService; import im.zhaojun.zfile.util.StringUtils; +import lombok.SneakyThrows; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClientConfig; import org.apache.commons.net.ftp.FTPFile; @@ -21,6 +22,7 @@ import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -51,28 +53,25 @@ public class FtpServiceImpl extends AbstractBaseFileService implements BaseFileS private String password; + @SneakyThrows(IOException.class) @Override public void init(Integer driveId) { - try { - this.driveId = driveId; - Map stringStorageConfigMap = - storageConfigService.selectStorageConfigMapByDriveId(driveId); - host = stringStorageConfigMap.get(StorageConfigConstant.HOST_KEY).getValue(); - port = stringStorageConfigMap.get(StorageConfigConstant.PORT_KEY).getValue(); - username = stringStorageConfigMap.get(StorageConfigConstant.USERNAME_KEY).getValue(); - password = stringStorageConfigMap.get(StorageConfigConstant.PASSWORD_KEY).getValue(); - domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue(); - super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); - if (Objects.isNull(host) || Objects.isNull(port) || Objects.isNull(username) || Objects.isNull(password)) { - isInitialized = false; - } else { - ftp = new Ftp(host, Integer.parseInt(port), username, password, StandardCharsets.UTF_8); - ftp.getClient().configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX)); - ftp.getClient().type(FTP.BINARY_FILE_TYPE); - isInitialized = testConnection(); - } - } catch (Exception e) { - log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过"); + this.driveId = driveId; + Map stringStorageConfigMap = + storageConfigService.selectStorageConfigMapByDriveId(driveId); + host = stringStorageConfigMap.get(StorageConfigConstant.HOST_KEY).getValue(); + port = stringStorageConfigMap.get(StorageConfigConstant.PORT_KEY).getValue(); + username = stringStorageConfigMap.get(StorageConfigConstant.USERNAME_KEY).getValue(); + password = stringStorageConfigMap.get(StorageConfigConstant.PASSWORD_KEY).getValue(); + domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue(); + super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); + if (Objects.isNull(host) || Objects.isNull(port)) { + isInitialized = false; + } else { + ftp = new Ftp(host, Integer.parseInt(port), username, password, StandardCharsets.UTF_8); + ftp.getClient().configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX)); + ftp.getClient().type(FTP.BINARY_FILE_TYPE); + testConnection(); } } diff --git a/src/main/java/im/zhaojun/zfile/service/impl/HuaweiServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/HuaweiServiceImpl.java index b68c5fa..6ece436 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/HuaweiServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/HuaweiServiceImpl.java @@ -32,32 +32,28 @@ public class HuaweiServiceImpl extends AbstractS3BaseFileService implements Base @Override public void init(Integer driveId) { - try { - this.driveId = driveId; - Map stringStorageConfigMap = - storageConfigService.selectStorageConfigMapByDriveId(driveId); - String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue(); - String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue(); - String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue(); + this.driveId = driveId; + Map stringStorageConfigMap = + storageConfigService.selectStorageConfigMapByDriveId(driveId); + String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue(); + String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_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(); - isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true); + bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue(); + domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue(); + basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); + isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true); - if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) { - log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); - isInitialized = false; - } else { - 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)) { + log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); + 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(); - } - } catch (Exception e) { - log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过"); + testConnection(); } } diff --git a/src/main/java/im/zhaojun/zfile/service/impl/LocalServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/LocalServiceImpl.java index d5da2c1..451c0e2 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/LocalServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/LocalServiceImpl.java @@ -1,5 +1,6 @@ package im.zhaojun.zfile.service.impl; +import im.zhaojun.zfile.exception.InitializeDriveException; import im.zhaojun.zfile.exception.NotExistFileException; import im.zhaojun.zfile.model.constant.StorageConfigConstant; import im.zhaojun.zfile.model.constant.SystemConfigConstant; @@ -48,19 +49,21 @@ public class LocalServiceImpl extends AbstractBaseFileService implements BaseFil @Override public void init(Integer driveId) { - try { - this.driveId = driveId; - Map stringStorageConfigMap = - storageConfigService.selectStorageConfigMapByDriveId(driveId); - filePath = stringStorageConfigMap.get(StorageConfigConstant.FILE_PATH_KEY).getValue(); - if (Objects.isNull(filePath)) { - log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); - isInitialized = false; - } else { - isInitialized = testConnection(); - } - } catch (Exception e) { - log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过"); + this.driveId = driveId; + Map stringStorageConfigMap = + storageConfigService.selectStorageConfigMapByDriveId(driveId); + filePath = stringStorageConfigMap.get(StorageConfigConstant.FILE_PATH_KEY).getValue(); + if (Objects.isNull(filePath)) { + log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); + isInitialized = false; + return; + } + + File file = new File(filePath); + if (!file.exists()) { + throw new InitializeDriveException("文件路径: \"" + file.getAbsolutePath() + "\"不存在, 请检查是否填写正确."); + } else { + testConnection(); } } diff --git a/src/main/java/im/zhaojun/zfile/service/impl/MinIOServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/MinIOServiceImpl.java index 94dae5d..47c361f 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/MinIOServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/MinIOServiceImpl.java @@ -32,31 +32,27 @@ public class MinIOServiceImpl extends AbstractS3BaseFileService implements BaseF @Override public void init(Integer driveId) { - try { - this.driveId = driveId; - Map stringStorageConfigMap = - storageConfigService.selectStorageConfigMapByDriveId(driveId); - String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue(); - String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue(); - String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue(); - bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue(); - basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); - isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true); + this.driveId = driveId; + Map stringStorageConfigMap = + storageConfigService.selectStorageConfigMapByDriveId(driveId); + String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue(); + String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue(); + String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue(); + bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue(); + basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); + isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true); - if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) { - log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); - isInitialized = false; - } else { - BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); - s3Client = AmazonS3ClientBuilder.standard() - .withPathStyleAccessEnabled(true) - .withCredentials(new AWSStaticCredentialsProvider(credentials)) - .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "minio")).build(); + if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) { + log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); + isInitialized = false; + } else { + BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); + s3Client = AmazonS3ClientBuilder.standard() + .withPathStyleAccessEnabled(true) + .withCredentials(new AWSStaticCredentialsProvider(credentials)) + .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "minio")).build(); - isInitialized = testConnection(); - } - } catch (Exception e) { - log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过"); + testConnection(); } } diff --git a/src/main/java/im/zhaojun/zfile/service/impl/OneDriveChinaServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/OneDriveChinaServiceImpl.java index 6835a8f..db078b6 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/OneDriveChinaServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/OneDriveChinaServiceImpl.java @@ -41,23 +41,19 @@ public class OneDriveChinaServiceImpl extends AbstractOneDriveServiceBase implem @Override public void init(Integer driveId) { - try { - this.driveId = driveId; - Map stringStorageConfigMap = - storageConfigService.selectStorageConfigMapByDriveId(driveId); - String accessToken = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_TOKEN_KEY).getValue(); - String refreshToken = stringStorageConfigMap.get(StorageConfigConstant.REFRESH_TOKEN_KEY).getValue(); - super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); + this.driveId = driveId; + Map stringStorageConfigMap = + storageConfigService.selectStorageConfigMapByDriveId(driveId); + String accessToken = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_TOKEN_KEY).getValue(); + String refreshToken = stringStorageConfigMap.get(StorageConfigConstant.REFRESH_TOKEN_KEY).getValue(); + super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); - if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(refreshToken)) { - log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); - isInitialized = false; - } else { - refreshOneDriveToken(); - isInitialized = testConnection(); - } - } catch (Exception e) { - log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过"); + if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(refreshToken)) { + log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); + isInitialized = false; + } else { + refreshOneDriveToken(); + testConnection(); } } diff --git a/src/main/java/im/zhaojun/zfile/service/impl/OneDriveServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/OneDriveServiceImpl.java index f08de7e..37a079e 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/OneDriveServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/OneDriveServiceImpl.java @@ -41,23 +41,19 @@ public class OneDriveServiceImpl extends AbstractOneDriveServiceBase implements @Override public void init(Integer driveId) { - try { - this.driveId = driveId; - Map stringStorageConfigMap = - storageConfigService.selectStorageConfigMapByDriveId(driveId); - String accessToken = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_TOKEN_KEY).getValue(); - String refreshToken = stringStorageConfigMap.get(StorageConfigConstant.REFRESH_TOKEN_KEY).getValue(); - super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); + this.driveId = driveId; + Map stringStorageConfigMap = + storageConfigService.selectStorageConfigMapByDriveId(driveId); + String accessToken = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_TOKEN_KEY).getValue(); + String refreshToken = stringStorageConfigMap.get(StorageConfigConstant.REFRESH_TOKEN_KEY).getValue(); + super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); - if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(refreshToken)) { - log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); - isInitialized = false; - } else { - refreshOneDriveToken(); - isInitialized = testConnection(); - } - } catch (Exception e) { - log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过"); + if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(refreshToken)) { + log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); + isInitialized = false; + } else { + refreshOneDriveToken(); + testConnection(); } } diff --git a/src/main/java/im/zhaojun/zfile/service/impl/QiniuServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/QiniuServiceImpl.java index cfa3ccf..b832efa 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/QiniuServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/QiniuServiceImpl.java @@ -32,32 +32,28 @@ public class QiniuServiceImpl extends AbstractS3BaseFileService implements BaseF @Override public void init(Integer driveId) { - try { - this.driveId = driveId; - Map stringStorageConfigMap = - storageConfigService.selectStorageConfigMapByDriveId(driveId); - String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue(); - String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue(); - String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue(); + this.driveId = driveId; + Map stringStorageConfigMap = + storageConfigService.selectStorageConfigMapByDriveId(driveId); + String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue(); + String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_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(); - isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true); + bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue(); + domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue(); + basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); + isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true); - if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) { - log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); - isInitialized = false; - } else { - 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)) { + log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); + 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(); - } - } catch (Exception e) { - log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过"); + testConnection(); } } diff --git a/src/main/java/im/zhaojun/zfile/service/impl/S3ServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/S3ServiceImpl.java index 792fc53..90b62b7 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/S3ServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/S3ServiceImpl.java @@ -32,37 +32,33 @@ public class S3ServiceImpl extends AbstractS3BaseFileService implements BaseFile @Override public void init(Integer driveId) { - try { - this.driveId = driveId; - Map stringStorageConfigMap = - storageConfigService.selectStorageConfigMapByDriveId(driveId); - String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue(); - String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue(); - String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue(); + this.driveId = driveId; + Map stringStorageConfigMap = + storageConfigService.selectStorageConfigMapByDriveId(driveId); + String accessKey = stringStorageConfigMap.get(StorageConfigConstant.ACCESS_KEY).getValue(); + String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_KEY).getValue(); + String endPoint = stringStorageConfigMap.get(StorageConfigConstant.ENDPOINT_KEY).getValue(); - super.domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue(); - super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); - super.bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue(); - super.isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true); + super.domain = stringStorageConfigMap.get(StorageConfigConstant.DOMAIN_KEY).getValue(); + super.basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); + super.bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_KEY).getValue(); + super.isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true); - String pathStyle = stringStorageConfigMap.get(StorageConfigConstant.PATH_STYLE).getValue(); + String pathStyle = stringStorageConfigMap.get(StorageConfigConstant.PATH_STYLE).getValue(); - boolean isPathStyle = "path-style".equals(pathStyle); + boolean isPathStyle = "path-style".equals(pathStyle); - if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) { - log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); - isInitialized = false; - } else { - BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); - s3Client = AmazonS3ClientBuilder.standard() - .withPathStyleAccessEnabled(isPathStyle) - .withCredentials(new AWSStaticCredentialsProvider(credentials)) - .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "")).build(); + if (Objects.isNull(accessKey) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) { + log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); + isInitialized = false; + } else { + BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); + s3Client = AmazonS3ClientBuilder.standard() + .withPathStyleAccessEnabled(isPathStyle) + .withCredentials(new AWSStaticCredentialsProvider(credentials)) + .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "")).build(); - isInitialized = testConnection(); - } - } catch (Exception e) { - log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过"); + testConnection(); } } diff --git a/src/main/java/im/zhaojun/zfile/service/impl/TencentServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/TencentServiceImpl.java index ecea8d3..8e12c6e 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/TencentServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/TencentServiceImpl.java @@ -32,31 +32,27 @@ public class TencentServiceImpl extends AbstractS3BaseFileService implements Bas @Override public void init(Integer driveId) { - try { - this.driveId = driveId; - Map stringStorageConfigMap = - storageConfigService.selectStorageConfigMapByDriveId(driveId); - String secretId = stringStorageConfigMap.get(StorageConfigConstant.SECRET_ID_KEY).getValue(); - String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_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(); - isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true); + this.driveId = driveId; + Map stringStorageConfigMap = + storageConfigService.selectStorageConfigMapByDriveId(driveId); + String secretId = stringStorageConfigMap.get(StorageConfigConstant.SECRET_ID_KEY).getValue(); + String secretKey = stringStorageConfigMap.get(StorageConfigConstant.SECRET_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(); + isPrivate = Convert.toBool(stringStorageConfigMap.get(StorageConfigConstant.IS_PRIVATE).getValue(), true); - if (Objects.isNull(secretId) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) { - log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); - isInitialized = false; - } else { - BasicAWSCredentials credentials = new BasicAWSCredentials(secretId, secretKey); - s3Client = AmazonS3ClientBuilder.standard() - .withCredentials(new AWSStaticCredentialsProvider(credentials)) - .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "cos")).build(); + if (Objects.isNull(secretId) || Objects.isNull(secretKey) || Objects.isNull(endPoint) || Objects.isNull(bucketName)) { + log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); + 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(); - } - } catch (Exception e) { - log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过"); + testConnection(); } } diff --git a/src/main/java/im/zhaojun/zfile/service/impl/UpYunServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/UpYunServiceImpl.java index 374a3bb..d87c86d 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/UpYunServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/UpYunServiceImpl.java @@ -50,26 +50,22 @@ public class UpYunServiceImpl extends AbstractBaseFileService implements BaseFil @Override public void init(Integer driveId) { - try { - this.driveId = driveId; - Map stringStorageConfigMap = - storageConfigService.selectStorageConfigMapByDriveId(driveId); - String bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_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(); - basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); - basePath = ObjectUtil.defaultIfNull(basePath, ""); + this.driveId = driveId; + Map stringStorageConfigMap = + storageConfigService.selectStorageConfigMapByDriveId(driveId); + String bucketName = stringStorageConfigMap.get(StorageConfigConstant.BUCKET_NAME_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(); + basePath = stringStorageConfigMap.get(StorageConfigConstant.BASE_PATH).getValue(); + basePath = ObjectUtil.defaultIfNull(basePath, ""); - if (Objects.isNull(bucketName) || Objects.isNull(username) || Objects.isNull(password)) { - log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); - isInitialized = false; - } else { - upYun = new UpYun(bucketName, username, password); - isInitialized = testConnection(); - } - } catch (Exception e) { - log.debug(getStorageTypeEnum().getDescription() + " 初始化异常, 已跳过"); + if (Objects.isNull(bucketName) || Objects.isNull(username) || Objects.isNull(password)) { + log.debug("初始化存储策略 [{}] 失败: 参数不完整", getStorageTypeEnum().getDescription()); + isInitialized = false; + } else { + upYun = new UpYun(bucketName, username, password); + testConnection(); } }