Skip to content

Commit 8fabdc0

Browse files
authored
Merge pull request #362 from qiniu/fix/resumable-upload-v2
修复分片上传 v2 的 bug
2 parents 4e1fae5 + fe63159 commit 8fabdc0

File tree

9 files changed

+175
-36
lines changed

9 files changed

+175
-36
lines changed

composer.json

+14-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
"name": "qiniu/php-sdk",
33
"type": "library",
44
"description": "Qiniu Resource (Cloud) Storage SDK for PHP",
5-
"keywords": ["qiniu", "storage", "sdk", "cloud"],
5+
"keywords": [
6+
"qiniu",
7+
"storage",
8+
"sdk",
9+
"cloud"
10+
],
611
"homepage": "http://developer.qiniu.com/",
712
"license": "MIT",
813
"authors": [
@@ -16,11 +21,16 @@
1621
"php": ">=5.3.3"
1722
},
1823
"require-dev": {
24+
"paragonie/random_compat": ">=2",
1925
"phpunit/phpunit": "~4.0",
2026
"squizlabs/php_codesniffer": "~3.6"
2127
},
2228
"autoload": {
23-
"psr-4": {"Qiniu\\": "src/Qiniu"},
24-
"files": ["src/Qiniu/functions.php"]
29+
"psr-4": {
30+
"Qiniu\\": "src/Qiniu"
31+
},
32+
"files": [
33+
"src/Qiniu/functions.php"
34+
]
2535
}
26-
}
36+
}

src/Qiniu/Http/Client.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private static function escapeQuotes($str)
152152
$replace = array("\\\\", "\\\"");
153153
return str_replace($find, $replace, $str);
154154
}
155-
155+
156156
private static function ucwordsHyphen($str)
157157
{
158158
return str_replace('- ', '-', ucwords(str_replace('-', '- ', $str)));

src/Qiniu/Http/Response.php

+10
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ public function json()
128128
return $this->jsonData;
129129
}
130130

131+
public function headers()
132+
{
133+
return $this->headers;
134+
}
135+
136+
public function body()
137+
{
138+
return $this->body;
139+
}
140+
131141
private static function bodyJson($body)
132142
{
133143
return \Qiniu\json_decode((string) $body, true, 512);

src/Qiniu/Storage/ResumeUploader.php

+31-6
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ public function upload($fname)
171171
$response = $this->makeBlock($data, $blockSize);
172172
} else {
173173
$md5 = md5($data);
174-
$response = $this->uploadPart($data, $partNumber, $this->finishedEtags["uploadId"], $encodedObjectName);
174+
$response = $this->uploadPart(
175+
$data,
176+
$partNumber,
177+
$this->finishedEtags["uploadId"],
178+
$encodedObjectName,
179+
$md5
180+
);
175181
}
176182

177183
$ret = null;
@@ -203,7 +209,8 @@ public function upload($fname)
203209
$data,
204210
$partNumber,
205211
$this->finishedEtags["uploadId"],
206-
$encodedObjectName
212+
$encodedObjectName,
213+
$md5
207214
);
208215
$ret = $response->json();
209216
}
@@ -330,12 +337,12 @@ private function initReq($encodedObjectName)
330337
/**
331338
* 分块上传v2
332339
*/
333-
private function uploadPart($block, $partNumber, $uploadId, $encodedObjectName)
340+
private function uploadPart($block, $partNumber, $uploadId, $encodedObjectName, $md5)
334341
{
335342
$headers = array(
336343
'Authorization' => 'UpToken ' . $this->upToken,
337344
'Content-Type' => 'application/octet-stream',
338-
'Content-MD5' => $block
345+
'Content-MD5' => $md5
339346
);
340347
$url = $this->host.'/buckets/'.$this->bucket.'/objects/'.$encodedObjectName.
341348
'/uploads/'.$uploadId.'/'.$partNumber;
@@ -351,10 +358,28 @@ private function completeParts($fname, $uploadId, $encodedObjectName)
351358
);
352359
$etags = $this->finishedEtags['etags'];
353360
$sortedEtags = \Qiniu\arraySort($etags, 'partNumber');
361+
$metadata = array();
362+
$customVars = array();
363+
if ($this->params) {
364+
foreach ($this->params as $k => $v) {
365+
if (strpos($k, 'x:') === 0) {
366+
$customVars[$k] = $v;
367+
} elseif (strpos($k, 'x-qn-meta-') === 0) {
368+
$metadata[$k] = $v;
369+
}
370+
}
371+
}
372+
if (empty($metadata)) {
373+
$metadata = null;
374+
}
375+
if (empty($customVars)) {
376+
$customVars = null;
377+
}
354378
$body = array(
355379
'fname' => $fname,
356-
'$mimeType' => $this->mime,
357-
'customVars' => $this->params,
380+
'mimeType' => $this->mime,
381+
'metadata' => $metadata,
382+
'customVars' => $customVars,
358383
'parts' => $sortedEtags
359384
);
360385
$jsonBody = json_encode($body);

src/Qiniu/Storage/UploadManager.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function put(
4848
$mime = 'application/octet-stream',
4949
$fname = "default_filename"
5050
) {
51-
51+
5252
$params = self::trimParams($params);
5353
return FormUploader::put(
5454
$upToken,
@@ -92,7 +92,7 @@ public function putFile(
9292
$version = 'v1',
9393
$partSize = config::BLOCK_SIZE
9494
) {
95-
95+
9696
$file = fopen($filePath, 'rb');
9797
if ($file === false) {
9898
throw new \Exception("file can not open", 1);

tests/Qiniu/Tests/CdnManagerTest.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -56,43 +56,43 @@ protected function setUp()
5656
public function testRefreshUrls()
5757
{
5858
list($ret, $err) = $this->cdnManager->refreshUrls(array($this->refreshUrl));
59-
$this->assertNotNull($ret);
6059
$this->assertNull($err);
60+
$this->assertNotNull($ret);
6161
}
6262

6363
public function testRefreshDirs()
6464
{
6565
list($ret, $err) = $this->cdnManager->refreshDirs(array($this->refreshDirs));
66-
$this->assertNotNull($ret);
6766
$this->assertNull($err);
67+
$this->assertNotNull($ret);
6868
}
6969

7070
public function testRefreshUrlsAndDirs()
7171
{
7272
list($ret, $err) = $this->cdnManager->refreshUrlsAndDirs(array($this->refreshUrl), array($this->refreshDirs));
73-
$this->assertNotNull($ret);
7473
$this->assertNull($err);
74+
$this->assertNotNull($ret);
7575
}
7676

7777
public function testGetCdnRefreshList()
7878
{
7979
list($ret, $err) = $this->cdnManager->getCdnRefreshList(null, null, null, 'success');
80-
$this->assertNotNull($ret);
8180
$this->assertNull($err);
81+
$this->assertNotNull($ret);
8282
}
8383

8484
public function testPrefetchUrls()
8585
{
8686
list($ret, $err) = $this->cdnManager->prefetchUrls(array($this->refreshUrl));
87-
$this->assertNotNull($ret);
8887
$this->assertNull($err);
88+
$this->assertNotNull($ret);
8989
}
9090

9191
public function testGetCdnPrefetchList()
9292
{
9393
list($ret, $err) = $this->cdnManager->getCdnPrefetchList(null, null, 'success');
94-
$this->assertNotNull($ret);
9594
$this->assertNull($err);
95+
$this->assertNotNull($ret);
9696
}
9797

9898
public function testGetBandwidthData()
@@ -103,8 +103,8 @@ public function testGetBandwidthData()
103103
$this->testEndDate,
104104
$this->testGranularity
105105
);
106-
$this->assertNotNull($ret);
107106
$this->assertNull($err);
107+
$this->assertNotNull($ret);
108108
}
109109

110110
public function testGetFluxData()
@@ -115,31 +115,31 @@ public function testGetFluxData()
115115
$this->testEndDate,
116116
$this->testGranularity
117117
);
118-
$this->assertNotNull($ret);
119118
$this->assertNull($err);
119+
$this->assertNotNull($ret);
120120
}
121121

122122
public function testGetCdnLogList()
123123
{
124124
list($ret, $err) = $this->cdnManager->getCdnLogList(array('fake.qiniu.com'), $this->testLogDate);
125-
$this->assertNull($ret);
126125
$this->assertNotNull($err);
126+
$this->assertNull($ret);
127127
}
128128

129129
public function testCreateTimestampAntiLeechUrl()
130130
{
131131
$signUrl = $this->cdnManager->createTimestampAntiLeechUrl($this->refreshUrl, $this->encryptKey, 3600);
132132
$response = Client::get($signUrl);
133-
$this->assertEquals($response->statusCode, 200);
134133
$this->assertNull($response->error);
134+
$this->assertEquals($response->statusCode, 200);
135135

136136
$signUrl = $this->cdnManager->createTimestampAntiLeechUrl(
137137
$this->refreshUrl . '?qiniu',
138138
$this->encryptKey,
139139
3600
140140
);
141141
$response = Client::get($signUrl);
142-
$this->assertEquals($response->statusCode, 200);
143142
$this->assertNull($response->error);
143+
$this->assertEquals($response->statusCode, 200);
144144
}
145145
}

tests/Qiniu/Tests/EtagTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class EtagTest extends \PHPUnit_Framework_TestCase
77
{
88
public function test0M()
99
{
10-
$file = qiniuTempFile(0);
10+
$file = qiniuTempFile(0, false);
1111
list($r, $error) = Etag::sum($file);
1212
unlink($file);
1313
$this->assertEquals('Fto5o-5ea0sNMlW_75VgGJCv2AcJ', $r);
@@ -16,7 +16,7 @@ public function test0M()
1616

1717
public function testLess4M()
1818
{
19-
$file = qiniuTempFile(3 * 1024 * 1024);
19+
$file = qiniuTempFile(3 * 1024 * 1024, false);
2020
list($r, $error) = Etag::sum($file);
2121
unlink($file);
2222
$this->assertEquals('Fs5BpnAjRykYTg6o5E09cjuXrDkG', $r);
@@ -25,7 +25,7 @@ public function testLess4M()
2525

2626
public function test4M()
2727
{
28-
$file = qiniuTempFile(4 * 1024 * 1024);
28+
$file = qiniuTempFile(4 * 1024 * 1024, false);
2929
list($r, $error) = Etag::sum($file);
3030
unlink($file);
3131
$this->assertEquals('FiuKULnybewpEnrfTmxjsxc-3dWp', $r);
@@ -34,7 +34,7 @@ public function test4M()
3434

3535
public function testMore4M()
3636
{
37-
$file = qiniuTempFile(5 * 1024 * 1024);
37+
$file = qiniuTempFile(5 * 1024 * 1024, false);
3838
list($r, $error) = Etag::sum($file);
3939
unlink($file);
4040
$this->assertEquals('lhvyfIWMYFTq4s4alzlhXoAkqfVL', $r);
@@ -43,7 +43,7 @@ public function testMore4M()
4343

4444
public function test8M()
4545
{
46-
$file = qiniuTempFile(8 * 1024 * 1024);
46+
$file = qiniuTempFile(8 * 1024 * 1024, false);
4747
list($r, $error) = Etag::sum($file);
4848
unlink($file);
4949
$this->assertEquals('lmRm9ZfGZ86bnMys4wRTWtJj9ClG', $r);

0 commit comments

Comments
 (0)