Skip to content

Commit 5bfcde4

Browse files
authored
fix: download url hash prefix / (#602)
1 parent 8d3cc04 commit 5bfcde4

File tree

9 files changed

+31
-16
lines changed

9 files changed

+31
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Changelog
2+
## 7.15.1(2024-05-29)
3+
* 处理在构造 Download URL 时 Key 前缀为 / 的情况
4+
25
## 7.15.0(2023-12-25)
36
* 区域缓存支持磁盘缓存
47
* 提供快速生成公有云的区域实例的方法

src/main/java/com/qiniu/common/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public final class Constants {
1010
/**
1111
* 版本号
1212
*/
13-
public static final String VERSION = "7.15.0";
13+
public static final String VERSION = "7.15.1";
1414
/**
1515
* 块大小,不能改变
1616
*/

src/main/java/com/qiniu/storage/Api.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,13 @@ protected Request(String urlPrefix) {
403403
this.scheme = url.getProtocol();
404404
this.host = url.getHost();
405405
this.port = url.getPort();
406-
this.addPathSegment(url.getPath());
406+
407+
// segment 不包含左边的 /, 截掉左边第一个 /
408+
String segment = url.getPath();
409+
if (segment != null && segment.startsWith("/")) {
410+
segment = segment.substring(1);
411+
}
412+
this.addPathSegment(segment);
407413

408414
String query = url.getQuery();
409415
if (StringUtils.isNullOrEmpty(query)) {
@@ -507,7 +513,7 @@ public String getPath() throws QiniuException {
507513
*/
508514
protected void buildPath() throws QiniuException {
509515
path = StringUtils.join(pathSegments, "/");
510-
if (!path.startsWith("/")) {
516+
if (!path.isEmpty()) {
511517
path = "/" + path;
512518
}
513519
}

src/main/java/com/qiniu/storage/ApiUploadV1MakeBlock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ protected void buildPath() throws QiniuException {
136136
}
137137

138138
addPathSegment("mkblk");
139-
addPathSegment(blockSize + "");
139+
addPathSegment("" + blockSize);
140140
super.buildPath();
141141
}
142142

src/main/java/com/qiniu/storage/ApiUploadV1MakeFile.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ protected void buildPath() throws QiniuException {
172172
}
173173

174174
addPathSegment("mkfile");
175-
addPathSegment(fileSize + "");
175+
addPathSegment("" + fileSize);
176176

177177
if (!StringUtils.isNullOrEmpty(fileMimeType)) {
178178
addPathSegment("mimeType");
@@ -189,14 +189,14 @@ protected void buildPath() throws QiniuException {
189189
addPathSegment(UrlSafeBase64.encodeToString(key));
190190
}
191191

192-
if (params != null && params.size() > 0) {
192+
if (params != null && !params.isEmpty()) {
193193
for (String key : params.keySet()) {
194194
addPathSegment(key);
195195
addPathSegment(UrlSafeBase64.encodeToString("" + params.get(key)));
196196
}
197197
}
198198

199-
if (metaDataParam != null && metaDataParam.size() > 0) {
199+
if (metaDataParam != null && !metaDataParam.isEmpty()) {
200200
for (String key : metaDataParam.keySet()) {
201201
addPathSegment(key);
202202
addPathSegment(UrlSafeBase64.encodeToString("" + metaDataParam.get(key)));

src/main/java/com/qiniu/storage/ApiUploadV1PutChunk.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected void buildPath() throws QiniuException {
142142

143143
addPathSegment("bput");
144144
addPathSegment(blockLastContext);
145-
addPathSegment(chunkOffset + "");
145+
addPathSegment("" + chunkOffset);
146146
super.buildPath();
147147
}
148148

src/main/java/com/qiniu/storage/ApiUploadV2UploadPart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ protected void buildPath() throws QiniuException {
168168
addPathSegment(ApiUtils.resumeV2EncodeKey(key));
169169
addPathSegment("uploads");
170170
addPathSegment(uploadId);
171-
addPathSegment(partNumber + "");
171+
addPathSegment("" + partNumber);
172172
super.buildPath();
173173
}
174174

src/main/java/com/qiniu/storage/DownloadUrl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ public DownloadUrl setFop(String fop) {
9090
* 配置 style【可选】
9191
* 如果觉得 fop 这样的形式够冗长,还可以为这些串行的 fop 集合定义一个友好别名。如此一来,就可以用友好URL风格进行访问,这个别名就是 style 。
9292
* eg:
93-
* 对 userBucket 的 fop(imageView2/2/w/320/h/480) 使用 style 的方式, 分隔符为 "-"
94-
* 使用 qrsctl 命令定义 style: (qrsctl separator [bucket] [styleSeparator])
95-
* qrsctl separator userBucket -
96-
* 定义数据处理的别名为 aliasName: (qrsctl style [bucket] [aliasName] [fop])
97-
* qrsctl style userBucket iphone imageView2/2/w/320/h/480
93+
* 对 userBucket 的 fop(imageView2/2/w/320/h/480) 使用 style 的方式, 分隔符为 "-"
94+
* 使用 qrsctl 命令定义 style: (qrsctl separator [bucket] [styleSeparator])
95+
* qrsctl separator userBucket -
96+
* 定义数据处理的别名为 aliasName: (qrsctl style [bucket] [aliasName] [fop])
97+
* qrsctl style userBucket iphone imageView2/2/w/320/h/480
9898
* <p>
9999
* <a href="https://developer.qiniu.com/dora/6217/directions-for-use-pfop"> 相关链接 </a>
100100
*

src/test/java/test/com/qiniu/storage/DownloadUrlTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
import org.junit.jupiter.api.Tag;
1111
import org.junit.jupiter.api.Test;
1212
import test.com.qiniu.TestConfig;
13+
1314
import static org.junit.jupiter.api.Assertions.assertEquals;
1415
import static org.junit.jupiter.api.Assertions.assertFalse;
1516
import static org.junit.jupiter.api.Assertions.assertNotNull;
1617
import static org.junit.jupiter.api.Assertions.assertTrue;
1718
import static org.junit.jupiter.api.Assertions.fail;
19+
1820
import java.net.URLEncoder;
1921
import java.util.Date;
2022
import java.util.HashMap;
@@ -59,6 +61,7 @@ public void testSpecialKey() {
5961
{
6062
put("", "");
6163
put("abc_def.mp4", "abc_def.mp4");
64+
put("/ab/cd", "/ab/cd");
6265
put("ab/cd", "ab/cd");
6366
put("ab/中文/de", "ab/%E4%B8%AD%E6%96%87/de");
6467
put("ab+-*de f", "ab%2B-%2Ade%20f");
@@ -77,8 +80,11 @@ public void testSpecialKey() {
7780
String encodeKey = keys.get(key);
7881
try {
7982
String url = new DownloadUrl(domain, false, key).buildURL();
80-
String exceptUrl = "http://" + domain + "/" + encodeKey;
81-
assertEquals(exceptUrl, url, "url:" + url + " exceptUrl:" + exceptUrl);
83+
String exceptUrl = "http://" + domain;
84+
if (!key.isEmpty()) {
85+
exceptUrl += "/" + encodeKey;
86+
}
87+
assertEquals(exceptUrl, url, "key:" + key);
8288
} catch (QiniuException e) {
8389
fail(e.error());
8490
}

0 commit comments

Comments
 (0)