Skip to content

Commit

Permalink
* 生成下载链接的API加入配置Content-Disposition的filename
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Yin committed Jan 7, 2020
1 parent 50542ad commit c136f3a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public static void main(String[] args) {
try {
String url = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.getDownloadUrlFromPrivateBucket(keyName, bucketName, expiresDuration)
/**
* 使用Content-Disposition: attachment,并且默认文件名为KeyName
*/
// .withAttachment()
/**
* 使用Content-Disposition: attachment,并且配置文件名
*/
// .withAttachment("filename")
.createUrl();
getStream(url, localDir, saveName);
} catch (UfileParamException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public class GenerateObjectPrivateUrlApi {
*/
private long expiresDuration;

/**
* Ufile 文件下载链接所需的Content-Disposition: attachment文件名
*/
private String attachmentFileName;

/**
* 用户可选签名参数
*/
Expand All @@ -65,6 +70,25 @@ protected GenerateObjectPrivateUrlApi(ObjectAuthorizer authorizer, String host,
this.expiresDuration = expiresDuration;
}

/**
* 使用Content-Disposition: attachment,并配置attachment的文件名
* @param attachmentFileName Content-Disposition: attachment的文件名
* @return
*/
public GenerateObjectPrivateUrlApi withAttachment(String attachmentFileName) {
this.attachmentFileName = attachmentFileName;
return this;
}

/**
* 使用Content-Disposition: attachment,并且文件名默认为keyName
* @return
*/
public GenerateObjectPrivateUrlApi withAttachment() {
this.attachmentFileName = keyName;
return this;
}


/**
* 配置签名可选参数
Expand Down Expand Up @@ -96,10 +120,21 @@ public String createUrl() throws UfileClientException {
GetRequestBuilder builder = (GetRequestBuilder) new GetRequestBuilder()
.baseUrl(generateFinalHost(bucketName, keyName));

return builder.addParam(new Parameter("UCloudPublicKey", authorizer.getPublicKey()))
builder.addParam(new Parameter("UCloudPublicKey", authorizer.getPublicKey()))
.addParam(new Parameter("Signature", signature))
.addParam(new Parameter("Expires", String.valueOf(expiresTime)))
.generateGetUrl(builder.getBaseUrl(), builder.getParams());
.addParam(new Parameter("Expires", String.valueOf(expiresTime)));

if (attachmentFileName != null && !attachmentFileName.isEmpty()) {
try {
attachmentFileName = URLEncoder.encode(attachmentFileName, "UTF-8").replace("+", "%20");
builder.addParam(new Parameter("ufileattname", attachmentFileName));
} catch (UnsupportedEncodingException e) {
throw new UfileClientException("Occur error during URLEncode attachmentFileName", e);
}
}


return builder.generateGetUrl(builder.getBaseUrl(), builder.getParams());
}

public interface CreatePrivateUrlCallback {
Expand All @@ -122,20 +157,7 @@ public void createUrlAsync(final CreatePrivateUrlCallback callback) {
@Override
public void run() {
try {
parameterValidat();
long expiresTime = System.currentTimeMillis() / 1000 + expiresDuration;

String signature = authorizer.authorizePrivateUrl(
(ObjectDownloadAuthParam) new ObjectDownloadAuthParam(HttpMethod.GET, bucketName, keyName, expiresTime)
.setOptional(authOptionalData));

GetRequestBuilder builder = (GetRequestBuilder) new GetRequestBuilder()
.baseUrl(generateFinalHost(bucketName, keyName));

String url = builder.addParam(new Parameter("UCloudPublicKey", authorizer.getPublicKey()))
.addParam(new Parameter("Signature", signature))
.addParam(new Parameter("Expires", String.valueOf(expiresTime)))
.generateGetUrl(builder.getBaseUrl(), builder.getParams());
String url = createUrl();
if (callback != null)
callback.onSuccess(url);
} catch (UfileClientException e) {
Expand All @@ -154,8 +176,8 @@ private String generateFinalHost(String bucketName, String keyName) throws Ufile
return String.format("%s/%s", host, keyName);

try {
bucketName = URLEncoder.encode(bucketName, "UTF-8").replace("+","%20");
keyName = URLEncoder.encode(keyName, "UTF-8").replace("+","%20");
bucketName = URLEncoder.encode(bucketName, "UTF-8").replace("+", "%20");
keyName = URLEncoder.encode(keyName, "UTF-8").replace("+", "%20");
return String.format("http://%s.%s/%s", bucketName, host, keyName);
} catch (UnsupportedEncodingException e) {
throw new UfileClientException("Occur error during URLEncode bucketName and keyName");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileParamException;
import cn.ucloud.ufile.exception.UfileRequiredParamNotFoundException;
import cn.ucloud.ufile.http.request.GetRequestBuilder;
import cn.ucloud.ufile.util.Parameter;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
Expand All @@ -27,6 +29,11 @@ public class GenerateObjectPublicUrlApi {
*/
private String bucketName;

/**
* Ufile 文件下载链接所需的Content-Disposition: attachment文件名
*/
private String attachmentFileName;

/**
* 构造方法
*
Expand All @@ -40,6 +47,25 @@ protected GenerateObjectPublicUrlApi(String host, String keyName, String bucketN
this.bucketName = bucketName;
}

/**
* 使用Content-Disposition: attachment,并配置attachment的文件名
* @param attachmentFileName Content-Disposition: attachment的文件名
* @return
*/
public GenerateObjectPublicUrlApi withAttachment(String attachmentFileName) {
this.attachmentFileName = attachmentFileName;
return this;
}

/**
* 使用Content-Disposition: attachment,并且文件名默认为keyName
* @return
*/
public GenerateObjectPublicUrlApi withAttachment() {
this.attachmentFileName = keyName;
return this;
}

/**
* 生成下载URL
*
Expand All @@ -48,7 +74,19 @@ protected GenerateObjectPublicUrlApi(String host, String keyName, String bucketN
*/
public String createUrl() throws UfileClientException {
parameterValidat();
return generateFinalHost(bucketName, keyName);
GetRequestBuilder builder = (GetRequestBuilder) new GetRequestBuilder()
.baseUrl(generateFinalHost(bucketName, keyName));

if (attachmentFileName != null && !attachmentFileName.isEmpty()) {
try {
attachmentFileName = URLEncoder.encode(attachmentFileName, "UTF-8").replace("+", "%20");
builder.addParam(new Parameter("ufileattname", attachmentFileName));
} catch (UnsupportedEncodingException e) {
throw new UfileClientException("Occur error during URLEncode attachmentFileName", e);
}
}

return builder.generateGetUrl(builder.getBaseUrl(), builder.getParams());
}

private String generateFinalHost(String bucketName, String keyName) throws UfileClientException {
Expand Down

0 comments on commit c136f3a

Please sign in to comment.