From 22fd00832df867b2927d6fe67cdab5316096ebc4 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期五, 23 七月 2021 15:35:28 +0800
Subject: [PATCH] update 更新OSS模块 七牛云相关代码

---
 ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QiniuCloudStorageServiceImpl.java |   62 ++++++++++++++++++++++--------
 1 files changed, 45 insertions(+), 17 deletions(-)

diff --git a/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QiniuCloudStorageServiceImpl.java b/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QiniuCloudStorageServiceImpl.java
index 2f7a246..944dd23 100644
--- a/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QiniuCloudStorageServiceImpl.java
+++ b/ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QiniuCloudStorageServiceImpl.java
@@ -1,11 +1,13 @@
 package com.ruoyi.oss.service.impl;
 
+import cn.hutool.core.util.ArrayUtil;
 import com.qiniu.http.Response;
 import com.qiniu.storage.BucketManager;
 import com.qiniu.storage.Configuration;
 import com.qiniu.storage.Region;
 import com.qiniu.storage.UploadManager;
 import com.qiniu.util.Auth;
+import com.ruoyi.oss.entity.UploadResult;
 import com.ruoyi.oss.enumd.CloudServiceEnumd;
 import com.ruoyi.oss.exception.OssException;
 import com.ruoyi.oss.factory.OssFactory;
@@ -36,18 +38,27 @@
 	@Autowired
 	public QiniuCloudStorageServiceImpl(CloudStorageProperties properties) {
 		this.properties = properties.getQiniu();
-		// z0 z1 z2
-		Configuration config = new Configuration(Region.autoRegion());
-		// 榛樿涓嶄娇鐢╤ttps
-		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 {
+			Configuration config = new Configuration(getRegion(this.properties.getRegion()));
+			// https璁剧疆
+			config.useHttpsDomains = false;
+			if (this.properties.getIsHttps() != null) {
+				config.useHttpsDomains = this.properties.getIsHttps();
+			}
+			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);
 
+			if (!ArrayUtil.contains(bucketManager.buckets(), this.properties.getBucketName())) {
+				bucketManager.createBucket(this.properties.getBucketName(), this.properties.getRegion());
+			}
+		} catch (Exception e) {
+			throw new IllegalArgumentException("涓冪墰浜戝瓨鍌ㄩ厤缃敊璇�! 璇锋鏌ョ郴缁熼厤缃�!");
+		}
+	}
 
 	@Override
 	public String getServiceType() {
@@ -55,7 +66,7 @@
 	}
 
 	@Override
-	public String upload(byte[] data, String path) {
+	public UploadResult upload(byte[] data, String path, String contentType) {
 		try {
 			Response res = uploadManager.put(data, path, token);
 			if (!res.isOK()) {
@@ -64,7 +75,7 @@
 		} catch (Exception e) {
 			throw new OssException("涓婁紶鏂囦欢澶辫触锛岃鏍稿涓冪墰閰嶇疆淇℃伅");
 		}
-		return this.properties.getDomain() + "/" + path;
+		return new UploadResult().setUrl(properties.getDomain() + "/" + path).setFilename(path);
 	}
 
 	@Override
@@ -81,13 +92,13 @@
 	}
 
 	@Override
-	public String uploadSuffix(byte[] data, String suffix) {
-		return upload(data, getPath(this.properties.getPrefix(), suffix));
+	public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) {
+		return upload(data, getPath(this.properties.getPrefix(), suffix), contentType);
 	}
 
 	@Override
-	public String uploadSuffix(InputStream inputStream, String suffix) {
-		return upload(inputStream, getPath(this.properties.getPrefix(), suffix));
+	public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) {
+		return upload(inputStream, getPath(this.properties.getPrefix(), suffix), contentType);
 	}
 
 	@Override
@@ -95,4 +106,21 @@
 		OssFactory.register(getServiceType(),this);
 	}
 
+	private Region getRegion(String region) {
+		switch (region) {
+			case "z0":
+				return Region.region0();
+			case "z1":
+				return Region.region1();
+			case "z2":
+				return Region.region2();
+			case "na0":
+				return Region.regionNa0();
+			case "as0":
+				return Region.regionAs0();
+			default:
+				return Region.autoRegion();
+		}
+	}
+
 }

--
Gitblit v1.9.3