Qiniu.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Upload\Lib\View;
  3. use Dever;
  4. Dever::apply('sdk/qiniu', 'upload');
  5. class Qiniu
  6. {
  7. private $client;
  8. private $token;
  9. private $auth;
  10. private $bucket;
  11. public function token($config, $upload, $file = null)
  12. {
  13. $cur = time();
  14. //if (!$config['token'] || ($config['token_endtime'] && $cur > $config['token_endtime'])) {
  15. $this->auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
  16. if (true) {
  17. # 暂时没用到callback
  18. $policy = array(
  19. 'callbackUrl' => Dever::url('yun.callback?config=' . $config['id'], 'upload'),
  20. 'callbackBody' => 'filename=$(fname)&key=$(key)&filesize=$(fsize)&width=$(imageInfo.width)&height=$(imageInfo.height)'
  21. );
  22. $token = $this->auth->uploadToken($upload['bucket'], $file, 3600);
  23. $up['token'] = $token;
  24. $up['token_endtime'] = $cur + 3600 - 60;
  25. $up['where_id'] = $config['id'];
  26. Dever::db('upload/yun')->update($up);
  27. } else {
  28. $token = $config['token'];
  29. }
  30. $domain = 'http://up-z1.qiniup.com/';
  31. return array('qiniu', $token, $domain, $upload['bucket']);
  32. }
  33. public function callback()
  34. {
  35. $body = file_get_contents('php://input');
  36. Dever::log($body, 'qiniu_callback');
  37. $body = json_decode($body, true);
  38. return $body;
  39. }
  40. # 连接
  41. public function connect($config, $upload, $file = null)
  42. {
  43. list($type, $token, $domain, $bucket) = $this->token($config, $upload, $file);
  44. $this->token = $token;
  45. $this->bucket = $bucket;
  46. return $this;
  47. }
  48. # 删除文件
  49. public function delete($file, $bucket = false)
  50. {
  51. $bucket = $bucket ? $bucket : $this->bucket;
  52. $this->client = new \Qiniu\Storage\BucketManager($this->auth);
  53. $result = $this->client->delete($bucket, $file);
  54. return $result;
  55. }
  56. # 下载文件
  57. public function download($bucket, $file, $local = false)
  58. {
  59. $this->client = new \Qiniu\Storage\UploadManager();
  60. if ($local) {
  61. $options = array(
  62. OssClient::OSS_FILE_DOWNLOAD => $local
  63. );
  64. } else {
  65. $options = array();
  66. }
  67. $content = $this->client->getObject($bucket, $file, $options);
  68. return $content;
  69. }
  70. # 上传文件
  71. public function upload($file, $source_file, $options = array(), $base64 = false)
  72. {
  73. if ($base64) {
  74. $method = 'put';
  75. $this->client = new \Qiniu\Storage\UploadManager();
  76. list($ret, $err) = $this->client->$method($this->token, $file, $source_file, $options);
  77. } elseif (strstr($source_file, 'http')) {
  78. $method = 'fetch';
  79. $this->client = new \Qiniu\Storage\BucketManager($this->auth);
  80. $items = $this->client->$method($source_file, $this->bucket, $file);
  81. if (isset($items[0])) {
  82. $ret = $items[0];
  83. }
  84. } else {
  85. $method = 'putFile';
  86. $this->client = new \Qiniu\Storage\UploadManager();
  87. list($ret, $err) = $this->client->$method($this->token, $file, $source_file, $options);
  88. }
  89. return $ret;
  90. }
  91. # 视频转码
  92. public function convert($key, $file, $config, $upload)
  93. {
  94. $accessKey = $config['appkey'];
  95. $secretKey = $config['appsecret'];
  96. $host = $yun['host'];
  97. $auth = new \Qiniu\Auth($accessKey, $secretKey);
  98. $bucket = $upload['bucket'];
  99. # 转码
  100. //转码是使用的队列名称。 https://portal.qiniu.com/mps/pipeline
  101. $pipeline = $upload['pipeline'];
  102. if ($pipeline) {
  103. $array = explode(',', $pipeline);
  104. $index = array_rand($array);
  105. if (isset($array[$index])) {
  106. $pipeline = $array[$index];
  107. }
  108. } else {
  109. $pipeline = 'convert';
  110. }
  111. $ext = 'mp4';
  112. if (strstr($file, '.mp3') || strstr($file, '.wma') || strstr($file, '.wav')) {
  113. $ext = 'mp3';
  114. }
  115. $force = false;
  116. //转码完成后通知到你的业务服务器。
  117. $notifyUrl = 'http://375dec79.ngrok.com/notify.php';
  118. $notifyUrl = false;
  119. $config = new \Qiniu\Config();
  120. //$config->useHTTPS=true;
  121. $pfop = new \Qiniu\Processing\PersistentFop($auth, $config);
  122. //要进行转码的转码操作。 http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html
  123. //$fops = "avthumb/m3u8/s/640x360/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ":qiniu_640x360.mp4");
  124. $fops = "avthumb/mp4/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ':' . $file . '_c.' . $ext);
  125. list($id, $err) = $pfop->execute($bucket, $file, $fops, $pipeline, $notifyUrl, $force);
  126. /*
  127. echo "\n====> pfop avthumb result: \n";
  128. if ($err != null) {
  129. var_dump($err);
  130. } else {
  131. echo "PersistentFop Id: $id\n";
  132. }
  133. die;
  134. //查询转码的进度和状态
  135. list($ret, $err) = $pfop->status($id);
  136. echo "\n====> pfop avthumb status: \n";
  137. if ($err != null) {
  138. var_dump($err);
  139. } else {
  140. var_dump($ret);
  141. }*/
  142. }
  143. # 视频截图
  144. public function cover($key, $video, $num = 1, $local = 2)
  145. {
  146. $file = $video . '?vframe/jpg/offset/' . $num;
  147. if ($local == 1) {
  148. $data = Dever::load('upload/save')->copy($file, 7, false, false, 'jpg');
  149. return $data['url'] . '?time=' . time();
  150. } else {
  151. return $file;
  152. }
  153. //vframe/jpg/offset/7/w/480/h/360
  154. }
  155. }