diff --git a/examples/rs_change_type.php b/examples/rs_change_type.php index c1d7b86f..8b3201fc 100644 --- a/examples/rs_change_type.php +++ b/examples/rs_change_type.php @@ -21,7 +21,12 @@ // 参考文档:https://developer.qiniu.com/kodo/api/3710/chtype $key = "qiniu.mp4"; -$fileType = 1; // 0 表示标准存储;1 表示低频存储;2 表示归档存储;3 表示深度归档存储;4 表示归档直读存储; +// 0 表示标准存储; +// 1 表示低频存储; +// 2 表示归档存储; +// 3 表示深度归档存储; +// 4 表示归档直读存储; +$fileType = 1; list($ret, $err) = $bucketManager->changeType($bucket, $key, $fileType); if ($err != null) { diff --git a/src/Qiniu/Storage/BucketManager.php b/src/Qiniu/Storage/BucketManager.php index 29b4a363..bfca4fc1 100644 --- a/src/Qiniu/Storage/BucketManager.php +++ b/src/Qiniu/Storage/BucketManager.php @@ -7,6 +7,7 @@ use Qiniu\Http\Error; use Qiniu\Http\Client; use Qiniu\Http\Proxy; +use Qiniu\Http\Response; /** * 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考 @@ -830,7 +831,12 @@ public function fetch($url, $bucket, $key = null) * @param string $callbackbody 回调Body * @param string $callbackbodytype 回调Body内容类型,默认为"application/x-www-form-urlencoded" * @param string $callbackhost 回调时使用的Host - * @param int $file_type 存储文件类型 0:标准存储(默认),1:低频存储,2:归档存储,3:深度归档存储,4:归档直读存储 + * @param int $file_type 存储文件类型 + * 0:标准存储(默认) + * 1:低频存储 + * 2:归档存储 + * 3:深度归档存储 + * 4:归档直读存储 * @param bool $ignore_same_key 如果空间中已经存在同名文件则放弃本次抓取 * @return array * @link https://developer.qiniu.com/kodo/api/4097/asynch-fetch @@ -943,7 +949,20 @@ public function batch($operations) $scheme = "https://"; } $params = 'op=' . implode('&op=', $operations); - return $this->postV2($scheme . Config::RS_HOST . '/batch', $params); + $errResp = new Response(0, 0); + if (count($operations) <= 0) { + $errResp->error = 'empty operations'; + return array(null, new Error($scheme . '/batch', $errResp)); + } + $bucket = ''; + foreach ($operations as $op) { + $segments = explode('/', $op); + if (count($segments) < 3) { + continue; + } + list($bucket,) = \Qiniu\decodeEntry($segments[2]); + } + return $this->rsPost($bucket, '/batch', $params); } /** diff --git a/src/Qiniu/functions.php b/src/Qiniu/functions.php index 5e0402ac..cc258d7e 100644 --- a/src/Qiniu/functions.php +++ b/src/Qiniu/functions.php @@ -140,6 +140,16 @@ function entry($bucket, $key = null) return base64_urlSafeEncode($en); } + function decodeEntry($entry) + { + $en = base64_urlSafeDecode($entry); + $en = explode(':', $en); + if (count($en) == 1) { + return array($en[0], null); + } + return array($en[0], $en[1]); + } + /** * array 辅助方法,无值时不set * diff --git a/tests/Qiniu/Tests/EntryTest.php b/tests/Qiniu/Tests/EntryTest.php index ebed1875..73bfac4c 100644 --- a/tests/Qiniu/Tests/EntryTest.php +++ b/tests/Qiniu/Tests/EntryTest.php @@ -46,4 +46,43 @@ public function testKeyNeedReplaceSlashSymbol() $encodeEntryURI = Qiniu\entry($bucket, $key); $this->assertEquals('cWluaXVwaG90b3M6MDEydHM_YQ==', $encodeEntryURI); } + public function testDecodeEntry() + { + $entry = 'cWluaXVwaG90b3M6Z29nb3BoZXIuanBn'; + list($bucket, $key) = Qiniu\decodeEntry($entry); + $this->assertEquals('qiniuphotos', $bucket); + $this->assertEquals('gogopher.jpg', $key); + } + + public function testDecodeEntryWithEmptyKey() + { + $entry = 'cWluaXVwaG90b3M6'; + list($bucket, $key) = Qiniu\decodeEntry($entry); + $this->assertEquals('qiniuphotos', $bucket); + $this->assertEquals('', $key); + } + + public function testDecodeEntryWithNullKey() + { + $entry = 'cWluaXVwaG90b3M='; + list($bucket, $key) = Qiniu\decodeEntry($entry); + $this->assertEquals('qiniuphotos', $bucket); + $this->assertNull($key); + } + + public function testDecodeEntryWithPlusSymbol() + { + $entry = 'cWluaXVwaG90b3M6MDEydHM-YQ=='; + list($bucket, $key) = Qiniu\decodeEntry($entry); + $this->assertEquals('qiniuphotos', $bucket); + $this->assertEquals('012ts>a', $key); + } + + public function testDecodeEntryWithSlashSymbol() + { + $entry = 'cWluaXVwaG90b3M6MDEydHM_YQ=='; + list($bucket, $key) = Qiniu\decodeEntry($entry); + $this->assertEquals('qiniuphotos', $bucket); + $this->assertEquals('012ts?a', $key); + } }