PersistentFop.php 2.9 KB

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