Skip to content

Commit

Permalink
fix: download url hash prefix / (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSen-qn authored May 30, 2024
1 parent 8d3cc04 commit 5bfcde4
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
## 7.15.1(2024-05-29)
* 处理在构造 Download URL 时 Key 前缀为 / 的情况

## 7.15.0(2023-12-25)
* 区域缓存支持磁盘缓存
* 提供快速生成公有云的区域实例的方法
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public final class Constants {
/**
* 版本号
*/
public static final String VERSION = "7.15.0";
public static final String VERSION = "7.15.1";
/**
* 块大小,不能改变
*/
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/qiniu/storage/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,13 @@ protected Request(String urlPrefix) {
this.scheme = url.getProtocol();
this.host = url.getHost();
this.port = url.getPort();
this.addPathSegment(url.getPath());

// segment 不包含左边的 /, 截掉左边第一个 /
String segment = url.getPath();
if (segment != null && segment.startsWith("/")) {
segment = segment.substring(1);
}
this.addPathSegment(segment);

String query = url.getQuery();
if (StringUtils.isNullOrEmpty(query)) {
Expand Down Expand Up @@ -507,7 +513,7 @@ public String getPath() throws QiniuException {
*/
protected void buildPath() throws QiniuException {
path = StringUtils.join(pathSegments, "/");
if (!path.startsWith("/")) {
if (!path.isEmpty()) {
path = "/" + path;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/storage/ApiUploadV1MakeBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected void buildPath() throws QiniuException {
}

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

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

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

if (!StringUtils.isNullOrEmpty(fileMimeType)) {
addPathSegment("mimeType");
Expand All @@ -189,14 +189,14 @@ protected void buildPath() throws QiniuException {
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)));
}
}

if (metaDataParam != null && metaDataParam.size() > 0) {
if (metaDataParam != null && !metaDataParam.isEmpty()) {
for (String key : metaDataParam.keySet()) {
addPathSegment(key);
addPathSegment(UrlSafeBase64.encodeToString("" + metaDataParam.get(key)));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/storage/ApiUploadV1PutChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected void buildPath() throws QiniuException {

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

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/storage/ApiUploadV2UploadPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected void buildPath() throws QiniuException {
addPathSegment(ApiUtils.resumeV2EncodeKey(key));
addPathSegment("uploads");
addPathSegment(uploadId);
addPathSegment(partNumber + "");
addPathSegment("" + partNumber);
super.buildPath();
}

Expand Down
10 changes: 5 additions & 5 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
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

0 comments on commit 5bfcde4

Please sign in to comment.