PersistentFop.php 2.9 KB

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