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

支持获取七牛云文件公开下载链接和私有下载链接
This commit is contained in:
Chill 2021-02-22 15:57:17 +08:00 committed by GitHub
commit 3200b9b14c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,6 +33,8 @@ import org.springblade.core.tool.utils.StringPool;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream; import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date; import java.util.Date;
import java.util.List; 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 @SneakyThrows
public BladeFile putFile(MultipartFile file) { public BladeFile putFile(MultipartFile file) {
return putFile(ossProperties.getBucketName(), file.getOriginalFilename(), file); return putFile(ossProperties.getBucketName(), file.getOriginalFilename(), file);
@ -158,6 +187,8 @@ public class QiniuTemplate {
@SneakyThrows @SneakyThrows
public BladeFile put(String bucketName, InputStream stream, String key, boolean cover) { public BladeFile put(String bucketName, InputStream stream, String key, boolean cover) {
BladeFile file = new BladeFile();
file.setOriginalName(key);
makeBucket(bucketName); makeBucket(bucketName);
key = getFileName(key); key = getFileName(key);
// 覆盖上传 // 覆盖上传
@ -172,7 +203,6 @@ public class QiniuTemplate {
retry++; retry++;
} }
} }
BladeFile file = new BladeFile();
file.setName(key); file.setName(key);
file.setLink(fileLink(bucketName, key)); file.setLink(fileLink(bucketName, key));
return file; return file;