From 7cffafcdaa92f904cffe91284d641c781829b035 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期日, 18 七月 2021 19:29:33 +0800
Subject: [PATCH] update 增加OSS模块service自动激活

---
 ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QcloudCloudStorageServiceImpl.java |   70 ++++++++++++++++++++++-------------
 1 files changed, 44 insertions(+), 26 deletions(-)

diff --git a/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QcloudCloudStorageServiceImpl.java b/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QcloudCloudStorageServiceImpl.java
index 6681310..3b367b7 100644
--- a/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QcloudCloudStorageServiceImpl.java
+++ b/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QcloudCloudStorageServiceImpl.java
@@ -1,33 +1,54 @@
 package com.ruoyi.oss.service.impl;
 
-import cn.hutool.core.io.IoUtil;
 import com.qcloud.cos.COSClient;
-import com.ruoyi.oss.config.CloudStorageConfig;
+import com.qcloud.cos.ClientConfig;
+import com.qcloud.cos.auth.BasicCOSCredentials;
+import com.qcloud.cos.auth.COSCredentials;
+import com.qcloud.cos.region.Region;
+import com.ruoyi.oss.enumd.CloudServiceEnumd;
+import com.ruoyi.oss.factory.OssFactory;
+import com.ruoyi.oss.properties.CloudStorageProperties;
+import com.ruoyi.oss.properties.CloudStorageProperties.QcloudProperties;
 import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Lazy;
+import org.springframework.stereotype.Service;
 
 import java.io.InputStream;
 
 /**
  * 鑵捐浜戝瓨鍌�
+ *
+ * @author Lion Li
  */
-public class QcloudCloudStorageServiceImpl extends AbstractCloudStorageService {
+@Lazy
+@Service
+public class QcloudCloudStorageServiceImpl extends AbstractCloudStorageService implements InitializingBean {
 
-	private COSClient client;
+	private final COSClient client;
+	private final QcloudProperties properties;
 
-	public QcloudCloudStorageServiceImpl(CloudStorageConfig config) {
-		this.config = config;
-		// 鍒濆鍖�
-		init();
+	@Autowired
+	public QcloudCloudStorageServiceImpl(CloudStorageProperties properties) {
+		this.properties = properties.getQcloud();
+		try {
+			COSCredentials credentials = new BasicCOSCredentials(
+				this.properties.getSecretId(),
+				this.properties.getSecretKey());
+			// 鍒濆鍖栧鎴风閰嶇疆
+			ClientConfig clientConfig = new ClientConfig();
+			// 璁剧疆bucket鎵�鍦ㄧ殑鍖哄煙锛屽崕鍗楋細gz 鍗庡寳锛歵j 鍗庝笢锛歴h
+			clientConfig.setRegion(new Region(this.properties.getRegion()));
+			client = new COSClient(credentials, clientConfig);
+		} catch (Exception e) {
+			throw new IllegalArgumentException("鑵捐浜戝瓨鍌ㄩ厤缃敊璇�! 璇锋鏌ョ郴缁熼厤缃�!");
+		}
 	}
 
-	private void init() {
-//        Credentials credentials = new Credentials(config.getQcloudAppId(), config.getQcloudSecretId(),
-//                config.getQcloudSecretKey());
-//         鍒濆鍖栧鎴风閰嶇疆
-//        ClientConfig clientConfig = new ClientConfig();
-//        // 璁剧疆bucket鎵�鍦ㄧ殑鍖哄煙锛屽崕鍗楋細gz 鍗庡寳锛歵j 鍗庝笢锛歴h
-//        clientConfig.setRegion(config.getQcloudRegion());
-//        client = new COSClient(clientConfig, credentials);
+	@Override
+	public String getServiceType() {
+		return CloudServiceEnumd.QCLOUD.getValue();
 	}
 
 	@Override
@@ -43,7 +64,7 @@
 //        if (Convert.toInt(jsonObject.get("code")) != 0) {
 //            throw new OssException("鏂囦欢涓婁紶澶辫触锛�" + Convert.toStr(jsonObject.get("message")));
 //        }
-		return config.getDomain() + path;
+		return this.properties.getDomain() + path;
 	}
 
 	@Override
@@ -57,21 +78,18 @@
 //        }
 	}
 
-
-	@Override
-	public String upload(InputStream inputStream, String path) {
-		byte[] data = IoUtil.readBytes(inputStream);
-		return this.upload(data, path);
-	}
-
 	@Override
 	public String uploadSuffix(byte[] data, String suffix) {
-		return upload(data, getPath(config.getPrefix(), suffix));
+		return upload(data, getPath(this.properties.getPrefix(), suffix));
 	}
 
 	@Override
 	public String uploadSuffix(InputStream inputStream, String suffix) {
-		return upload(inputStream, getPath(config.getPrefix(), suffix));
+		return upload(inputStream, getPath(this.properties.getPrefix(), suffix));
 	}
 
+	@Override
+	public void afterPropertiesSet() throws Exception {
+		OssFactory.register(getServiceType(),this);
+	}
 }

--
Gitblit v1.9.3