-
Notifications
You must be signed in to change notification settings - Fork 424
/
Copy pathPersistentFop.php
135 lines (122 loc) · 4.08 KB
/
PersistentFop.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace Qiniu\Processing;
use Qiniu\Config;
use Qiniu\Http\Error;
use Qiniu\Http\Client;
use Qiniu\Http\Proxy;
use Qiniu\Zone;
/**
* 持久化处理类,该类用于主动触发异步持久化操作.
*
* @link http://developer.qiniu.com/docs/v6/api/reference/fop/pfop/pfop.html
*/
final class PersistentFop
{
/**
* @var 账号管理密钥对,Auth对象
*/
private $auth;
/*
* @var 配置对象,Config 对象
* */
private $config;
/**
* @var 代理信息
*/
private $proxy;
public function __construct($auth, $config = null, $proxy = null, $proxy_auth = null, $proxy_user_password = null)
{
$this->auth = $auth;
if ($config == null) {
$this->config = new Config();
} else {
$this->config = $config;
}
$this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password);
}
/**
* 对资源文件进行异步持久化处理
* @param string $bucket 资源所在空间
* @param string $key 待处理的源文件
* @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 与可能出现的错误。
*
* @link http://developer.qiniu.com/docs/v6/api/reference/fop/
*/
public function execute(
$bucket,
$key,
$fops = null,
$pipeline = null,
$notify_url = null,
$force = false,
$type = null,
$workflow_template_id = null
) {
if (is_array($fops)) {
$fops = implode(';', $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;
}
$data = http_build_query($params);
$scheme = "http://";
if ($this->config->useHTTPS === true) {
$scheme = "https://";
}
$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());
if (!$response->ok()) {
return array(null, new Error($url, $response));
}
$r = $response->json();
$id = $r['persistentId'];
return array($id, null);
}
/**
* @param string $id
* @return array 返回任务状态与可能出现的错误
*/
public function status($id)
{
$scheme = "http://";
if ($this->config->useHTTPS === true) {
$scheme = "https://";
}
$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;
}
}