Skip to content

Commit

Permalink
Merge pull request #362 from qiniu/fix/resumable-upload-v2
Browse files Browse the repository at this point in the history
修复分片上传 v2 的 bug
  • Loading branch information
xwen-winnie authored Sep 24, 2021
2 parents 4e1fae5 + fe63159 commit 8fabdc0
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 36 deletions.
18 changes: 14 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"name": "qiniu/php-sdk",
"type": "library",
"description": "Qiniu Resource (Cloud) Storage SDK for PHP",
"keywords": ["qiniu", "storage", "sdk", "cloud"],
"keywords": [
"qiniu",
"storage",
"sdk",
"cloud"
],
"homepage": "http://developer.qiniu.com/",
"license": "MIT",
"authors": [
Expand All @@ -16,11 +21,16 @@
"php": ">=5.3.3"
},
"require-dev": {
"paragonie/random_compat": ">=2",
"phpunit/phpunit": "~4.0",
"squizlabs/php_codesniffer": "~3.6"
},
"autoload": {
"psr-4": {"Qiniu\\": "src/Qiniu"},
"files": ["src/Qiniu/functions.php"]
"psr-4": {
"Qiniu\\": "src/Qiniu"
},
"files": [
"src/Qiniu/functions.php"
]
}
}
}
2 changes: 1 addition & 1 deletion src/Qiniu/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private static function escapeQuotes($str)
$replace = array("\\\\", "\\\"");
return str_replace($find, $replace, $str);
}

private static function ucwordsHyphen($str)
{
return str_replace('- ', '-', ucwords(str_replace('-', '- ', $str)));
Expand Down
10 changes: 10 additions & 0 deletions src/Qiniu/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ public function json()
return $this->jsonData;
}

public function headers()
{
return $this->headers;
}

public function body()
{
return $this->body;
}

private static function bodyJson($body)
{
return \Qiniu\json_decode((string) $body, true, 512);
Expand Down
37 changes: 31 additions & 6 deletions src/Qiniu/Storage/ResumeUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,13 @@ public function upload($fname)
$response = $this->makeBlock($data, $blockSize);
} else {
$md5 = md5($data);
$response = $this->uploadPart($data, $partNumber, $this->finishedEtags["uploadId"], $encodedObjectName);
$response = $this->uploadPart(
$data,
$partNumber,
$this->finishedEtags["uploadId"],
$encodedObjectName,
$md5
);
}

$ret = null;
Expand Down Expand Up @@ -203,7 +209,8 @@ public function upload($fname)
$data,
$partNumber,
$this->finishedEtags["uploadId"],
$encodedObjectName
$encodedObjectName,
$md5
);
$ret = $response->json();
}
Expand Down Expand Up @@ -330,12 +337,12 @@ private function initReq($encodedObjectName)
/**
* 分块上传v2
*/
private function uploadPart($block, $partNumber, $uploadId, $encodedObjectName)
private function uploadPart($block, $partNumber, $uploadId, $encodedObjectName, $md5)
{
$headers = array(
'Authorization' => 'UpToken ' . $this->upToken,
'Content-Type' => 'application/octet-stream',
'Content-MD5' => $block
'Content-MD5' => $md5
);
$url = $this->host.'/buckets/'.$this->bucket.'/objects/'.$encodedObjectName.
'/uploads/'.$uploadId.'/'.$partNumber;
Expand All @@ -351,10 +358,28 @@ private function completeParts($fname, $uploadId, $encodedObjectName)
);
$etags = $this->finishedEtags['etags'];
$sortedEtags = \Qiniu\arraySort($etags, 'partNumber');
$metadata = array();
$customVars = array();
if ($this->params) {
foreach ($this->params as $k => $v) {
if (strpos($k, 'x:') === 0) {
$customVars[$k] = $v;
} elseif (strpos($k, 'x-qn-meta-') === 0) {
$metadata[$k] = $v;
}
}
}
if (empty($metadata)) {
$metadata = null;
}
if (empty($customVars)) {
$customVars = null;
}
$body = array(
'fname' => $fname,
'$mimeType' => $this->mime,
'customVars' => $this->params,
'mimeType' => $this->mime,
'metadata' => $metadata,
'customVars' => $customVars,
'parts' => $sortedEtags
);
$jsonBody = json_encode($body);
Expand Down
4 changes: 2 additions & 2 deletions src/Qiniu/Storage/UploadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function put(
$mime = 'application/octet-stream',
$fname = "default_filename"
) {

$params = self::trimParams($params);
return FormUploader::put(
$upToken,
Expand Down Expand Up @@ -92,7 +92,7 @@ public function putFile(
$version = 'v1',
$partSize = config::BLOCK_SIZE
) {

$file = fopen($filePath, 'rb');
if ($file === false) {
throw new \Exception("file can not open", 1);
Expand Down
22 changes: 11 additions & 11 deletions tests/Qiniu/Tests/CdnManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,43 +56,43 @@ protected function setUp()
public function testRefreshUrls()
{
list($ret, $err) = $this->cdnManager->refreshUrls(array($this->refreshUrl));
$this->assertNotNull($ret);
$this->assertNull($err);
$this->assertNotNull($ret);
}

public function testRefreshDirs()
{
list($ret, $err) = $this->cdnManager->refreshDirs(array($this->refreshDirs));
$this->assertNotNull($ret);
$this->assertNull($err);
$this->assertNotNull($ret);
}

public function testRefreshUrlsAndDirs()
{
list($ret, $err) = $this->cdnManager->refreshUrlsAndDirs(array($this->refreshUrl), array($this->refreshDirs));
$this->assertNotNull($ret);
$this->assertNull($err);
$this->assertNotNull($ret);
}

public function testGetCdnRefreshList()
{
list($ret, $err) = $this->cdnManager->getCdnRefreshList(null, null, null, 'success');
$this->assertNotNull($ret);
$this->assertNull($err);
$this->assertNotNull($ret);
}

public function testPrefetchUrls()
{
list($ret, $err) = $this->cdnManager->prefetchUrls(array($this->refreshUrl));
$this->assertNotNull($ret);
$this->assertNull($err);
$this->assertNotNull($ret);
}

public function testGetCdnPrefetchList()
{
list($ret, $err) = $this->cdnManager->getCdnPrefetchList(null, null, 'success');
$this->assertNotNull($ret);
$this->assertNull($err);
$this->assertNotNull($ret);
}

public function testGetBandwidthData()
Expand All @@ -103,8 +103,8 @@ public function testGetBandwidthData()
$this->testEndDate,
$this->testGranularity
);
$this->assertNotNull($ret);
$this->assertNull($err);
$this->assertNotNull($ret);
}

public function testGetFluxData()
Expand All @@ -115,31 +115,31 @@ public function testGetFluxData()
$this->testEndDate,
$this->testGranularity
);
$this->assertNotNull($ret);
$this->assertNull($err);
$this->assertNotNull($ret);
}

public function testGetCdnLogList()
{
list($ret, $err) = $this->cdnManager->getCdnLogList(array('fake.qiniu.com'), $this->testLogDate);
$this->assertNull($ret);
$this->assertNotNull($err);
$this->assertNull($ret);
}

public function testCreateTimestampAntiLeechUrl()
{
$signUrl = $this->cdnManager->createTimestampAntiLeechUrl($this->refreshUrl, $this->encryptKey, 3600);
$response = Client::get($signUrl);
$this->assertEquals($response->statusCode, 200);
$this->assertNull($response->error);
$this->assertEquals($response->statusCode, 200);

$signUrl = $this->cdnManager->createTimestampAntiLeechUrl(
$this->refreshUrl . '?qiniu',
$this->encryptKey,
3600
);
$response = Client::get($signUrl);
$this->assertEquals($response->statusCode, 200);
$this->assertNull($response->error);
$this->assertEquals($response->statusCode, 200);
}
}
10 changes: 5 additions & 5 deletions tests/Qiniu/Tests/EtagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class EtagTest extends \PHPUnit_Framework_TestCase
{
public function test0M()
{
$file = qiniuTempFile(0);
$file = qiniuTempFile(0, false);
list($r, $error) = Etag::sum($file);
unlink($file);
$this->assertEquals('Fto5o-5ea0sNMlW_75VgGJCv2AcJ', $r);
Expand All @@ -16,7 +16,7 @@ public function test0M()

public function testLess4M()
{
$file = qiniuTempFile(3 * 1024 * 1024);
$file = qiniuTempFile(3 * 1024 * 1024, false);
list($r, $error) = Etag::sum($file);
unlink($file);
$this->assertEquals('Fs5BpnAjRykYTg6o5E09cjuXrDkG', $r);
Expand All @@ -25,7 +25,7 @@ public function testLess4M()

public function test4M()
{
$file = qiniuTempFile(4 * 1024 * 1024);
$file = qiniuTempFile(4 * 1024 * 1024, false);
list($r, $error) = Etag::sum($file);
unlink($file);
$this->assertEquals('FiuKULnybewpEnrfTmxjsxc-3dWp', $r);
Expand All @@ -34,7 +34,7 @@ public function test4M()

public function testMore4M()
{
$file = qiniuTempFile(5 * 1024 * 1024);
$file = qiniuTempFile(5 * 1024 * 1024, false);
list($r, $error) = Etag::sum($file);
unlink($file);
$this->assertEquals('lhvyfIWMYFTq4s4alzlhXoAkqfVL', $r);
Expand All @@ -43,7 +43,7 @@ public function testMore4M()

public function test8M()
{
$file = qiniuTempFile(8 * 1024 * 1024);
$file = qiniuTempFile(8 * 1024 * 1024, false);
list($r, $error) = Etag::sum($file);
unlink($file);
$this->assertEquals('lmRm9ZfGZ86bnMys4wRTWtJj9ClG', $r);
Expand Down
Loading

0 comments on commit 8fabdc0

Please sign in to comment.