From ee752ffa7263ce99fca0bd7340cf13c486a3516c Mon Sep 17 00:00:00 2001 From: lihsai0 Date: Fri, 25 Oct 2024 16:39:01 +0800 Subject: [PATCH] feat: add support for pfop workflow template (#435) --- CHANGELOG.md | 3 + src/Qiniu/Auth.php | 3 +- src/Qiniu/Config.php | 2 +- src/Qiniu/Processing/PersistentFop.php | 14 +- src/Qiniu/Storage/ArgusManager.php | 2 - tests/Qiniu/Tests/PfopTest.php | 170 ++++++++++++++++++++----- 6 files changed, 157 insertions(+), 37 deletions(-) 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/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/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 分块上传块大小,该参数为接口规格,不能修改 diff --git a/src/Qiniu/Processing/PersistentFop.php b/src/Qiniu/Processing/PersistentFop.php index 3d831561..8dca4a9f 100644 --- a/src/Qiniu/Processing/PersistentFop.php +++ b/src/Qiniu/Processing/PersistentFop.php @@ -61,19 +61,27 @@ 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); + + 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); \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/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(); diff --git a/tests/Qiniu/Tests/PfopTest.php b/tests/Qiniu/Tests/PfopTest.php index 1d10ac78..77d06ecb 100755 --- a/tests/Qiniu/Tests/PfopTest.php +++ b/tests/Qiniu/Tests/PfopTest.php @@ -3,13 +3,34 @@ 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 prepareEnvironment() + { + global $bucketName; + global $testAuth; + + self::$bucketName = $bucketName; + self::$testAuth = $testAuth; + } + private static function getConfig() { // use this func to test in test env @@ -52,7 +73,7 @@ public function testPfopExecuteAndStatusWithMultipleFops() $this->assertNull($error); } - private function pfopTypeTestData() + private function pfopOptionsTestData() { return array( array( @@ -69,23 +90,48 @@ 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) { + $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( + $bucket, + implode( + '_', + array( + 'test-pfop/test-pfop-by-api', + 'type', + $type + ) + ) + ); + $fops = 'avinfo|saveas/' . $persistentEntry; + } list($id, $error) = $pfop->execute( $bucket, $key, @@ -93,17 +139,27 @@ public function testPfopWithIdleTimeType() null, null, false, - $testCase['type'] + $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 ($workflowTemplateID) { + // assertStringContainsString when PHPUnit >= 8.0 + $this->assertTrue( + strpos( + $status['taskFrom'], + $workflowTemplateID + ) !== false + ); + } $this->assertNotEmpty($status['creationDate']); } else { $this->assertNotNull($error); @@ -111,24 +167,70 @@ public function testPfopWithIdleTimeType() } } + 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 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) { + $workflowTemplateID = null; + $type = null; + + if (array_key_exists('workflowTemplateID', $testCase)) { + $workflowTemplateID = $testCase['workflowTemplateID']; + } + if (array_key_exists('type', $testCase)) { + $type = $testCase['type']; + } + $putPolicy = array( - 'persistentOps' => $fops, - 'persistentType' => $testCase['type'] + 'persistentType' => $type ); + if ($workflowTemplateID) { + $putPolicy['persistentWorkflowTemplateID'] = $workflowTemplateID; + } else { + $persistentEntry = \Qiniu\entry( + $bucket, + implode( + '_', + array( + 'test-pfop/test-pfop-by-upload', + 'type', + $type + ) + ) + ); + $putPolicy['persistentOps'] = 'avinfo|saveas/' . $persistentEntry; + } - if ($testCase['type'] == null) { + if ($type == null) { unset($putPolicy['persistentType']); } @@ -148,7 +250,7 @@ public function testPfopByUploadPolicy() 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']; @@ -162,19 +264,27 @@ public function testPfopByUploadPolicy() $this->assertNotNull($status); $this->assertNull($error); - if ($testCase['type'] == 1) { + if ($type == 1) { $this->assertEquals(1, $status['type']); } + if ($workflowTemplateID) { + // assertStringContainsString when PHPUnit >= 8.0 + $this->assertTrue( + strpos( + $status['taskFrom'], + $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';