Yun.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | 处理云端图片的一些常用方法
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace Upload\Src;
  8. use Dever;
  9. class Yun
  10. {
  11. # 查看视频截图
  12. public function cover($key, $video, $num = 1, $local = 2)
  13. {
  14. $domain = parse_url($video);
  15. $host = $domain['scheme'] . '://' . $domain['host'] . '/';
  16. $config = Dever::db('upload/yun')->one(array('host' => $host));
  17. if ($config) {
  18. return Dever::load('upload/lib/yun')->getMethod($config['type'])->cover($key, $video, $num, $local);
  19. }
  20. return false;
  21. }
  22. # 获取token
  23. public function token()
  24. {
  25. $return = Dever::input('return', 2);
  26. $key = Dever::input('key');
  27. $upload = Dever::db('upload/upload')->one($key);
  28. if (!$upload) {
  29. return;
  30. }
  31. if ($upload['save_type'] == 1) {
  32. return;
  33. }
  34. if (!$upload['yun']) {
  35. return;
  36. }
  37. $config = Dever::db('upload/yun')->one($upload['yun']);
  38. if (!$config) {
  39. return;
  40. }
  41. # 是否覆盖原文件,1是覆盖,2是不覆盖生成新文件
  42. $cover = 1;
  43. if ($upload['cover'] > 1) {
  44. $cover = 2;
  45. }
  46. list($type, $token, $domain, $bucket) = Dever::load('upload/lib/yun')->getMethod($config['type'])->token($config, $upload);
  47. $result = array('type' => $type, 'uptoken' => $token, 'domain' => $domain, 'host' => $config['host'], 'bucket' => $bucket, 'cover' => $cover);
  48. if ($return == 1) {
  49. return $result;
  50. }
  51. echo json_encode($result);die;
  52. }
  53. # 获取回调
  54. public function callback()
  55. {
  56. $config = Dever::input('config');
  57. if (!$config) {
  58. return 'error';
  59. }
  60. $config = Dever::db('upload/yun')->one($config);
  61. if (!$config) {
  62. return 'error';
  63. }
  64. $body = Dever::load('upload/lib/yun')->getMethod($config['type'])->callback();
  65. $file = $body['filename'];
  66. $key = $body['key'];
  67. $size = $body['filesize'];
  68. $width = $body['width'];
  69. $height = $body['height'];
  70. $upload = Dever::db('upload/upload')->one($key);
  71. if ($upload) {
  72. $this->initFile($key, $file, $file, $width, $height, $size);
  73. }
  74. return 'ok';
  75. }
  76. # 初始化
  77. private function initFile($key, $file, $source, $width = false, $height = false, $size = false, $search = '')
  78. {
  79. $temp = explode('/', $file);
  80. $name = $temp[count($temp) - 1];
  81. $temp = explode('.', $name);
  82. $name = $temp[0];
  83. $ext = $temp[count($temp) - 1];
  84. $info = Dever::load('upload/file-name', array('where_name' => $name, 'where_upload' => $key));
  85. $data['name'] = $name;
  86. $data['file'] = $file;
  87. $data['key'] = md5($file);
  88. $data['ext'] = '.' . $ext;
  89. if ($width) {
  90. $data['width'] = $width;
  91. }
  92. if ($height) {
  93. $data['height'] = $height;
  94. }
  95. if ($size) {
  96. $data['size'] = $size;
  97. }
  98. $data['upload'] = $key;
  99. if ($search) {
  100. $data['search'] = $search;
  101. }
  102. if ($info) {
  103. $data['where_id'] = $info['id'];
  104. Dever::db('upload/file')->update($data);
  105. } else {
  106. $data['source_name'] = $source;
  107. Dever::db('upload/file')->insert($data);
  108. }
  109. }
  110. # 上传文件之后同步到本地数据库
  111. public function addFile()
  112. {
  113. $file = Dever::input('file');
  114. $key = Dever::input('key');
  115. $source = Dever::input('source');
  116. $search = Dever::input('search');
  117. $upload = Dever::db('upload/upload')->one($key);
  118. if ($upload) {
  119. if ($upload['yun'] > 0 && $upload['vod_convert'] == 2) {
  120. $config = Dever::db('upload/yun')->one($upload['yun']);
  121. if ($config) {
  122. Dever::load('upload/lib/yun')->getMethod($config['type'])->convert($key, $file, $config, $upload);
  123. }
  124. }
  125. $this->initFile($key, $file, $source, false, false, false, $search);
  126. }
  127. }
  128. }