|
@@ -0,0 +1,291 @@
|
|
|
+package org.springblade.resource.builder.oss;
|
|
|
+
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.common.utils.BinaryUtil;
|
|
|
+import com.aliyun.oss.model.MatchMode;
|
|
|
+import com.aliyun.oss.model.ObjectMetadata;
|
|
|
+import com.aliyun.oss.model.PolicyConditions;
|
|
|
+import com.aliyun.oss.model.PutObjectResult;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import org.springblade.core.oss.OssTemplate;
|
|
|
+import org.springblade.core.oss.model.BladeFile;
|
|
|
+import org.springblade.core.oss.model.OssFile;
|
|
|
+import org.springblade.core.oss.props.OssProperties;
|
|
|
+import org.springblade.core.oss.rule.OssRule;
|
|
|
+import org.springblade.core.tool.jackson.JsonUtil;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+public class AliossTemplateRe implements OssTemplate {
|
|
|
+ private final OSSClient ossClient;
|
|
|
+ private final OssProperties ossProperties;
|
|
|
+ private final OssRule ossRule;
|
|
|
+
|
|
|
+ public void makeBucket(String bucketName) {
|
|
|
+ try {
|
|
|
+ if (!this.bucketExists(bucketName)) {
|
|
|
+ this.ossClient.createBucket(this.getBucketName(bucketName));
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Throwable var3) {
|
|
|
+ throw var3;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void removeBucket(String bucketName) {
|
|
|
+ try {
|
|
|
+ this.ossClient.deleteBucket(this.getBucketName(bucketName));
|
|
|
+ } catch (Throwable var3) {
|
|
|
+ throw var3;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean bucketExists(String bucketName) {
|
|
|
+ try {
|
|
|
+ return this.ossClient.doesBucketExist(this.getBucketName(bucketName));
|
|
|
+ } catch (Throwable var3) {
|
|
|
+ throw var3;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void copyFile(String bucketName, String fileName, String destBucketName) {
|
|
|
+ try {
|
|
|
+ this.ossClient.copyObject(this.getBucketName(bucketName), fileName, this.getBucketName(destBucketName), fileName);
|
|
|
+ } catch (Throwable var5) {
|
|
|
+ throw var5;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void copyFile(String bucketName, String fileName, String destBucketName, String destFileName) {
|
|
|
+ try {
|
|
|
+ this.ossClient.copyObject(this.getBucketName(bucketName), fileName, this.getBucketName(destBucketName), destFileName);
|
|
|
+ } catch (Throwable var6) {
|
|
|
+ throw var6;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public OssFile statFile(String fileName) {
|
|
|
+ try {
|
|
|
+ return this.statFile(this.ossProperties.getBucketName(), fileName);
|
|
|
+ } catch (Throwable var3) {
|
|
|
+ throw var3;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public OssFile statFile(String bucketName, String fileName) {
|
|
|
+ try {
|
|
|
+ ObjectMetadata stat = this.ossClient.getObjectMetadata(this.getBucketName(bucketName), fileName);
|
|
|
+ OssFile ossFile = new OssFile();
|
|
|
+ ossFile.setName(fileName);
|
|
|
+ ossFile.setLink(this.fileLink(ossFile.getName()));
|
|
|
+ ossFile.setHash(stat.getContentMD5());
|
|
|
+ ossFile.setLength(stat.getContentLength());
|
|
|
+ ossFile.setPutTime(stat.getLastModified());
|
|
|
+ ossFile.setContentType(stat.getContentType());
|
|
|
+ return ossFile;
|
|
|
+ } catch (Throwable var5) {
|
|
|
+ throw var5;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String filePath(String fileName) {
|
|
|
+ try {
|
|
|
+ return this.getOssHost().concat("/").concat(fileName);
|
|
|
+ } catch (Throwable var3) {
|
|
|
+ throw var3;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String filePath(String bucketName, String fileName) {
|
|
|
+ try {
|
|
|
+ return this.getOssHost(bucketName).concat("/").concat(fileName);
|
|
|
+ } catch (Throwable var4) {
|
|
|
+ throw var4;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String fileLink(String fileName) {
|
|
|
+ try {
|
|
|
+ return this.getOssHost().concat("/").concat(fileName);
|
|
|
+ } catch (Throwable var3) {
|
|
|
+ throw var3;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String fileLink(String bucketName, String fileName) {
|
|
|
+ try {
|
|
|
+ return this.getOssHost(bucketName).concat("/").concat(fileName);
|
|
|
+ } catch (Throwable var4) {
|
|
|
+ throw var4;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public BladeFile putFile(MultipartFile file) {
|
|
|
+ try {
|
|
|
+ return this.putFile(this.ossProperties.getBucketName(), file.getOriginalFilename(), file);
|
|
|
+ } catch (Throwable var3) {
|
|
|
+ throw var3;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public BladeFile putFile(String fileName, MultipartFile file) {
|
|
|
+ try {
|
|
|
+ return this.putFile(this.ossProperties.getBucketName(), fileName, file);
|
|
|
+ } catch (Throwable var4) {
|
|
|
+ throw var4;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ public BladeFile putFile(String bucketName, String fileName, MultipartFile file) {
|
|
|
+ try {
|
|
|
+ return this.putFile(bucketName, fileName, file.getInputStream());
|
|
|
+ } catch (Throwable var5) {
|
|
|
+ throw var5;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public BladeFile putFile(String fileName, InputStream stream) {
|
|
|
+ try {
|
|
|
+ return this.putFile(this.ossProperties.getBucketName(), fileName, stream);
|
|
|
+ } catch (Throwable var4) {
|
|
|
+ throw var4;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public BladeFile putFile(String bucketName, String fileName, InputStream stream) {
|
|
|
+ try {
|
|
|
+ return this.put(bucketName, stream, fileName, false);
|
|
|
+ } catch (Throwable var5) {
|
|
|
+ throw var5;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public BladeFile put(String bucketName, InputStream stream, String key, boolean cover) {
|
|
|
+ try {
|
|
|
+ this.makeBucket(bucketName);
|
|
|
+ String originalName = key;
|
|
|
+ key = this.getFileName(key);
|
|
|
+ if (cover) {
|
|
|
+ this.ossClient.putObject(this.getBucketName(bucketName), key, stream);
|
|
|
+ } else {
|
|
|
+ PutObjectResult response = this.ossClient.putObject(this.getBucketName(bucketName), key, stream);
|
|
|
+ int retry = 0;
|
|
|
+
|
|
|
+ for(byte retryCount = 5; StringUtils.isEmpty(response.getETag()) && retry < retryCount; ++retry) {
|
|
|
+ response = this.ossClient.putObject(this.getBucketName(bucketName), key, stream);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ BladeFile file = new BladeFile();
|
|
|
+ file.setOriginalName(originalName);
|
|
|
+ file.setName(key);
|
|
|
+ file.setDomain(this.getOssHost(bucketName));
|
|
|
+ file.setLink(this.fileLink(bucketName, key));
|
|
|
+ return file;
|
|
|
+ } catch (Throwable var9) {
|
|
|
+ throw var9;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void removeFile(String fileName) {
|
|
|
+ try {
|
|
|
+ this.ossClient.deleteObject(this.getBucketName(), fileName);
|
|
|
+ } catch (Throwable var3) {
|
|
|
+ throw var3;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void removeFile(String bucketName, String fileName) {
|
|
|
+ try {
|
|
|
+ this.ossClient.deleteObject(this.getBucketName(bucketName), fileName);
|
|
|
+ } catch (Throwable var4) {
|
|
|
+ throw var4;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void removeFiles(List<String> fileNames) {
|
|
|
+ try {
|
|
|
+ fileNames.forEach(this::removeFile);
|
|
|
+ } catch (Throwable var3) {
|
|
|
+ throw var3;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void removeFiles(String bucketName, List<String> fileNames) {
|
|
|
+ try {
|
|
|
+ fileNames.forEach((fileName) -> {
|
|
|
+ this.removeFile(this.getBucketName(bucketName), fileName);
|
|
|
+ });
|
|
|
+ } catch (Throwable var4) {
|
|
|
+ throw var4;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getBucketName() {
|
|
|
+ return this.getBucketName(this.ossProperties.getBucketName());
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getBucketName(String bucketName) {
|
|
|
+ return this.ossRule.bucketName(bucketName);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getFileName(String originalFilename) {
|
|
|
+ return this.ossRule.fileName(originalFilename);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getUploadToken() {
|
|
|
+ return this.getUploadToken(this.ossProperties.getBucketName());
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getUploadToken(String bucketName) {
|
|
|
+ return this.getUploadToken(bucketName, (Long)this.ossProperties.getArgs().get("expireTime", 3600L));
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getUploadToken(String bucketName, long expireTime) {
|
|
|
+ String baseDir = "upload";
|
|
|
+ long expireEndTime = System.currentTimeMillis() + expireTime * 1000L;
|
|
|
+ Date expiration = new Date(expireEndTime);
|
|
|
+ PolicyConditions policyConds = new PolicyConditions();
|
|
|
+ policyConds.addConditionItem("content-length-range", 0L, (long)(Integer)this.ossProperties.getArgs().get("contentLengthRange", 10485760));
|
|
|
+ policyConds.addConditionItem(MatchMode.StartWith, "key", baseDir);
|
|
|
+ String postPolicy = this.ossClient.generatePostPolicy(expiration, policyConds);
|
|
|
+ byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8);
|
|
|
+ String encodedPolicy = BinaryUtil.toBase64String(binaryData);
|
|
|
+ String postSignature = this.ossClient.calculatePostSignature(postPolicy);
|
|
|
+ Map<String, String> respMap = new LinkedHashMap(16);
|
|
|
+ respMap.put("accessid", this.ossProperties.getAccessKey());
|
|
|
+ respMap.put("policy", encodedPolicy);
|
|
|
+ respMap.put("signature", postSignature);
|
|
|
+ respMap.put("dir", baseDir);
|
|
|
+ respMap.put("host", this.getOssHost(bucketName));
|
|
|
+ respMap.put("expire", String.valueOf(expireEndTime / 1000L));
|
|
|
+ return JsonUtil.toJson(respMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getOssHost(String bucketName) {
|
|
|
+ String prefix = this.ossProperties.getEndpoint().contains("https://") ? "https://" : "http://";
|
|
|
+ if(this.ossProperties.getEndpoint().indexOf("-internal")>=0){
|
|
|
+ return prefix + this.getBucketName(bucketName) + "." + this.ossProperties.getEndpoint().replaceAll("-internal","").replaceFirst(prefix, "");
|
|
|
+ }else{
|
|
|
+ return prefix + this.getBucketName(bucketName) + "." + this.ossProperties.getEndpoint().replaceFirst(prefix, "");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getOssHost() {
|
|
|
+ return this.getOssHost(this.ossProperties.getBucketName());
|
|
|
+ }
|
|
|
+
|
|
|
+ public AliossTemplateRe(final OSSClient ossClient, final OssProperties ossProperties, final OssRule ossRule) {
|
|
|
+ this.ossClient = ossClient;
|
|
|
+ this.ossProperties = ossProperties;
|
|
|
+ this.ossRule = ossRule;
|
|
|
+ }
|
|
|
+}
|