Skip to content

fix: download url hash prefix / #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/main/java/com/qiniu/storage/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,7 @@ public String getPath() throws QiniuException {
* @throws QiniuException 组装 query 时的异常,一般为缺失必要参数的异常
*/
protected void buildPath() throws QiniuException {
path = StringUtils.join(pathSegments, "/");
if (!path.startsWith("/")) {
path = "/" + path;
}
path = StringUtils.join(pathSegments, "");
}


Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/qiniu/storage/ApiQueryRegion.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ protected void buildQuery() throws QiniuException {

@Override
protected void buildPath() throws QiniuException {
addPathSegment("v4");
addPathSegment("query");
addPathSegment("/v4");
addPathSegment("/query");
super.buildPath();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/qiniu/storage/ApiUploadV1MakeBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ protected void buildPath() throws QiniuException {
ApiUtils.throwInvalidRequestParamException("block size");
}

addPathSegment("mkblk");
addPathSegment(blockSize + "");
addPathSegment("/mkblk");
addPathSegment("/" + blockSize);
super.buildPath();
}

Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/qiniu/storage/ApiUploadV1MakeFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,35 +171,35 @@ protected void buildPath() throws QiniuException {
ApiUtils.throwInvalidRequestParamException("file size");
}

addPathSegment("mkfile");
addPathSegment(fileSize + "");
addPathSegment("/mkfile");
addPathSegment("/" + fileSize);

if (!StringUtils.isNullOrEmpty(fileMimeType)) {
addPathSegment("mimeType");
addPathSegment(UrlSafeBase64.encodeToString(fileMimeType));
addPathSegment("/mimeType");
addPathSegment("/" + UrlSafeBase64.encodeToString(fileMimeType));
}

if (!StringUtils.isNullOrEmpty(fileName)) {
addPathSegment("fname");
addPathSegment(UrlSafeBase64.encodeToString(fileName));
addPathSegment("/fname");
addPathSegment("/" + UrlSafeBase64.encodeToString(fileName));
}

if (key != null) {
addPathSegment("key");
addPathSegment(UrlSafeBase64.encodeToString(key));
addPathSegment("/key");
addPathSegment("/" + UrlSafeBase64.encodeToString(key));
}

if (params != null && params.size() > 0) {
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
addPathSegment(key);
addPathSegment(UrlSafeBase64.encodeToString("" + params.get(key)));
addPathSegment("/" + key);
addPathSegment("/" + UrlSafeBase64.encodeToString("" + params.get(key)));
}
}

if (metaDataParam != null && metaDataParam.size() > 0) {
if (metaDataParam != null && !metaDataParam.isEmpty()) {
for (String key : metaDataParam.keySet()) {
addPathSegment(key);
addPathSegment(UrlSafeBase64.encodeToString("" + metaDataParam.get(key)));
addPathSegment("/" + key);
addPathSegment("/" + UrlSafeBase64.encodeToString("" + metaDataParam.get(key)));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/qiniu/storage/ApiUploadV1PutChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ protected void buildPath() throws QiniuException {
ApiUtils.throwInvalidRequestParamException("block last context");
}

addPathSegment("bput");
addPathSegment(blockLastContext);
addPathSegment(chunkOffset + "");
addPathSegment("/bput");
addPathSegment("/" + blockLastContext);
addPathSegment("/" + chunkOffset);
super.buildPath();
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/qiniu/storage/ApiUploadV2AbortUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ protected void buildPath() throws QiniuException {
}

String bucket = token.getBucket();
addPathSegment("buckets");
addPathSegment(bucket);
addPathSegment("objects");
addPathSegment(ApiUtils.resumeV2EncodeKey(key));
addPathSegment("uploads");
addPathSegment(uploadId);
addPathSegment("/buckets");
addPathSegment("/" + bucket);
addPathSegment("/objects");
addPathSegment("/" + ApiUtils.resumeV2EncodeKey(key));
addPathSegment("/uploads");
addPathSegment("/" + uploadId);
super.buildPath();
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/qiniu/storage/ApiUploadV2CompleteUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ protected void buildPath() throws QiniuException {
}

String bucket = token.getBucket();
addPathSegment("buckets");
addPathSegment(bucket);
addPathSegment("objects");
addPathSegment(ApiUtils.resumeV2EncodeKey(key));
addPathSegment("uploads");
addPathSegment(uploadId);
addPathSegment("/buckets");
addPathSegment("/" + bucket);
addPathSegment("/objects");
addPathSegment("/" + ApiUtils.resumeV2EncodeKey(key));
addPathSegment("/uploads");
addPathSegment("/" + uploadId);
super.buildPath();
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/qiniu/storage/ApiUploadV2InitUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ protected void buildPath() throws QiniuException {
}

String bucket = getUploadToken().getBucket();
addPathSegment("buckets");
addPathSegment(bucket);
addPathSegment("objects");
addPathSegment(ApiUtils.resumeV2EncodeKey(key));
addPathSegment("uploads");
addPathSegment("/buckets");
addPathSegment("/" + bucket);
addPathSegment("/objects");
addPathSegment("/" + ApiUtils.resumeV2EncodeKey(key));
addPathSegment("/uploads");
super.buildPath();
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/qiniu/storage/ApiUploadV2ListParts.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ protected void buildPath() throws QiniuException {
}

String bucket = token.getBucket();
addPathSegment("buckets");
addPathSegment(bucket);
addPathSegment("objects");
addPathSegment(ApiUtils.resumeV2EncodeKey(key));
addPathSegment("uploads");
addPathSegment(uploadId);
addPathSegment("/buckets");
addPathSegment("/" + bucket);
addPathSegment("/objects");
addPathSegment("/" + ApiUtils.resumeV2EncodeKey(key));
addPathSegment("/uploads");
addPathSegment("/" + uploadId);
super.buildPath();
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/qiniu/storage/ApiUploadV2UploadPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ protected void buildPath() throws QiniuException {
}

String bucket = getUploadToken().getBucket();
addPathSegment("buckets");
addPathSegment(bucket);
addPathSegment("objects");
addPathSegment(ApiUtils.resumeV2EncodeKey(key));
addPathSegment("uploads");
addPathSegment(uploadId);
addPathSegment(partNumber + "");
addPathSegment("/buckets");
addPathSegment("/" + bucket);
addPathSegment("/objects");
addPathSegment("/" + ApiUtils.resumeV2EncodeKey(key));
addPathSegment("/uploads");
addPathSegment("/" + uploadId);
addPathSegment("/" + partNumber);
super.buildPath();
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/qiniu/storage/DownloadPrivateCloudUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ protected void willBuildUrl() throws QiniuException {

@Override
protected void willSetKeyForUrl(Api.Request request) throws QiniuException {
request.addPathSegment("getfile");
request.addPathSegment(accessKey);
request.addPathSegment(bucketName);
request.addPathSegment("/getfile");
request.addPathSegment("/" + accessKey);
request.addPathSegment("/" + bucketName);
super.willSetKeyForUrl(request);
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/qiniu/storage/DownloadUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ public DownloadUrl setFop(String fop) {
* 配置 style【可选】
* 如果觉得 fop 这样的形式够冗长,还可以为这些串行的 fop 集合定义一个友好别名。如此一来,就可以用友好URL风格进行访问,这个别名就是 style 。
* eg:
* 对 userBucket 的 fop(imageView2/2/w/320/h/480) 使用 style 的方式, 分隔符为 "-"
* 使用 qrsctl 命令定义 style: (qrsctl separator [bucket] [styleSeparator])
* qrsctl separator userBucket -
* 定义数据处理的别名为 aliasName: (qrsctl style [bucket] [aliasName] [fop])
* qrsctl style userBucket iphone imageView2/2/w/320/h/480
* 对 userBucket 的 fop(imageView2/2/w/320/h/480) 使用 style 的方式, 分隔符为 "-"
* 使用 qrsctl 命令定义 style: (qrsctl separator [bucket] [styleSeparator])
* qrsctl separator userBucket -
* 定义数据处理的别名为 aliasName: (qrsctl style [bucket] [aliasName] [fop])
* qrsctl style userBucket iphone imageView2/2/w/320/h/480
* <p>
* <a href="https://developer.qiniu.com/dora/6217/directions-for-use-pfop"> 相关链接 </a>
*
Expand Down Expand Up @@ -159,7 +159,7 @@ public String buildURL() throws QiniuException {
}
}
if (!StringUtils.isNullOrEmpty(keyAndStyle)) {
request.addPathSegment(keyAndStyle);
request.addPathSegment("/" + keyAndStyle);
}
didSetKeyForUrl(request);

Expand Down
10 changes: 8 additions & 2 deletions src/test/java/test/com/qiniu/storage/DownloadUrlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import test.com.qiniu.TestConfig;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -59,6 +61,7 @@ public void testSpecialKey() {
{
put("", "");
put("abc_def.mp4", "abc_def.mp4");
put("/ab/cd", "/ab/cd");
put("ab/cd", "ab/cd");
put("ab/中文/de", "ab/%E4%B8%AD%E6%96%87/de");
put("ab+-*de f", "ab%2B-%2Ade%20f");
Expand All @@ -77,8 +80,11 @@ public void testSpecialKey() {
String encodeKey = keys.get(key);
try {
String url = new DownloadUrl(domain, false, key).buildURL();
String exceptUrl = "http://" + domain + "/" + encodeKey;
assertEquals(exceptUrl, url, "url:" + url + " exceptUrl:" + exceptUrl);
String exceptUrl = "http://" + domain;
if (!key.isEmpty()) {
exceptUrl += "/" + encodeKey;
}
assertEquals(exceptUrl, url, "key:" + key);
} catch (QiniuException e) {
fail(e.error());
}
Expand Down
Loading