1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Qiniu\Processing; |
4
|
|
|
|
5
|
|
|
use Qiniu\Config; |
6
|
|
|
use Qiniu\Http\Error; |
7
|
|
|
use Qiniu\Http\Client; |
8
|
|
|
use Qiniu\Http\Proxy; |
9
|
|
|
use Qiniu\Zone; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* 持久化处理类,该类用于主动触发异步持久化操作. |
13
|
|
|
* |
14
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/fop/pfop/pfop.html |
15
|
|
|
*/ |
16
|
|
|
final class PersistentFop |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var 账号管理密钥对,Auth对象 |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
private $auth; |
22
|
|
|
|
23
|
|
|
/* |
24
|
|
|
* @var 配置对象,Config 对象 |
|
|
|
|
25
|
|
|
* */ |
26
|
|
|
private $config; |
27
|
9 |
|
|
28
|
|
|
/** |
29
|
9 |
|
* @var 代理信息 |
|
|
|
|
30
|
9 |
|
*/ |
31
|
9 |
|
private $proxy; |
32
|
9 |
|
|
33
|
|
|
|
34
|
|
|
public function __construct($auth, $config = null, $proxy = null, $proxy_auth = null, $proxy_user_password = null) |
35
|
9 |
|
{ |
36
|
|
|
$this->auth = $auth; |
37
|
|
|
if ($config == null) { |
38
|
|
|
$this->config = new Config(); |
39
|
|
|
} else { |
40
|
|
|
$this->config = $config; |
41
|
|
|
} |
42
|
|
|
$this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* 对资源文件进行异步持久化处理 |
47
|
|
|
* @param string $bucket 资源所在空间 |
48
|
|
|
* @param string $key 待处理的源文件 |
49
|
|
|
* @param string|array $fops 待处理的pfop操作,多个pfop操作以array的形式传入。 |
50
|
|
|
* eg. avthumb/mp3/ab/192k, vframe/jpg/offset/7/w/480/h/360 |
51
|
|
|
* @param string $pipeline 资源处理队列 |
52
|
9 |
|
* @param string $notify_url 处理结果通知地址 |
53
|
|
|
* @param bool $force 是否强制执行一次新的指令 |
54
|
9 |
|
* @param int $type 为 `1` 时开启闲时任务 |
55
|
3 |
|
* |
56
|
3 |
|
* |
57
|
9 |
|
* @return array 返回持久化处理的 persistentId 与可能出现的错误。 |
58
|
9 |
|
* |
59
|
9 |
|
* @link http://developer.qiniu.com/docs/v6/api/reference/fop/ |
60
|
9 |
|
*/ |
61
|
|
|
public function execute( |
62
|
|
|
$bucket, |
63
|
9 |
|
$key, |
64
|
9 |
|
$fops = null, |
65
|
9 |
|
$pipeline = null, |
66
|
|
|
$notify_url = null, |
67
|
|
|
$force = false, |
68
|
9 |
|
$type = null, |
69
|
9 |
|
$workflow_template_id = null |
70
|
9 |
|
) { |
71
|
9 |
|
if (is_array($fops)) { |
72
|
9 |
|
$fops = implode(';', $fops); |
73
|
|
|
} |
74
|
|
|
|
75
|
9 |
|
if (!$fops && !$workflow_template_id) { |
76
|
9 |
|
throw new \InvalidArgumentException('Must provide one of fops or template_id'); |
77
|
9 |
|
} |
78
|
|
|
|
79
|
|
|
$params = array('bucket' => $bucket, 'key' => $key); |
80
|
9 |
|
\Qiniu\setWithoutEmpty($params, 'fops', $fops); |
|
|
|
|
81
|
|
|
\Qiniu\setWithoutEmpty($params, 'pipeline', $pipeline); |
82
|
9 |
|
\Qiniu\setWithoutEmpty($params, 'notifyURL', $notify_url); |
83
|
|
|
\Qiniu\setWithoutEmpty($params, 'type', $type); |
84
|
9 |
|
\Qiniu\setWithoutEmpty($params, 'workflowTemplateID', $workflow_template_id); |
85
|
|
|
if ($force) { |
86
|
|
|
$params['force'] = 1; |
87
|
9 |
|
} |
88
|
9 |
|
$data = http_build_query($params); |
89
|
9 |
|
$scheme = "http://"; |
90
|
|
|
if ($this->config->useHTTPS === true) { |
91
|
|
|
$scheme = "https://"; |
92
|
9 |
|
} |
93
|
|
|
$apiHost = $this->getApiHost(); |
94
|
|
|
$url = $scheme . $apiHost . '/pfop/'; |
95
|
|
|
$headers = $this->auth->authorization($url, $data, 'application/x-www-form-urlencoded'); |
96
|
|
|
$headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
97
|
|
|
$response = Client::post($url, $data, $headers, $this->proxy->makeReqOpt()); |
98
|
|
|
if (!$response->ok()) { |
99
|
|
|
return array(null, new Error($url, $response)); |
100
|
|
|
} |
101
|
|
|
$r = $response->json(); |
102
|
|
|
$id = $r['persistentId']; |
103
|
|
|
return array($id, null); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $id |
108
|
|
|
* @return array 返回任务状态与可能出现的错误 |
109
|
|
|
*/ |
110
|
|
|
public function status($id) |
111
|
|
|
{ |
112
|
|
|
$scheme = "http://"; |
113
|
|
|
|
114
|
|
|
if ($this->config->useHTTPS === true) { |
115
|
|
|
$scheme = "https://"; |
116
|
|
|
} |
117
|
|
|
$apiHost = $this->getApiHost(); |
118
|
|
|
$url = $scheme . $apiHost . "/status/get/prefop?id=$id"; |
119
|
|
|
$response = Client::get($url, array(), $this->proxy->makeReqOpt()); |
120
|
|
|
if (!$response->ok()) { |
121
|
|
|
return array(null, new Error($url, $response)); |
122
|
|
|
} |
123
|
|
|
return array($response->json(), null); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function getApiHost() |
127
|
|
|
{ |
128
|
|
|
if (!empty($this->config->zone) && !empty($this->config->zone->apiHost)) { |
129
|
|
|
$apiHost = $this->config->zone->apiHost; |
130
|
|
|
} else { |
131
|
|
|
$apiHost = Config::API_HOST; |
132
|
|
|
} |
133
|
|
|
return $apiHost; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|