Skip to content

Commit 90c317d

Browse files
authored
Merge pull request #228 from qiniu/fix/customVars
fix customVars on resumeable upload
2 parents c9d1426 + 7c47820 commit 90c317d

File tree

5 files changed

+158
-55
lines changed

5 files changed

+158
-55
lines changed

.github/workflows/test-ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ jobs:
2323

2424
- name: Build
2525
run: dotnet build src/Qiniu/Qiniu.csproj
26-
26+
2727
- name: Test
28-
run: dotnet test src/QiniuTests/QiniuTests.csproj
29-
28+
run: dotnet test -v n src/QiniuTests/QiniuTests.csproj
29+
3030
env:
3131
QINIU_ACCESS_KEY: ${{ secrets.QINIU_ACCESS_KEY }}
3232
QINIU_SECRET_KEY: ${{ secrets.QINIU_SECRET_KEY }}
3333
QINIU_TEST_BUCKET: ${{ secrets.QINIU_TEST_BUCKET }}
34-
QINIU_TEST_DOMAIN: ${{ secrets.QINIU_TEST_DOMAIN }}
34+
QINIU_TEST_DOMAIN: ${{ secrets.QINIU_TEST_DOMAIN }}

src/Qiniu/Storage/PutExtra.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class PutExtra
2727
/// 设置文件上传的状态控制器
2828
/// </summary>
2929
public UploadController UploadController { set; get; }
30-
30+
3131
/// <summary>
3232
/// 最大重试次数
3333
/// </summary>

src/Qiniu/Storage/ResumableUploader.cs

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace Qiniu.Storage
1111
{
1212
/// <summary>
13-
/// 分片上传/断点续上传,适合于以下"情形2~3":
13+
/// 分片上传/断点续上传,适合于以下"情形2~3":
1414
/// (1)网络较好并且待上传的文件体积较小时(比如100MB或更小一点)使用简单上传;
1515
/// (2)文件较大或者网络状况不理想时请使用分片上传;
1616
/// (3)文件较大并且需要支持断点续上传,请使用分片上传(断点续上传)
@@ -84,7 +84,7 @@ public HttpResult UploadStream(Stream stream, string key, string upToken, PutExt
8484
{
8585
encodedObjectName = Base64.GetEncodedObjectName(key);
8686
}
87-
87+
8888
//check put extra
8989
if (putExtra == null)
9090
{
@@ -142,15 +142,15 @@ public HttpResult UploadStream(Stream stream, string key, string upToken, PutExt
142142
ExpiredAt = 0,
143143
};
144144
}
145-
else
145+
else if (putExtra.Version == "v2")
146146
{
147147
HttpResult res = initReq(encodedObjectName, upToken);
148148
Dictionary<string, string> responseBody = JsonConvert.DeserializeObject<Dictionary<string, string>>(res.Text);
149149
if (res.Code != 200)
150150
{
151151
return res;
152152
}
153-
153+
154154
resumeInfo = new ResumeInfo()
155155
{
156156
FileSize = fileSize,
@@ -160,14 +160,15 @@ public HttpResult UploadStream(Stream stream, string key, string upToken, PutExt
160160
ExpiredAt = long.Parse(responseBody["expireAt"]),
161161
UploadId = responseBody["uploadId"]
162162
};
163+
} else {
164+
throw new Exception("Invalid Version, only supports v1 / v2");
163165
}
164-
165166
}
166167

167168
//calc upload progress
168169
for (long blockIndex = 0; blockIndex < blockCount; blockIndex++)
169170
{
170-
171+
171172
if (putExtra.Version == "v1")
172173
{
173174
string context = resumeInfo.Contexts[blockIndex];
@@ -176,7 +177,7 @@ public HttpResult UploadStream(Stream stream, string key, string upToken, PutExt
176177
uploadedBytes += putExtra.PartSize;
177178
}
178179
}
179-
else
180+
else if (putExtra.Version == "v2")
180181
{
181182
Dictionary<string, object> etag = resumeInfo.Etags[blockIndex];
182183
if (etag != null)
@@ -188,8 +189,10 @@ public HttpResult UploadStream(Stream stream, string key, string upToken, PutExt
188189
uploadedBytes += putExtra.PartSize;
189190
resumeInfo.Uploaded = uploadedBytes;
190191
}
192+
} else {
193+
throw new Exception("Invalid Version, only supports v1 / v2");
191194
}
192-
195+
193196
}
194197

195198
//set upload progress
@@ -211,15 +214,17 @@ public HttpResult UploadStream(Stream stream, string key, string upToken, PutExt
211214
{
212215
context = resumeInfo.Contexts[blockIndex];
213216
}
214-
else
217+
else if (putExtra.Version == "v2")
215218
{
216219
Dictionary<string, object> etag = resumeInfo.Etags[blockIndex];
217220
if (etag != null && etag.Count > 0)
218221
{
219222
context = "~";
220223
}
224+
} else {
225+
throw new Exception("Invalid Version, only supports v1 / v2");
221226
}
222-
227+
223228
if (string.IsNullOrEmpty(context))
224229
{
225230
//check upload controller action before each chunk
@@ -258,7 +263,7 @@ public HttpResult UploadStream(Stream stream, string key, string upToken, PutExt
258263

259264
if (blockDataDict.Count == putExtra.BlockUploadThreads)
260265
{
261-
266+
262267
processMakeBlocks(blockDataDict, upToken, putExtra, resumeInfo, blockMakeResults, uploadedBytesDict, fileSize,
263268
encodedObjectName);
264269
//check mkblk results
@@ -312,11 +317,13 @@ public HttpResult UploadStream(Stream stream, string key, string upToken, PutExt
312317
{
313318
hr = MakeFile(key, fileSize, key, upToken, putExtra, resumeInfo.Contexts);
314319
}
315-
else
320+
else if (putExtra.Version == "v2")
316321
{
317322
hr = completeParts(key, resumeInfo, key, upToken, putExtra, encodedObjectName);
323+
} else {
324+
throw new Exception("Invalid Version, only supports v1 / v2");
318325
}
319-
326+
320327
if (hr.Code != (int)HttpCode.OK)
321328
{
322329
result.Shadow(hr);
@@ -462,12 +469,14 @@ private void MakeBlock(object resumeBlockerObj)
462469
{
463470
url = string.Format("{0}/mkblk/{1}", uploadHost, blockSize);
464471
}
465-
else
472+
else if (putExtra.Version == "v2")
466473
{
467474
url = string.Format("{0}/buckets/{1}/objects/{2}/uploads/{3}/{4}", uploadHost, bucket, resumeBlocker.encodedObjectName,
468475
resumeInfo.UploadId, blockIndex+1);
476+
} else {
477+
throw new Exception("Invalid Version, only supports v1 / v2");
469478
}
470-
479+
471480
string upTokenStr = string.Format("UpToken {0}", upToken);
472481
using (MemoryStream ms = new MemoryStream(blockBuffer, 0, blockSize))
473482
{
@@ -476,17 +485,19 @@ private void MakeBlock(object resumeBlockerObj)
476485
{
477486
result = httpManager.PostData(url, data, upTokenStr);
478487
}
479-
else
488+
else if (putExtra.Version == "v2")
480489
{
481490
Dictionary<string, string> headers = new Dictionary<string, string>();
482491
headers.Add("Authorization", upTokenStr);
483492
// data to md5
484493
string md5 = LabMD5.GenerateMD5(blockBuffer);
485494
headers.Add("Content-MD5", md5);
486495
result = httpManager.PutDataWithHeaders(url, data, headers);
496+
} else {
497+
throw new Exception("Invalid Version, only supports v1 / v2");
487498
}
488-
489-
499+
500+
490501
if (result.Code == (int)HttpCode.OK)
491502
{
492503
if (putExtra.Version == "v1")
@@ -514,14 +525,16 @@ private void MakeBlock(object resumeBlockerObj)
514525
putExtra.ProgressHandler(uploadedBytesDict["UploadProgress"], fileSize);
515526
}
516527
}
517-
else
528+
else if (putExtra.Version == "v2")
518529
{
519530
result.RefText += string.Format("[{0}] JSON Decode Error: text = {1}",
520531
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"), result.Text);
521532
result.RefCode = (int)HttpCode.USER_NEED_RETRY;
533+
} else {
534+
throw new Exception("Invalid Version, only supports v1 / v2");
522535
}
523536
}
524-
else
537+
else if (putExtra.Version == "v2")
525538
{
526539
Dictionary<string, string> rc = JsonConvert.DeserializeObject<Dictionary<string, string>>(result.Text);
527540
string md5 = LabMD5.GenerateMD5(blockBuffer);
@@ -543,6 +556,8 @@ private void MakeBlock(object resumeBlockerObj)
543556
}
544557
putExtra.ProgressHandler(uploadedBytesDict["UploadProgress"], fileSize);
545558
}
559+
} else {
560+
throw new Exception("Invalid Version, only supports v1 / v2");
546561
}
547562

548563
}
@@ -632,7 +647,7 @@ private HttpResult MakeFile(string fileName, long size, string key, string upTok
632647
string v = kvp.Value;
633648
if (k.StartsWith("x:") && !string.IsNullOrEmpty(v))
634649
{
635-
sb.AppendFormat("/{0}/{1}", k, v);
650+
sb.AppendFormat("/{0}/{1}", k, Base64.UrlSafeBase64Encode(v));
636651
}
637652
}
638653

@@ -694,7 +709,7 @@ private HttpResult MakeFile(string fileName, long size, string key, string upTok
694709
private HttpResult initReq(string encodedObjectName, string upToken)
695710
{
696711
HttpResult result = new HttpResult();
697-
712+
698713
try
699714
{
700715
string ak = UpToken.GetAccessKeyFromUpToken(upToken);
@@ -751,25 +766,20 @@ private HttpResult initReq(string encodedObjectName, string upToken)
751766
private HttpResult completeParts(string fileName, ResumeInfo resumeInfo, string key, string upToken, PutExtra putExtra, string encodedObjectName)
752767
{
753768
HttpResult result = new HttpResult();
754-
769+
755770
try
756771
{
757-
string paramStr = "{}";
758772
if (string.IsNullOrEmpty(fileName)) {
759773
fileName = "fname";
760774
}
761-
if (string.IsNullOrEmpty(putExtra.MimeType))
775+
if (string.IsNullOrEmpty(putExtra.MimeType))
762776
{
763777
putExtra.MimeType = "";
764778
}
765779
if (string.IsNullOrEmpty(key))
766780
{
767781
key = "";
768782
}
769-
if (putExtra.Params != null)
770-
{
771-
paramStr = JsonConvert.SerializeObject(putExtra.Params);
772-
}
773783
//get upload host
774784
string ak = UpToken.GetAccessKeyFromUpToken(upToken);
775785
string bucket = UpToken.GetBucketFromUpToken(upToken);
@@ -784,7 +794,7 @@ private HttpResult completeParts(string fileName, ResumeInfo resumeInfo, string
784794
Dictionary<string, object> body = new Dictionary<string, object>();
785795
body.Add("fname", fileName);
786796
body.Add("mimeType", putExtra.MimeType);
787-
body.Add("customVars", null);
797+
body.Add("customVars", putExtra.Params);
788798
body.Add("parts", resumeInfo.Etags);
789799
string url = string.Format("{0}/buckets/{1}/objects/{2}/uploads/{3}", uploadHost, bucket, encodedObjectName, resumeInfo.UploadId);
790800
string bodyStr = JsonConvert.SerializeObject(body);

src/QiniuTests/Storage/FormUploaderTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public void UploadFileTest()
1717
string key = string.Format("UploadFileTest_{0}.dat", rand.Next());
1818

1919
string tempPath = System.IO.Path.GetTempPath();
20-
int rnd = new Random().Next(1, 100000);
20+
int rnd = new Random().Next(1, 100000);
2121
string filePath = tempPath + "resumeFile" + rnd.ToString();
22-
char[] testBody = new char[4 * 1024 * 1024];
22+
char[] testBody = new char[4 * 1024 * 1024];
2323
System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Create);
2424
System.IO.StreamWriter sw = new System.IO.StreamWriter(stream, System.Text.Encoding.Default);
2525
sw.Write(testBody);
@@ -51,9 +51,9 @@ public void UploadFileV2Test()
5151
string key = string.Format("UploadFileTest_{0}.dat", rand.Next());
5252

5353
string tempPath = System.IO.Path.GetTempPath();
54-
int rnd = new Random().Next(1, 100000);
54+
int rnd = new Random().Next(1, 100000);
5555
string filePath = tempPath + "resumeFile" + rnd.ToString();
56-
char[] testBody = new char[4 * 1024 * 1024];
56+
char[] testBody = new char[4 * 1024 * 1024];
5757
System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Create);
5858
System.IO.StreamWriter sw = new System.IO.StreamWriter(stream, System.Text.Encoding.Default);
5959
sw.Write(testBody);
@@ -81,4 +81,4 @@ public void UploadFileV2Test()
8181
}
8282

8383
}
84-
}
84+
}

0 commit comments

Comments
 (0)