From e6384102317b47c6f4c19be960b4f6aa84e0407f Mon Sep 17 00:00:00 2001 From: lihsai0 Date: Thu, 29 Aug 2024 03:20:53 +0800 Subject: [PATCH] feat: add idle-time pfop support and pfop configure api host --- src/Qiniu/Auth.php | 1 + src/Qiniu/Config.php | 2 +- src/Qiniu/Processing/PersistentFop.php | 38 ++++++++++-- tests/Qiniu/Tests/PfopTest.php | 84 ++++++++++++++++++++++++-- 4 files changed, 113 insertions(+), 12 deletions(-) diff --git a/src/Qiniu/Auth.php b/src/Qiniu/Auth.php index e9c78ccd..89b625f4 100644 --- a/src/Qiniu/Auth.php +++ b/src/Qiniu/Auth.php @@ -222,6 +222,7 @@ public function uploadToken($bucket, $key = null, $expires = 3600, $policy = nul 'persistentOps', 'persistentNotifyUrl', 'persistentPipeline', + 'persistentType', // 为 `1` 时开启闲时任务 'deleteAfterDays', 'fileType', diff --git a/src/Qiniu/Config.php b/src/Qiniu/Config.php index 2bedd5fb..0e1e67dd 100644 --- a/src/Qiniu/Config.php +++ b/src/Qiniu/Config.php @@ -4,7 +4,7 @@ final class Config { - const SDK_VER = '7.12.1'; + const SDK_VER = '7.13.0'; const BLOCK_SIZE = 4194304; //4*1024*1024 分块上传块大小,该参数为接口规格,不能修改 diff --git a/src/Qiniu/Processing/PersistentFop.php b/src/Qiniu/Processing/PersistentFop.php index 97a4ff49..3d831561 100644 --- a/src/Qiniu/Processing/PersistentFop.php +++ b/src/Qiniu/Processing/PersistentFop.php @@ -6,6 +6,7 @@ use Qiniu\Http\Error; use Qiniu\Http\Client; use Qiniu\Http\Proxy; +use Qiniu\Zone; /** * 持久化处理类,该类用于主动触发异步持久化操作. @@ -45,25 +46,34 @@ public function __construct($auth, $config = null, $proxy = null, $proxy_auth = * 对资源文件进行异步持久化处理 * @param string $bucket 资源所在空间 * @param string $key 待处理的源文件 - * @param string $fops string|array 待处理的pfop操作,多个pfop操作以array的形式传入。 + * @param string|array $fops 待处理的pfop操作,多个pfop操作以array的形式传入。 * eg. avthumb/mp3/ab/192k, vframe/jpg/offset/7/w/480/h/360 * @param string $pipeline 资源处理队列 * @param string $notify_url 处理结果通知地址 * @param bool $force 是否强制执行一次新的指令 + * @param int $type 为 `1` 时开启闲时任务 * * - * @return array 返回持久化处理的persistentId, 和返回的错误。 + * @return array 返回持久化处理的 persistentId 与可能出现的错误。 * * @link http://developer.qiniu.com/docs/v6/api/reference/fop/ */ - public function execute($bucket, $key, $fops, $pipeline = null, $notify_url = null, $force = false) - { + public function execute( + $bucket, + $key, + $fops, + $pipeline = null, + $notify_url = null, + $force = false, + $type = null + ) { if (is_array($fops)) { $fops = implode(';', $fops); } $params = array('bucket' => $bucket, 'key' => $key, 'fops' => $fops); \Qiniu\setWithoutEmpty($params, 'pipeline', $pipeline); \Qiniu\setWithoutEmpty($params, 'notifyURL', $notify_url); + \Qiniu\setWithoutEmpty($params, 'type', $type); if ($force) { $params['force'] = 1; } @@ -72,7 +82,8 @@ public function execute($bucket, $key, $fops, $pipeline = null, $notify_url = nu if ($this->config->useHTTPS === true) { $scheme = "https://"; } - $url = $scheme . Config::API_HOST . '/pfop/'; + $apiHost = $this->getApiHost(); + $url = $scheme . $apiHost . '/pfop/'; $headers = $this->auth->authorization($url, $data, 'application/x-www-form-urlencoded'); $headers['Content-Type'] = 'application/x-www-form-urlencoded'; $response = Client::post($url, $data, $headers, $this->proxy->makeReqOpt()); @@ -84,6 +95,10 @@ public function execute($bucket, $key, $fops, $pipeline = null, $notify_url = nu return array($id, null); } + /** + * @param string $id + * @return array 返回任务状态与可能出现的错误 + */ public function status($id) { $scheme = "http://"; @@ -91,11 +106,22 @@ public function status($id) if ($this->config->useHTTPS === true) { $scheme = "https://"; } - $url = $scheme . Config::API_HOST . "/status/get/prefop?id=$id"; + $apiHost = $this->getApiHost(); + $url = $scheme . $apiHost . "/status/get/prefop?id=$id"; $response = Client::get($url, array(), $this->proxy->makeReqOpt()); if (!$response->ok()) { return array(null, new Error($url, $response)); } return array($response->json(), null); } + + private function getApiHost() + { + if (!empty($this->config->zone) && !empty($this->config->zone->apiHost)) { + $apiHost = $this->config->zone->apiHost; + } else { + $apiHost = Config::API_HOST; + } + return $apiHost; + } } diff --git a/tests/Qiniu/Tests/PfopTest.php b/tests/Qiniu/Tests/PfopTest.php index 19bacc00..60edb0eb 100755 --- a/tests/Qiniu/Tests/PfopTest.php +++ b/tests/Qiniu/Tests/PfopTest.php @@ -3,17 +3,26 @@ use PHPUnit\Framework\TestCase; -use Qiniu\Processing\Operation; use Qiniu\Processing\PersistentFop; +use Qiniu\Storage\UploadManager; +use Qiniu\Region; +use Qiniu\Config; class PfopTest extends TestCase { - public function testPfop() + private static function getConfig() + { + // use this func to test in test env + // `null` means to use production env + return null; + } + + public function testPfopExecuteAndStatusWithSingleFop() { global $testAuth; $bucket = 'testres'; $key = 'sintel_trailer.mp4'; - $pfop = new PersistentFop($testAuth, null); + $pfop = new PersistentFop($testAuth, self::getConfig()); $fops = 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240'; list($id, $error) = $pfop->execute($bucket, $key, $fops); @@ -24,7 +33,7 @@ public function testPfop() } - public function testPfops() + public function testPfopExecuteAndStatusWithMultipleFops() { global $testAuth; $bucket = 'testres'; @@ -33,7 +42,7 @@ public function testPfops() 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240', 'vframe/jpg/offset/7/w/480/h/360', ); - $pfop = new PersistentFop($testAuth, null); + $pfop = new PersistentFop($testAuth, self::getConfig()); list($id, $error) = $pfop->execute($bucket, $key, $fops); $this->assertNull($error); @@ -43,6 +52,71 @@ public function testPfops() $this->assertNull($error); } + public function testPfopWithIdleTimeType() + { + 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()); + + list($id, $error) = $pfop->execute( + $bucket, + $key, + $fops, + null, + null, + false, + 1 + ); + $this->assertNull($error); + list($status, $error) = $pfop->status($id); + $this->assertNotNull($status); + $this->assertNull($error); + $this->assertEquals(1, $status['type']); + $this->assertNotEmpty($status['creationDate']); + } + + public function testPfopByUploadPolicy() + { + 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; + + $token = $testAuth->uploadToken( + $bucket, + $key, + 3600, + array( + 'persistentOps' => $fops, + 'persistentType' => 1 + ) + ); + $upManager = new UploadManager(self::getConfig()); + list($ret, $error) = $upManager->putFile( + $token, + $key, + __file__, + null, + 'text/plain', + true + ); + $this->assertNull($error); + $this->assertNotEmpty($ret['persistentId']); + $id = $ret['persistentId']; + + $pfop = new PersistentFop($testAuth, self::getConfig()); + list($status, $error) = $pfop->status($id); + $this->assertNotNull($status); + $this->assertNull($error); + $this->assertEquals(1, $status['type']); + $this->assertNotEmpty($status['creationDate']); + } + public function testMkzip() { global $testAuth;