Qiniu.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Upload\Lib\View;
  3. use Dever;
  4. Dever::apply('sdk/qiniu', 'upload');
  5. class Qiniu
  6. {
  7. public function token($config, $upload)
  8. {
  9. $cur = time();
  10. if (!$config['token'] || ($config['token_endtime'] && $cur > $config['token_endtime'])) {
  11. $auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
  12. # 暂时没用到callback
  13. $policy = array(
  14. 'callbackUrl' => Dever::url('yun.callback?config=' . $config['id'], 'upload'),
  15. 'callbackBody' => 'filename=$(fname)&key=$(key)&filesize=$(fsize)&width=$(imageInfo.width)&height=$(imageInfo.height)'
  16. );
  17. $token = $auth->uploadToken($upload['bucket'], null, 3600);
  18. $up['token'] = $token;
  19. $up['token_endtime'] = $time + 3600 - 60;
  20. $up['where_id'] = $config['id'];
  21. Dever::db('upload/yun')->update($up);
  22. } else {
  23. $token = $config['token'];
  24. }
  25. $domain = 'http://up-z1.qiniup.com/';
  26. return array('qiniu', $token, $domain, $upload['bucket']);
  27. }
  28. public function callback()
  29. {
  30. $body = file_get_contents('php://input');
  31. Dever::log($body, 'qiniu_callback');
  32. $body = json_decode($body, true);
  33. return $body;
  34. }
  35. # 视频转码
  36. public function convert($key, $file, $config, $upload)
  37. {
  38. $accessKey = $config['appkey'];
  39. $secretKey = $config['appsecret'];
  40. $host = $yun['host'];
  41. $auth = new \Qiniu\Auth($accessKey, $secretKey);
  42. $bucket = $upload['bucket'];
  43. # 转码
  44. //转码是使用的队列名称。 https://portal.qiniu.com/mps/pipeline
  45. $pipeline = $upload['pipeline'];
  46. if ($pipeline) {
  47. $array = explode(',', $pipeline);
  48. $index = array_rand($array);
  49. if (isset($array[$index])) {
  50. $pipeline = $array[$index];
  51. }
  52. } else {
  53. $pipeline = 'convert';
  54. }
  55. $ext = 'mp4';
  56. if (strstr($file, '.mp3') || strstr($file, '.wma') || strstr($file, '.wav')) {
  57. $ext = 'mp3';
  58. }
  59. $force = false;
  60. //转码完成后通知到你的业务服务器。
  61. $notifyUrl = 'http://375dec79.ngrok.com/notify.php';
  62. $notifyUrl = false;
  63. $config = new \Qiniu\Config();
  64. //$config->useHTTPS=true;
  65. $pfop = new \Qiniu\Processing\PersistentFop($auth, $config);
  66. //要进行转码的转码操作。 http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html
  67. //$fops = "avthumb/m3u8/s/640x360/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ":qiniu_640x360.mp4");
  68. $fops = "avthumb/mp4/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ':' . $file . '_c.' . $ext);
  69. list($id, $err) = $pfop->execute($bucket, $file, $fops, $pipeline, $notifyUrl, $force);
  70. /*
  71. echo "\n====> pfop avthumb result: \n";
  72. if ($err != null) {
  73. var_dump($err);
  74. } else {
  75. echo "PersistentFop Id: $id\n";
  76. }
  77. die;
  78. //查询转码的进度和状态
  79. list($ret, $err) = $pfop->status($id);
  80. echo "\n====> pfop avthumb status: \n";
  81. if ($err != null) {
  82. var_dump($err);
  83. } else {
  84. var_dump($ret);
  85. }*/
  86. }
  87. }