Issues (76)

src/Qiniu/Processing/PersistentFop.php (3 issues)

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
10
/**
11
 * 持久化处理类,该类用于主动触发异步持久化操作.
12
 *
13
 * @link http://developer.qiniu.com/docs/v6/api/reference/fop/pfop/pfop.html
14
 */
15
final class PersistentFop
16
{
17
    /**
18
     * @var 账号管理密钥对,Auth对象
0 ignored issues
show
Documentation Bug introduced by
The doc comment 账号管理密钥对,Auth对象 at position 0 could not be parsed: Unknown type name '账号管理密钥对,Auth对象' at position 0 in 账号管理密钥对,Auth对象.
Loading history...
19
     */
20
    private $auth;
21
22
    /*
23
     * @var 配置对象,Config 对象
0 ignored issues
show
Documentation Bug introduced by
The doc comment 配置对象,Config at position 0 could not be parsed: Unknown type name '配置对象,Config' at position 0 in 配置对象,Config.
Loading history...
24
     * */
25
    private $config;
26
27 9
    /**
28
     * @var 代理信息
0 ignored issues
show
Documentation Bug introduced by
The doc comment 代理信息 at position 0 could not be parsed: Unknown type name '代理信息' at position 0 in 代理信息.
Loading history...
29 9
     */
30 9
    private $proxy;
31 9
32 9
33
    public function __construct($auth, $config = null, $proxy = null, $proxy_auth = null, $proxy_user_password = null)
34
    {
35 9
        $this->auth = $auth;
36
        if ($config == null) {
37
            $this->config = new Config();
38
        } else {
39
            $this->config = $config;
40
        }
41
        $this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password);
42
    }
43
44
    /**
45
     * 对资源文件进行异步持久化处理
46
     * @param string $bucket 资源所在空间
47
     * @param string $key 待处理的源文件
48
     * @param string $fops string|array  待处理的pfop操作,多个pfop操作以array的形式传入。
49
     *                    eg. avthumb/mp3/ab/192k, vframe/jpg/offset/7/w/480/h/360
50
     * @param string $pipeline 资源处理队列
51
     * @param string $notify_url 处理结果通知地址
52 9
     * @param bool $force 是否强制执行一次新的指令
53
     *
54 9
     *
55 3
     * @return array 返回持久化处理的persistentId, 和返回的错误。
56 3
     *
57 9
     * @link http://developer.qiniu.com/docs/v6/api/reference/fop/
58 9
     */
59 9
    public function execute($bucket, $key, $fops, $pipeline = null, $notify_url = null, $force = false)
60 9
    {
61
        if (is_array($fops)) {
62
            $fops = implode(';', $fops);
63 9
        }
64 9
        $params = array('bucket' => $bucket, 'key' => $key, 'fops' => $fops);
65 9
        \Qiniu\setWithoutEmpty($params, 'pipeline', $pipeline);
66
        \Qiniu\setWithoutEmpty($params, 'notifyURL', $notify_url);
67
        if ($force) {
68 9
            $params['force'] = 1;
69 9
        }
70 9
        $data = http_build_query($params);
71 9
        $scheme = "http://";
72 9
        if ($this->config->useHTTPS === true) {
73
            $scheme = "https://";
74
        }
75 9
        $url = $scheme . Config::API_HOST . '/pfop/';
76 9
        $headers = $this->auth->authorization($url, $data, 'application/x-www-form-urlencoded');
77 9
        $headers['Content-Type'] = 'application/x-www-form-urlencoded';
78
        $response = Client::post($url, $data, $headers, $this->proxy->makeReqOpt());
79
        if (!$response->ok()) {
80 9
            return array(null, new Error($url, $response));
81
        }
82 9
        $r = $response->json();
83
        $id = $r['persistentId'];
84 9
        return array($id, null);
85
    }
86
87 9
    public function status($id)
88 9
    {
89 9
        $scheme = "http://";
90
91
        if ($this->config->useHTTPS === true) {
92 9
            $scheme = "https://";
93
        }
94
        $url = $scheme . Config::API_HOST . "/status/get/prefop?id=$id";
95
        $response = Client::get($url, array(), $this->proxy->makeReqOpt());
96
        if (!$response->ok()) {
97
            return array(null, new Error($url, $response));
98
        }
99
        return array($response->json(), null);
100
    }
101
}
102