Skip to content

Commit

Permalink
feat: add support for pfop workflow template (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihsai0 authored Oct 25, 2024
1 parent a9b6716 commit ee752ff
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 37 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 7.14.0 (2024-10-16)
* 对象存储,持久化处理支持工作流模版

## 7.13.0 (2024-09-05)
* 对象存储,验证回调方法新增支持 Qiniu 签名
* 对象存储,调整查询空间区域域名顺序与默认空间管理域名
Expand Down
3 changes: 2 additions & 1 deletion src/Qiniu/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
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.13.0';
const SDK_VER = '7.14.0';

const BLOCK_SIZE = 4194304; //4*1024*1024 分块上传块大小,该参数为接口规格,不能修改

Expand Down
14 changes: 11 additions & 3 deletions src/Qiniu/Processing/PersistentFop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 0 additions & 2 deletions src/Qiniu/Storage/ArgusManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
170 changes: 140 additions & 30 deletions tests/Qiniu/Tests/PfopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,7 +73,7 @@ public function testPfopExecuteAndStatusWithMultipleFops()
$this->assertNull($error);
}

private function pfopTypeTestData()
private function pfopOptionsTestData()
{
return array(
array(
Expand All @@ -69,66 +90,147 @@ 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,
$fops,
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);
}
}
}

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']);
}

Expand All @@ -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'];
Expand All @@ -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';
Expand Down

0 comments on commit ee752ff

Please sign in to comment.