Procházet zdrojové kódy

:zap: 支持获取七牛云文件公开下载链接和私有下载链接

支持获取七牛云文件公开下载链接和私有下载链接
Chill před 4 roky
rodič
revize
3200b9b14c

+ 31 - 1
blade-core-oss/src/main/java/org/springblade/core/oss/QiniuTemplate.java

@@ -33,6 +33,8 @@ import org.springblade.core.tool.utils.StringPool;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.InputStream;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.util.Date;
 import java.util.List;
 
@@ -127,6 +129,33 @@ public class QiniuTemplate {
 	}
 
 
+	/**
+	 * 获取文件公开链接
+	 *
+	 * @param fileName 文件名
+	 * @return 文件公开链接
+	 * @link https://developer.qiniu.com/kodo/1239/java#public-get
+	 */
+	public String publicFileLink(String fileName) {
+		return String.format("%s/%s", ossProperties.getEndpoint(), fileName);
+	}
+
+	/**
+	 * 获取文件私有链接
+	 *
+	 * @param fileName   文件名
+	 * @param expireTime 超时时间
+	 * @return 私有文件链接
+	 * @link https://developer.qiniu.com/kodo/1239/java#private-get
+	 */
+	@SneakyThrows
+	public String privateFileLink(String fileName, Long expireTime) {
+		String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()).replace("+", "%20");
+		String publicUrl = String.format("%s/%s", ossProperties.getEndpoint(), encodedFileName);
+		return auth.privateDownloadUrl(publicUrl, expireTime);
+	}
+
+
 	@SneakyThrows
 	public BladeFile putFile(MultipartFile file) {
 		return putFile(ossProperties.getBucketName(), file.getOriginalFilename(), file);
@@ -158,6 +187,8 @@ public class QiniuTemplate {
 
 	@SneakyThrows
 	public BladeFile put(String bucketName, InputStream stream, String key, boolean cover) {
+		BladeFile file = new BladeFile();
+		file.setOriginalName(key);
 		makeBucket(bucketName);
 		key = getFileName(key);
 		// 覆盖上传
@@ -172,7 +203,6 @@ public class QiniuTemplate {
 				retry++;
 			}
 		}
-		BladeFile file = new BladeFile();
 		file.setName(key);
 		file.setLink(fileLink(bucketName, key));
 		return file;