From f8ab5663ef7220f8adc0fd8fcbd7d0cf84c6d77d Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期一, 02 八月 2021 12:15:14 +0800
Subject: [PATCH] update 日常字符串校验 统一重构到 StringUtils 便于维护扩展

---
 ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QcloudCloudStorageServiceImpl.java |  109 ++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 77 insertions(+), 32 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 4cc7bf2..523dff6 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,20 +1,26 @@
 package com.ruoyi.oss.service.impl;
 
+import com.ruoyi.common.utils.StringUtils;
 import com.qcloud.cos.COSClient;
 import com.qcloud.cos.ClientConfig;
 import com.qcloud.cos.auth.BasicCOSCredentials;
 import com.qcloud.cos.auth.COSCredentials;
+import com.qcloud.cos.http.HttpProtocol;
+import com.qcloud.cos.model.*;
 import com.qcloud.cos.region.Region;
+import com.ruoyi.oss.entity.UploadResult;
 import com.ruoyi.oss.enumd.CloudServiceEnumd;
+import com.ruoyi.oss.exception.OssException;
 import com.ruoyi.oss.factory.OssFactory;
 import com.ruoyi.oss.properties.CloudStorageProperties;
-import com.ruoyi.oss.properties.QcloudProperties;
+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.ByteArrayInputStream;
 import java.io.InputStream;
 
 /**
@@ -32,14 +38,39 @@
 	@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 鍗庡寳锛歵j 鍗庝笢锛歴h
-        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 鍗庡寳锛歵j 鍗庝笢锛歴h
+			clientConfig.setRegion(new Region(this.properties.getRegion()));
+			if (this.properties.getIsHttps()) {
+				clientConfig.setHttpProtocol(HttpProtocol.https);
+			} else {
+				clientConfig.setHttpProtocol(HttpProtocol.http);
+			}
+			client = new COSClient(credentials, clientConfig);
+			createBucket();
+		} catch (Exception e) {
+			throw new IllegalArgumentException("鑵捐浜戝瓨鍌ㄩ厤缃敊璇�! 璇锋鏌ョ郴缁熼厤缃�!");
+		}
+	}
+
+	@Override
+	public void createBucket() {
+		try {
+			String bucketName = properties.getBucketName();
+			if (client.doesBucketExist(bucketName)) {
+				return;
+			}
+			CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
+			createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
+			client.createBucket(createBucketRequest);
+		} catch (Exception e) {
+			throw new OssException("鍒涘缓Bucket澶辫触, 璇锋牳瀵硅吘璁簯閰嶇疆淇℃伅");
+		}
 	}
 
 	@Override
@@ -48,44 +79,58 @@
 	}
 
 	@Override
-	public String upload(byte[] data, String path) {
-		// 鑵捐浜戝繀闇�瑕佷互"/"寮�澶�
-		if (!path.startsWith("/")) {
-			path = "/" + path;
+	public UploadResult upload(byte[] data, String path, String contentType) {
+		return upload(new ByteArrayInputStream(data), path, contentType);
+	}
+
+	@Override
+	public UploadResult upload(InputStream inputStream, String path, String contentType) {
+		try {
+			ObjectMetadata metadata = new ObjectMetadata();
+			metadata.setContentType(contentType);
+			client.putObject(new PutObjectRequest(properties.getBucketName(), path, inputStream, metadata));
+		} catch (Exception e) {
+			throw new OssException("涓婁紶鏂囦欢澶辫触锛岃妫�鏌ヨ吘璁簯閰嶇疆淇℃伅");
 		}
-		// 涓婁紶鍒拌吘璁簯
-//        UploadFileRequest request = new UploadFileRequest(config.getQcloudBucketName(), path, data);
-//        String response = client.uploadFile(request);
-//        Map<String, Object> jsonObject = JsonUtils.parseMap(response);
-//        if (Convert.toInt(jsonObject.get("code")) != 0) {
-//            throw new OssException("鏂囦欢涓婁紶澶辫触锛�" + Convert.toStr(jsonObject.get("message")));
-//        }
-		return this.properties.getDomain() + path;
+		return new UploadResult().setUrl(getEndpointLink() + "/" + path).setFilename(path);
 	}
 
 	@Override
 	public void delete(String path) {
-//        path = path.replace(config.getDomain(),"");
-//        DelFileRequest request = new DelFileRequest(config.getBucketName(), path);
-//        String response = client.delFile(request);
-//		Map<String, Object> jsonObject = JsonUtils.parseMap(response);
-//        if (Convert.toInt(jsonObject.get("code")) != 0) {
-//            throw new OssException("鏂囦欢鍒犻櫎澶辫触锛�" + Convert.toStr(jsonObject.get("message")));
-//        }
+		path = path.replace(getEndpointLink() + "/", "");
+		try {
+			client.deleteObject(new DeleteObjectRequest(properties.getBucketName(), path));
+		} catch (Exception e) {
+			throw new OssException("涓婁紶鏂囦欢澶辫触锛岃妫�鑵捐浜戞煡閰嶇疆淇℃伅");
+		}
 	}
 
 	@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(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(properties.getPrefix(), suffix), contentType);
 	}
 
 	@Override
 	public void afterPropertiesSet() throws Exception {
 		OssFactory.register(getServiceType(),this);
 	}
+
+	@Override
+	public String getEndpointLink() {
+		String endpoint = properties.getEndpoint();
+		StringBuilder sb = new StringBuilder(endpoint);
+		if (StringUtils.containsAnyIgnoreCase(endpoint, "http://")) {
+			sb.insert(7, properties.getBucketName() + ".");
+		} else if (StringUtils.containsAnyIgnoreCase(endpoint, "https://")) {
+			sb.insert(8, properties.getBucketName() + ".");
+		} else {
+			throw new OssException("Endpoint閰嶇疆閿欒");
+		}
+		return sb.toString();
+	}
 }

--
Gitblit v1.9.3