Browse Source

update 增加OSS模块service自动激活

疯狂的狮子li 4 years ago
parent
commit
7cffafcdaa

+ 18 - 4
ruoyi-oss/src/main/java/com/ruoyi/oss/enumd/CloudServiceEnumd.java

@@ -1,5 +1,8 @@
 package com.ruoyi.oss.enumd;
 
+import com.ruoyi.oss.service.impl.AliyunCloudStorageServiceImpl;
+import com.ruoyi.oss.service.impl.MinioCloudStorageServiceImpl;
+import com.ruoyi.oss.service.impl.QiniuCloudStorageServiceImpl;
 import lombok.AllArgsConstructor;
 import lombok.Getter;
 
@@ -15,22 +18,33 @@ public enum CloudServiceEnumd {
 	/**
 	 * 七牛云
 	 */
-	QINIU("qiniu"),
+	QINIU("qiniu", QiniuCloudStorageServiceImpl.class),
 
 	/**
 	 * 阿里云
 	 */
-	ALIYUN("aliyun"),
+	ALIYUN("aliyun", AliyunCloudStorageServiceImpl.class),
 
 	/**
 	 * 腾讯云
 	 */
-	QCLOUD("qcloud"),
+	QCLOUD("qcloud", QiniuCloudStorageServiceImpl.class),
 
 	/**
 	 * minio
 	 */
-	MINIO("minio");
+	MINIO("minio", MinioCloudStorageServiceImpl.class);
 
 	private final String value;
+
+	private final Class<?> serviceClass;
+
+	public static Class<?> getServiceClass(String value) {
+		for (CloudServiceEnumd clazz : values()) {
+			if (clazz.getValue().equals(value)) {
+				return clazz.getServiceClass();
+			}
+		}
+		return null;
+	}
 }

+ 7 - 2
ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java

@@ -3,6 +3,7 @@ package com.ruoyi.oss.factory;
 import cn.hutool.core.lang.Assert;
 import com.ruoyi.common.utils.spring.SpringUtils;
 import com.ruoyi.oss.constant.CloudConstant;
+import com.ruoyi.oss.enumd.CloudServiceEnumd;
 import com.ruoyi.oss.service.ICloudStorageService;
 import com.ruoyi.system.service.ISysConfigService;
 
@@ -26,11 +27,15 @@ public class OssFactory {
 
 	public static ICloudStorageService instance() {
 		String type = sysConfigService.selectConfigByKey(CloudConstant.CLOUD_STORAGE_CONFIG_KEY);
-		return SERVICES.get(type);
+		return instance(type);
 	}
 
 	public static ICloudStorageService instance(String type) {
-		return SERVICES.get(type);
+		ICloudStorageService service = SERVICES.get(type);
+		if (service == null) {
+			service = (ICloudStorageService) SpringUtils.getBean(CloudServiceEnumd.getServiceClass(type));
+		}
+		return service;
 	}
 
 	public static void register(String type, ICloudStorageService iCloudStorageService) {

+ 9 - 5
ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/AliyunCloudStorageServiceImpl.java

@@ -32,11 +32,15 @@ public class AliyunCloudStorageServiceImpl extends AbstractCloudStorageService i
 	@Autowired
 	public AliyunCloudStorageServiceImpl(CloudStorageProperties properties) {
 		this.properties = properties.getAliyun();
-		ClientConfiguration configuration = new ClientConfiguration();
-		DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider(
-			this.properties.getAccessKeyId(),
-			this.properties.getAccessKeySecret());
-		client = new OSSClient(this.properties.getEndpoint(), credentialProvider, configuration);
+		try {
+			ClientConfiguration configuration = new ClientConfiguration();
+			DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider(
+				this.properties.getAccessKeyId(),
+				this.properties.getAccessKeySecret());
+			client = new OSSClient(this.properties.getEndpoint(), credentialProvider, configuration);
+		} catch (Exception e) {
+			throw new IllegalArgumentException("阿里云存储配置错误! 请检查系统配置!");
+		}
 	}
 
 	@Override

+ 8 - 4
ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/MinioCloudStorageServiceImpl.java

@@ -29,10 +29,14 @@ public class MinioCloudStorageServiceImpl extends AbstractCloudStorageService im
 	@Autowired
 	public MinioCloudStorageServiceImpl(CloudStorageProperties properties) {
 		this.properties = properties.getMinio();
-		minioClient = MinioClient.builder()
-			.endpoint(this.properties.getEndpoint())
-			.credentials(this.properties.getAccessKey(), this.properties.getSecretKey())
-			.build();
+		try {
+			minioClient = MinioClient.builder()
+				.endpoint(this.properties.getEndpoint())
+				.credentials(this.properties.getAccessKey(), this.properties.getSecretKey())
+				.build();
+		} catch (Exception e) {
+			throw new IllegalArgumentException("Minio存储配置错误! 请检查系统配置!");
+		}
 	}
 
 	@Override

+ 12 - 8
ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QcloudCloudStorageServiceImpl.java

@@ -32,14 +32,18 @@ public class QcloudCloudStorageServiceImpl extends AbstractCloudStorageService i
 	@Autowired
 	public QcloudCloudStorageServiceImpl(CloudStorageProperties properties) {
 		this.properties = properties.getQcloud();
-        COSCredentials credentials = new BasicCOSCredentials(
-			this.properties.getSecretId(),
-			this.properties.getSecretKey());
-        // 初始化客户端配置
-        ClientConfig clientConfig = new ClientConfig();
-        // 设置bucket所在的区域,华南:gz 华北:tj 华东:sh
-        clientConfig.setRegion(new Region(this.properties.getRegion()));
-        client = new COSClient(credentials, clientConfig);
+		try {
+			COSCredentials credentials = new BasicCOSCredentials(
+				this.properties.getSecretId(),
+				this.properties.getSecretKey());
+			// 初始化客户端配置
+			ClientConfig clientConfig = new ClientConfig();
+			// 设置bucket所在的区域,华南:gz 华北:tj 华东:sh
+			clientConfig.setRegion(new Region(this.properties.getRegion()));
+			client = new COSClient(credentials, clientConfig);
+		} catch (Exception e) {
+			throw new IllegalArgumentException("腾讯云存储配置错误! 请检查系统配置!");
+		}
 	}
 
 	@Override

+ 14 - 10
ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QiniuCloudStorageServiceImpl.java

@@ -36,16 +36,20 @@ public class QiniuCloudStorageServiceImpl extends AbstractCloudStorageService im
 	@Autowired
 	public QiniuCloudStorageServiceImpl(CloudStorageProperties properties) {
 		this.properties = properties.getQiniu();
-		// z0 z1 z2
-		Configuration config = new Configuration(Region.autoRegion());
-		// 默认不使用https
-		config.useHttpsDomains = false;
-		uploadManager = new UploadManager(config);
-		Auth auth = Auth.create(
-			this.properties.getAccessKey(),
-			this.properties.getSecretKey());
-		token = auth.uploadToken(this.properties.getBucketName());
-		bucketManager = new BucketManager(auth, config);
+		try {
+			// z0 z1 z2
+			Configuration config = new Configuration(Region.autoRegion());
+			// 默认不使用https
+			config.useHttpsDomains = false;
+			uploadManager = new UploadManager(config);
+			Auth auth = Auth.create(
+				this.properties.getAccessKey(),
+				this.properties.getSecretKey());
+			token = auth.uploadToken(this.properties.getBucketName());
+			bucketManager = new BucketManager(auth, config);
+		} catch (Exception e) {
+			throw new IllegalArgumentException("七牛云存储配置错误! 请检查系统配置!");
+		}
 	}