PersistentFop.php 3.1 KB

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