Skip to content

Commit

Permalink
feat: add idle-time pfop support and pfop configure api host
Browse files Browse the repository at this point in the history
  • Loading branch information
lihsai0 committed Sep 5, 2024
1 parent 29f746c commit e638410
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/Qiniu/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ public function uploadToken($bucket, $key = null, $expires = 3600, $policy = nul
'persistentOps',
'persistentNotifyUrl',
'persistentPipeline',
'persistentType', // 为 `1` 时开启闲时任务

'deleteAfterDays',
'fileType',
Expand Down
2 changes: 1 addition & 1 deletion src/Qiniu/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 分块上传块大小,该参数为接口规格,不能修改

Expand Down
38 changes: 32 additions & 6 deletions src/Qiniu/Processing/PersistentFop.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Qiniu\Http\Error;
use Qiniu\Http\Client;
use Qiniu\Http\Proxy;
use Qiniu\Zone;

/**
* 持久化处理类,该类用于主动触发异步持久化操作.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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());
Expand All @@ -84,18 +95,33 @@ 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://";

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;
}
}
84 changes: 79 additions & 5 deletions tests/Qiniu/Tests/PfopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -24,7 +33,7 @@ public function testPfop()
}


public function testPfops()
public function testPfopExecuteAndStatusWithMultipleFops()
{
global $testAuth;
$bucket = 'testres';
Expand All @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit e638410

Please sign in to comment.