Operation.php 1.8 KB

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