| 1 | <?php |
||
| 8 | final class Operation |
||
| 9 | { |
||
| 10 | |||
| 11 | private $auth; |
||
| 12 | private $token_expire; |
||
| 13 | private $domain; |
||
| 14 | |||
| 15 | 9 | public function __construct($domain, $auth = null, $token_expire = 3600) |
|
| 16 | { |
||
| 17 | 9 | $this->auth = $auth; |
|
| 18 | 9 | $this->domain = $domain; |
|
| 19 | 9 | $this->token_expire = $token_expire; |
|
| 20 | 9 | } |
|
| 21 | |||
| 22 | |||
| 23 | /** |
||
| 24 | * 对资源文件进行处理 |
||
| 25 | * |
||
| 26 | * @param $key 待处理的资源文件名 |
||
| 27 | * @param $fops string|array fop操作,多次fop操作以array的形式传入。 |
||
| 28 | * eg. imageView2/1/w/200/h/200, imageMogr2/thumbnail/!75px |
||
| 29 | * |
||
| 30 | * @return array 文件处理后的结果及错误。 |
||
| 31 | * |
||
| 32 | * @link http://developer.qiniu.com/docs/v6/api/reference/fop/ |
||
| 33 | */ |
||
| 34 | 6 | public function execute($key, $fops) |
|
| 35 | { |
||
| 36 | 6 | $url = $this->buildUrl($key, $fops); |
|
| 37 | 6 | $resp = Client::get($url); |
|
| 38 | 6 | if (!$resp->ok()) { |
|
| 39 | 3 | return array(null, new Error($url, $resp)); |
|
| 40 | } |
||
| 41 | 3 | if ($resp->json() !== null) { |
|
| 42 | 3 | return array($resp->json(), null); |
|
| 43 | } |
||
| 44 | return array($resp->body, null); |
||
| 45 | } |
||
| 46 | |||
| 47 | 9 | public function buildUrl($key, $fops,$protocol = 'http') |
|
|
|
|||
| 48 | { |
||
| 49 | 9 | if (is_array($fops)) { |
|
| 50 | 3 | $fops = implode('|', $fops); |
|
| 51 | 2 | } |
|
| 52 | |||
| 53 | 9 | $url = $protocol."://$this->domain/$key?$fops"; |
|
| 54 | 9 | if ($this->auth !== null) { |
|
| 55 | 3 | $url = $this->auth->privateDownloadUrl($url, $this->token_expire); |
|
| 56 | 2 | } |
|
| 57 | |||
| 58 | 9 | return $url; |
|
| 59 | } |
||
| 60 | } |
||
| 61 |