Operation.php 1.5 KB

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