Skip to content

Commit e638410

Browse files
committed
feat: add idle-time pfop support and pfop configure api host
1 parent 29f746c commit e638410

File tree

4 files changed

+113
-12
lines changed

4 files changed

+113
-12
lines changed

src/Qiniu/Auth.php

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ public function uploadToken($bucket, $key = null, $expires = 3600, $policy = nul
222222
'persistentOps',
223223
'persistentNotifyUrl',
224224
'persistentPipeline',
225+
'persistentType', // 为 `1` 时开启闲时任务
225226

226227
'deleteAfterDays',
227228
'fileType',

src/Qiniu/Config.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
final class Config
66
{
7-
const SDK_VER = '7.12.1';
7+
const SDK_VER = '7.13.0';
88

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

src/Qiniu/Processing/PersistentFop.php

+32-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Qiniu\Http\Error;
77
use Qiniu\Http\Client;
88
use Qiniu\Http\Proxy;
9+
use Qiniu\Zone;
910

1011
/**
1112
* 持久化处理类,该类用于主动触发异步持久化操作.
@@ -45,25 +46,34 @@ public function __construct($auth, $config = null, $proxy = null, $proxy_auth =
4546
* 对资源文件进行异步持久化处理
4647
* @param string $bucket 资源所在空间
4748
* @param string $key 待处理的源文件
48-
* @param string $fops string|array 待处理的pfop操作,多个pfop操作以array的形式传入。
49+
* @param string|array $fops 待处理的pfop操作,多个pfop操作以array的形式传入。
4950
* eg. avthumb/mp3/ab/192k, vframe/jpg/offset/7/w/480/h/360
5051
* @param string $pipeline 资源处理队列
5152
* @param string $notify_url 处理结果通知地址
5253
* @param bool $force 是否强制执行一次新的指令
54+
* @param int $type 为 `1` 时开启闲时任务
5355
*
5456
*
55-
* @return array 返回持久化处理的persistentId, 和返回的错误
57+
* @return array 返回持久化处理的 persistentId 与可能出现的错误
5658
*
5759
* @link http://developer.qiniu.com/docs/v6/api/reference/fop/
5860
*/
59-
public function execute($bucket, $key, $fops, $pipeline = null, $notify_url = null, $force = false)
60-
{
61+
public function execute(
62+
$bucket,
63+
$key,
64+
$fops,
65+
$pipeline = null,
66+
$notify_url = null,
67+
$force = false,
68+
$type = null
69+
) {
6170
if (is_array($fops)) {
6271
$fops = implode(';', $fops);
6372
}
6473
$params = array('bucket' => $bucket, 'key' => $key, 'fops' => $fops);
6574
\Qiniu\setWithoutEmpty($params, 'pipeline', $pipeline);
6675
\Qiniu\setWithoutEmpty($params, 'notifyURL', $notify_url);
76+
\Qiniu\setWithoutEmpty($params, 'type', $type);
6777
if ($force) {
6878
$params['force'] = 1;
6979
}
@@ -72,7 +82,8 @@ public function execute($bucket, $key, $fops, $pipeline = null, $notify_url = nu
7282
if ($this->config->useHTTPS === true) {
7383
$scheme = "https://";
7484
}
75-
$url = $scheme . Config::API_HOST . '/pfop/';
85+
$apiHost = $this->getApiHost();
86+
$url = $scheme . $apiHost . '/pfop/';
7687
$headers = $this->auth->authorization($url, $data, 'application/x-www-form-urlencoded');
7788
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
7889
$response = Client::post($url, $data, $headers, $this->proxy->makeReqOpt());
@@ -84,18 +95,33 @@ public function execute($bucket, $key, $fops, $pipeline = null, $notify_url = nu
8495
return array($id, null);
8596
}
8697

98+
/**
99+
* @param string $id
100+
* @return array 返回任务状态与可能出现的错误
101+
*/
87102
public function status($id)
88103
{
89104
$scheme = "http://";
90105

91106
if ($this->config->useHTTPS === true) {
92107
$scheme = "https://";
93108
}
94-
$url = $scheme . Config::API_HOST . "/status/get/prefop?id=$id";
109+
$apiHost = $this->getApiHost();
110+
$url = $scheme . $apiHost . "/status/get/prefop?id=$id";
95111
$response = Client::get($url, array(), $this->proxy->makeReqOpt());
96112
if (!$response->ok()) {
97113
return array(null, new Error($url, $response));
98114
}
99115
return array($response->json(), null);
100116
}
117+
118+
private function getApiHost()
119+
{
120+
if (!empty($this->config->zone) && !empty($this->config->zone->apiHost)) {
121+
$apiHost = $this->config->zone->apiHost;
122+
} else {
123+
$apiHost = Config::API_HOST;
124+
}
125+
return $apiHost;
126+
}
101127
}

tests/Qiniu/Tests/PfopTest.php

+79-5
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33

44
use PHPUnit\Framework\TestCase;
55

6-
use Qiniu\Processing\Operation;
76
use Qiniu\Processing\PersistentFop;
7+
use Qiniu\Storage\UploadManager;
8+
use Qiniu\Region;
9+
use Qiniu\Config;
810

911
class PfopTest extends TestCase
1012
{
11-
public function testPfop()
13+
private static function getConfig()
14+
{
15+
// use this func to test in test env
16+
// `null` means to use production env
17+
return null;
18+
}
19+
20+
public function testPfopExecuteAndStatusWithSingleFop()
1221
{
1322
global $testAuth;
1423
$bucket = 'testres';
1524
$key = 'sintel_trailer.mp4';
16-
$pfop = new PersistentFop($testAuth, null);
25+
$pfop = new PersistentFop($testAuth, self::getConfig());
1726

1827
$fops = 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240';
1928
list($id, $error) = $pfop->execute($bucket, $key, $fops);
@@ -24,7 +33,7 @@ public function testPfop()
2433
}
2534

2635

27-
public function testPfops()
36+
public function testPfopExecuteAndStatusWithMultipleFops()
2837
{
2938
global $testAuth;
3039
$bucket = 'testres';
@@ -33,7 +42,7 @@ public function testPfops()
3342
'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240',
3443
'vframe/jpg/offset/7/w/480/h/360',
3544
);
36-
$pfop = new PersistentFop($testAuth, null);
45+
$pfop = new PersistentFop($testAuth, self::getConfig());
3746

3847
list($id, $error) = $pfop->execute($bucket, $key, $fops);
3948
$this->assertNull($error);
@@ -43,6 +52,71 @@ public function testPfops()
4352
$this->assertNull($error);
4453
}
4554

55+
public function testPfopWithIdleTimeType()
56+
{
57+
global $testAuth;
58+
59+
$bucket = 'testres';
60+
$key = 'sintel_trailer.mp4';
61+
$persistentEntry = \Qiniu\entry($bucket, 'test-pfop-type_1');
62+
$fops = 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240|saveas/' . $persistentEntry;
63+
$pfop = new PersistentFop($testAuth, self::getConfig());
64+
65+
list($id, $error) = $pfop->execute(
66+
$bucket,
67+
$key,
68+
$fops,
69+
null,
70+
null,
71+
false,
72+
1
73+
);
74+
$this->assertNull($error);
75+
list($status, $error) = $pfop->status($id);
76+
$this->assertNotNull($status);
77+
$this->assertNull($error);
78+
$this->assertEquals(1, $status['type']);
79+
$this->assertNotEmpty($status['creationDate']);
80+
}
81+
82+
public function testPfopByUploadPolicy()
83+
{
84+
global $testAuth;
85+
$bucket = 'testres';
86+
$key = 'sintel_trailer.mp4';
87+
$persistentEntry = \Qiniu\entry($bucket, 'test-pfop-type_1');
88+
$fops = 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240|saveas/' . $persistentEntry;
89+
90+
$token = $testAuth->uploadToken(
91+
$bucket,
92+
$key,
93+
3600,
94+
array(
95+
'persistentOps' => $fops,
96+
'persistentType' => 1
97+
)
98+
);
99+
$upManager = new UploadManager(self::getConfig());
100+
list($ret, $error) = $upManager->putFile(
101+
$token,
102+
$key,
103+
__file__,
104+
null,
105+
'text/plain',
106+
true
107+
);
108+
$this->assertNull($error);
109+
$this->assertNotEmpty($ret['persistentId']);
110+
$id = $ret['persistentId'];
111+
112+
$pfop = new PersistentFop($testAuth, self::getConfig());
113+
list($status, $error) = $pfop->status($id);
114+
$this->assertNotNull($status);
115+
$this->assertNull($error);
116+
$this->assertEquals(1, $status['type']);
117+
$this->assertNotEmpty($status['creationDate']);
118+
}
119+
46120
public function testMkzip()
47121
{
48122
global $testAuth;

0 commit comments

Comments
 (0)