From 3d5c4eee2eef33dd299ddccd638a937bbf418c69 Mon Sep 17 00:00:00 2001 From: lihsai0 Date: Thu, 7 Dec 2023 11:39:24 +0800 Subject: [PATCH] fix batch rs host --- src/Qiniu/Storage/BucketManager.php | 16 +++++++++++- src/Qiniu/functions.php | 10 ++++++++ tests/Qiniu/Tests/EntryTest.php | 39 +++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/Qiniu/Storage/BucketManager.php b/src/Qiniu/Storage/BucketManager.php index 29b4a363..11eda79b 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; /** * 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考 @@ -943,7 +944,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); + } }