Qiniu.php 6.0 KB

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