From 36a494233324db9a02e8b8173e7a4fed0249ddce Mon Sep 17 00:00:00 2001 From: lihsai0 Date: Mon, 14 Oct 2024 21:14:54 +0800 Subject: [PATCH 1/6] feat: add workflow template id for pfop --- src/Qiniu/Auth.php | 3 +- src/Qiniu/Processing/PersistentFop.php | 9 +- tests/Qiniu/Tests/PfopTest.php | 114 +++++++++++++++++++------ 3 files changed, 97 insertions(+), 29 deletions(-) diff --git a/src/Qiniu/Auth.php b/src/Qiniu/Auth.php index 89b625f4..6da2be4e 100644 --- a/src/Qiniu/Auth.php +++ b/src/Qiniu/Auth.php @@ -219,10 +219,11 @@ public function uploadToken($bucket, $key = null, $expires = 3600, $policy = nul 'fsizeMin', 'fsizeLimit', - 'persistentOps', + 'persistentOps', // 与 persistentWorkflowTemplateID 二选一 'persistentNotifyUrl', 'persistentPipeline', 'persistentType', // 为 `1` 时开启闲时任务 + 'persistentWorkflowTemplateID', // 与 persistentOps 二选一 'deleteAfterDays', 'fileType', diff --git a/src/Qiniu/Processing/PersistentFop.php b/src/Qiniu/Processing/PersistentFop.php index 3d831561..acb9f15e 100644 --- a/src/Qiniu/Processing/PersistentFop.php +++ b/src/Qiniu/Processing/PersistentFop.php @@ -61,19 +61,22 @@ public function __construct($auth, $config = null, $proxy = null, $proxy_auth = public function execute( $bucket, $key, - $fops, + $fops = null, $pipeline = null, $notify_url = null, $force = false, - $type = null + $type = null, + $workflow_template_id = null ) { if (is_array($fops)) { $fops = implode(';', $fops); } - $params = array('bucket' => $bucket, 'key' => $key, 'fops' => $fops); + $params = array('bucket' => $bucket, 'key' => $key); + \Qiniu\setWithoutEmpty($params, 'fops', $fops); \Qiniu\setWithoutEmpty($params, 'pipeline', $pipeline); \Qiniu\setWithoutEmpty($params, 'notifyURL', $notify_url); \Qiniu\setWithoutEmpty($params, 'type', $type); + \Qiniu\setWithoutEmpty($params, 'workflowTemplateID', $workflow_template_id); if ($force) { $params['force'] = 1; } diff --git a/tests/Qiniu/Tests/PfopTest.php b/tests/Qiniu/Tests/PfopTest.php index 1d10ac78..108a89b4 100755 --- a/tests/Qiniu/Tests/PfopTest.php +++ b/tests/Qiniu/Tests/PfopTest.php @@ -3,13 +3,32 @@ use PHPUnit\Framework\TestCase; +use Qiniu\Auth; use Qiniu\Processing\PersistentFop; use Qiniu\Storage\UploadManager; -use Qiniu\Region; -use Qiniu\Config; +//use Qiniu\Region; +//use Qiniu\Config; class PfopTest extends TestCase { + /** + * @var Auth + */ + private static $testAuth; + + private static $bucketName; + /** + * @beforeClass + */ + public static function setupBeforeClass() + { + global $bucketName; + global $testAuth; + + self::$bucketName = $bucketName; + self::$testAuth = $testAuth; + } + private static function getConfig() { // use this func to test in test env @@ -52,7 +71,7 @@ public function testPfopExecuteAndStatusWithMultipleFops() $this->assertNull($error); } - private function pfopTypeTestData() + private function pfopOptionsTestData() { return array( array( @@ -69,23 +88,38 @@ private function pfopTypeTestData() ), array( 'type' => 2 + ), + array( + 'workflowTemplateID' => 'test-workflow' ) ); } - public function testPfopWithIdleTimeType() + public function testPfopExecuteWithOptions() { - global $testAuth; - - $bucket = 'testres'; - $key = 'sintel_trailer.mp4'; - $persistentEntry = \Qiniu\entry($bucket, 'test-pfop-type_1'); - $fops = 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240|saveas/' . $persistentEntry; - $pfop = new PersistentFop($testAuth, self::getConfig()); + $bucket = self::$bucketName; + $key = 'qiniu.png'; + $pfop = new PersistentFop(self::$testAuth, self::getConfig()); - $testCases = $this->pfopTypeTestData(); + $testCases = $this->pfopOptionsTestData(); foreach ($testCases as $testCase) { + if ($testCase['workflowTemplateID']) { + $fops = null; + } else { + $persistentEntry = \Qiniu\entry( + $bucket, + implode( + '_', + array( + 'test-pfop/test-pfop-by-api', + 'type', + $testCase['type'] + ) + ) + ); + $fops = 'avinfo|saveas/' . $persistentEntry; + } list($id, $error) = $pfop->execute( $bucket, $key, @@ -93,7 +127,8 @@ public function testPfopWithIdleTimeType() null, null, false, - $testCase['type'] + $testCase['type'], + $testCase['workflowTemplateID'] ); if (in_array($testCase['type'], array(null, 0, 1))) { @@ -104,6 +139,15 @@ public function testPfopWithIdleTimeType() if ($testCase['type'] == 1) { $this->assertEquals(1, $status['type']); } + if ($testCase['workflowTemplateID']) { + // assertStringContainsString when PHPUnit >= 8.0 + $this->assertTrue( + strpos( + $status['taskFrom'], + $testCase['workflowTemplateID'] + ) !== false + ); + } $this->assertNotEmpty($status['creationDate']); } else { $this->assertNotNull($error); @@ -111,22 +155,34 @@ public function testPfopWithIdleTimeType() } } - - public function testPfopByUploadPolicy() + public function testPfopWithUploadPolicy() { - global $testAuth; - $bucket = 'testres'; - $key = 'sintel_trailer.mp4'; - $persistentEntry = \Qiniu\entry($bucket, 'test-pfop-type_1'); - $fops = 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240|saveas/' . $persistentEntry; + $bucket = self::$bucketName; + $testAuth = self::$testAuth; + $key = 'test-pfop/upload-file'; - $testCases = $this->pfopTypeTestData(); + $testCases = $this->pfopOptionsTestData(); foreach ($testCases as $testCase) { $putPolicy = array( - 'persistentOps' => $fops, 'persistentType' => $testCase['type'] ); + if ($testCase['workflowTemplateID']) { + $putPolicy['persistentWorkflowTemplateID'] = $testCase['workflowTemplateID']; + } else { + $persistentEntry = \Qiniu\entry( + $bucket, + implode( + '_', + array( + 'test-pfop/test-pfop-by-upload', + 'type', + $testCase['type'] + ) + ) + ); + $putPolicy['persistentOps'] = 'avinfo|saveas/' . $persistentEntry; + } if ($testCase['type'] == null) { unset($putPolicy['persistentType']); @@ -165,16 +221,24 @@ public function testPfopByUploadPolicy() if ($testCase['type'] == 1) { $this->assertEquals(1, $status['type']); } + if ($testCase['workflowTemplateID']) { + // assertStringContainsString when PHPUnit >= 8.0 + $this->assertTrue( + strpos( + $status['taskFrom'], + $testCase['workflowTemplateID'] + ) !== false + ); + } $this->assertNotEmpty($status['creationDate']); } } public function testMkzip() { - global $testAuth; - $bucket = 'phpsdk'; + $bucket = self::$bucketName; $key = 'php-logo.png'; - $pfop = new PersistentFop($testAuth, null); + $pfop = new PersistentFop(self::$testAuth, null); $url1 = 'http://phpsdk.qiniudn.com/php-logo.png'; $url2 = 'http://phpsdk.qiniudn.com/php-sdk.html'; From 2a2fe122b4595f8f2eba35833b6287f37babc07f Mon Sep 17 00:00:00 2001 From: lihsai0 Date: Wed, 16 Oct 2024 09:59:42 +0800 Subject: [PATCH 2/6] update version to 7.14.0 and changelog --- CHANGELOG.md | 3 +++ src/Qiniu/Config.php | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 017acf0f..6f8da5d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 7.14.0 (2024-10-16) +* 对象存储,持久化处理支持工作流模版 + ## 7.13.0 (2024-09-05) * 对象存储,验证回调方法新增支持 Qiniu 签名 * 对象存储,调整查询空间区域域名顺序与默认空间管理域名 diff --git a/src/Qiniu/Config.php b/src/Qiniu/Config.php index 0e1e67dd..3ce7fa5a 100644 --- a/src/Qiniu/Config.php +++ b/src/Qiniu/Config.php @@ -4,7 +4,7 @@ final class Config { - const SDK_VER = '7.13.0'; + const SDK_VER = '7.14.0'; const BLOCK_SIZE = 4194304; //4*1024*1024 分块上传块大小,该参数为接口规格,不能修改 From 12d433021afb3da9094179dda0fea5d04ccd004c Mon Sep 17 00:00:00 2001 From: lihsai0 Date: Wed, 16 Oct 2024 15:48:24 +0800 Subject: [PATCH 3/6] style: fix code style --- tests/Qiniu/Tests/PfopTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Qiniu/Tests/PfopTest.php b/tests/Qiniu/Tests/PfopTest.php index 108a89b4..e268ef92 100755 --- a/tests/Qiniu/Tests/PfopTest.php +++ b/tests/Qiniu/Tests/PfopTest.php @@ -6,6 +6,7 @@ use Qiniu\Auth; use Qiniu\Processing\PersistentFop; use Qiniu\Storage\UploadManager; + //use Qiniu\Region; //use Qiniu\Config; @@ -17,10 +18,11 @@ class PfopTest extends TestCase private static $testAuth; private static $bucketName; + /** * @beforeClass */ - public static function setupBeforeClass() + public static function prepareEnvironment() { global $bucketName; global $testAuth; From a78e11af54da932e47f14cc28559322b7f47468e Mon Sep 17 00:00:00 2001 From: lihsai0 Date: Wed, 16 Oct 2024 16:58:43 +0800 Subject: [PATCH 4/6] test: fix cases --- tests/Qiniu/Tests/PfopTest.php | 54 +++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/tests/Qiniu/Tests/PfopTest.php b/tests/Qiniu/Tests/PfopTest.php index e268ef92..1fbce7e8 100755 --- a/tests/Qiniu/Tests/PfopTest.php +++ b/tests/Qiniu/Tests/PfopTest.php @@ -106,7 +106,17 @@ public function testPfopExecuteWithOptions() $testCases = $this->pfopOptionsTestData(); foreach ($testCases as $testCase) { - if ($testCase['workflowTemplateID']) { + $workflowTemplateID = null; + $type = null; + + if (array_key_exists('workflowTemplateID', $testCase)) { + $workflowTemplateID = $testCase['workflowTemplateID']; + } + if (array_key_exists('type', $testCase)) { + $type = $testCase['type']; + } + + if ($workflowTemplateID) { $fops = null; } else { $persistentEntry = \Qiniu\entry( @@ -116,7 +126,7 @@ public function testPfopExecuteWithOptions() array( 'test-pfop/test-pfop-by-api', 'type', - $testCase['type'] + $type ) ) ); @@ -129,24 +139,24 @@ public function testPfopExecuteWithOptions() null, null, false, - $testCase['type'], - $testCase['workflowTemplateID'] + $type, + $workflowTemplateID ); - if (in_array($testCase['type'], array(null, 0, 1))) { + if (in_array($type, array(null, 0, 1))) { $this->assertNull($error); list($status, $error) = $pfop->status($id); $this->assertNotNull($status); $this->assertNull($error); - if ($testCase['type'] == 1) { + if ($type == 1) { $this->assertEquals(1, $status['type']); } - if ($testCase['workflowTemplateID']) { + if ($workflowTemplateID) { // assertStringContainsString when PHPUnit >= 8.0 $this->assertTrue( strpos( $status['taskFrom'], - $testCase['workflowTemplateID'] + $workflowTemplateID ) !== false ); } @@ -166,11 +176,21 @@ public function testPfopWithUploadPolicy() $testCases = $this->pfopOptionsTestData(); foreach ($testCases as $testCase) { + $workflowTemplateID = null; + $type = null; + + if (array_key_exists('workflowTemplateID', $testCase)) { + $workflowTemplateID = $testCase['workflowTemplateID']; + } + if (array_key_exists('type', $testCase)) { + $type = $testCase['type']; + } + $putPolicy = array( - 'persistentType' => $testCase['type'] + 'persistentType' => $type ); - if ($testCase['workflowTemplateID']) { - $putPolicy['persistentWorkflowTemplateID'] = $testCase['workflowTemplateID']; + if ($workflowTemplateID) { + $putPolicy['persistentWorkflowTemplateID'] = $workflowTemplateID; } else { $persistentEntry = \Qiniu\entry( $bucket, @@ -179,14 +199,14 @@ public function testPfopWithUploadPolicy() array( 'test-pfop/test-pfop-by-upload', 'type', - $testCase['type'] + $type ) ) ); $putPolicy['persistentOps'] = 'avinfo|saveas/' . $persistentEntry; } - if ($testCase['type'] == null) { + if ($type == null) { unset($putPolicy['persistentType']); } @@ -206,7 +226,7 @@ public function testPfopWithUploadPolicy() true ); - if (in_array($testCase['type'], array(null, 0, 1))) { + if (in_array($type, array(null, 0, 1))) { $this->assertNull($error); $this->assertNotEmpty($ret['persistentId']); $id = $ret['persistentId']; @@ -220,15 +240,15 @@ public function testPfopWithUploadPolicy() $this->assertNotNull($status); $this->assertNull($error); - if ($testCase['type'] == 1) { + if ($type == 1) { $this->assertEquals(1, $status['type']); } - if ($testCase['workflowTemplateID']) { + if ($workflowTemplateID) { // assertStringContainsString when PHPUnit >= 8.0 $this->assertTrue( strpos( $status['taskFrom'], - $testCase['workflowTemplateID'] + $workflowTemplateID ) !== false ); } From d3c4ed8f68feb2725c4e79cc167d583fc0f22142 Mon Sep 17 00:00:00 2001 From: lihsai0 Date: Wed, 16 Oct 2024 17:49:07 +0800 Subject: [PATCH 5/6] chore: remove useless print --- src/Qiniu/Storage/ArgusManager.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Qiniu/Storage/ArgusManager.php b/src/Qiniu/Storage/ArgusManager.php index 5f5e5544..51b4200c 100644 --- a/src/Qiniu/Storage/ArgusManager.php +++ b/src/Qiniu/Storage/ArgusManager.php @@ -84,7 +84,6 @@ public function censorStatus($jobid) $url = $scheme . Config::ARGUS_HOST . "/v3/jobs/video/$jobid"; $response = $this->get($url); if (!$response->ok()) { - print("statusCode: " . $response->statusCode); return array(null, new Error($url, $response)); } return array($response->json(), null); @@ -118,7 +117,6 @@ private function post($url, $body) $headers['Content-Type'] = 'application/json'; $ret = Client::post($url, $body, $headers, $this->proxy->makeReqOpt()); if (!$ret->ok()) { - print("statusCode: " . $ret->statusCode); return array(null, new Error($url, $ret)); } $r = ($ret->body === null) ? array() : $ret->json(); From 796b8c7deb42c427ada48b79300558375097e3a3 Mon Sep 17 00:00:00 2001 From: lihsai0 Date: Tue, 22 Oct 2024 15:12:18 +0800 Subject: [PATCH 6/6] feat: pfop execute add fops and template checking --- src/Qiniu/Processing/PersistentFop.php | 5 +++++ tests/Qiniu/Tests/PfopTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/Qiniu/Processing/PersistentFop.php b/src/Qiniu/Processing/PersistentFop.php index acb9f15e..8dca4a9f 100644 --- a/src/Qiniu/Processing/PersistentFop.php +++ b/src/Qiniu/Processing/PersistentFop.php @@ -71,6 +71,11 @@ public function execute( if (is_array($fops)) { $fops = implode(';', $fops); } + + if (!$fops && !$workflow_template_id) { + throw new \InvalidArgumentException('Must provide one of fops or template_id'); + } + $params = array('bucket' => $bucket, 'key' => $key); \Qiniu\setWithoutEmpty($params, 'fops', $fops); \Qiniu\setWithoutEmpty($params, 'pipeline', $pipeline); diff --git a/tests/Qiniu/Tests/PfopTest.php b/tests/Qiniu/Tests/PfopTest.php index 1fbce7e8..77d06ecb 100755 --- a/tests/Qiniu/Tests/PfopTest.php +++ b/tests/Qiniu/Tests/PfopTest.php @@ -167,6 +167,30 @@ public function testPfopExecuteWithOptions() } } + public function testPfopWithInvalidArgument() + { + $bucket = self::$bucketName; + $key = 'qiniu.png'; + $pfop = new PersistentFop(self::$testAuth, self::getConfig()); + $err = null; + try { + $pfop->execute( + $bucket, + $key + ); + } catch (\Exception $e) { + $err = $e; + } + + $this->assertNotEmpty($err); + $this->assertTrue( + strpos( + $err->getMessage(), + 'Must provide one of fops or template_id' + ) !== false + ); + } + public function testPfopWithUploadPolicy() { $bucket = self::$bucketName;