File.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace library;
  14. use Exception;
  15. use Qiniu\Auth;
  16. use Qiniu\Storage\UploadManager;
  17. use think\Log;
  18. /**
  19. * 文件处理库
  20. *
  21. * @author Anyon <zoujingli@qq.com>
  22. * @date 2016/03/19 13:40
  23. */
  24. class File {
  25. /**
  26. * 获取文件MINE信息
  27. * @param string $exts
  28. * @return string
  29. */
  30. static public function getMine($exts) {
  31. $_exts = is_string($exts) ? explode(',', $exts) : $exts;
  32. $_mines = [];
  33. $mines = require(__DIR__ . DS . 'resource' . DS . 'mines.php');
  34. foreach ($_exts as $_e) {
  35. if (isset($mines[strtolower($_e)])) {
  36. $_exinfo = $mines[strtolower($_e)];
  37. $_mines[] = is_array($_exinfo) ? join(',', $_exinfo) : $_exinfo;
  38. }
  39. }
  40. return join(',', $_mines);
  41. }
  42. /**
  43. * 存储微信上传的文件
  44. * @param string $appid 公众号APPID
  45. * @param string $medias MediaID列表
  46. * @param string $ext 文件后缀
  47. * @param string $method 获取素材接口方法(getForeverMedia|getMedia)
  48. * @param string $split 多项分割符
  49. * @param array $list 文件URL列表
  50. * @return string
  51. */
  52. static public function wechat($appid, $medias, $ext = '.png', $method = 'getMedia', $split = ',', $list = array()) {
  53. $wechat = &load_wechat('Media', $appid);
  54. if (is_string($medias)) {
  55. $medias = explode($split, $medias);
  56. }
  57. foreach ($medias as $media_id) {
  58. if (stripos($media_id, 'http') === 0) {
  59. $list[] = $media_id;
  60. continue;
  61. }
  62. $filename = 'wechat/' . join('/', str_split(md5($media_id), 16)) . $ext;
  63. $content = $wechat->$method($media_id, (strtolower($ext) === '.mp4'));
  64. if ($content === FALSE) {
  65. Log::record("获取微信素材失败,{$wechat->errMsg}" . var_export(func_get_args(), TRUE), Log::ERROR);
  66. continue;
  67. }
  68. // 视频需要处理
  69. if (strtolower($ext) === '.mp4' && is_array($content) && isset($content['down_url'])) {
  70. $content = file_get_contents($content['down_url']);
  71. // 半个小时就失效了
  72. // $list[] = $content['down_url'];
  73. // continue;
  74. }
  75. $result = self::save($filename, $content, 'qiniu');
  76. if ($result !== false && isset($result['url'])) {
  77. $list[] = $result['url'];
  78. }
  79. }
  80. return join($split, $list);
  81. }
  82. /**
  83. * 根据Key读取文件内容
  84. * @param type $filename
  85. * @param type $file_storage
  86. * @return type
  87. */
  88. static public function get($filename, $file_storage = NULL) {
  89. switch (empty($file_storage) ? sysconf('file_storage') : $file_storage) {
  90. case 'local':
  91. if (file_exists(ROOT_PATH . 'public/upload/' . $filename)) {
  92. return file_get_contents(ROOT_PATH . 'public/upload/' . $filename);
  93. }
  94. case 'qiniu':
  95. $auth = new Auth(sysconf('qiniu_accesskey'), sysconf('qiniu_secretkey'));
  96. return file_get_contents($auth->privateDownloadUrl(sysconf('qiniu_protocol') . '://' . sysconf('qiniu_domain') . '/' . $filename));
  97. }
  98. return null;
  99. }
  100. /**
  101. * 根据当前配置存储文件
  102. * @param string $filename
  103. * @param string $bodycontent
  104. * @param string|null $file_storage
  105. * @return array|null
  106. */
  107. static public function save($filename, $bodycontent, $file_storage = NULL) {
  108. $type = empty($file_storage) ? sysconf('file_storage') : $file_storage;
  109. if (!method_exists(__CLASS__, $type)) {
  110. Log::record("保存存储失败,调用{$type}存储引擎不存在!", Log::ERROR);
  111. return null;
  112. }
  113. return self::$type($filename, $bodycontent);
  114. }
  115. /**
  116. * 文件储存在本地
  117. * @param string $filename
  118. * @param string $bodycontent
  119. * @return string
  120. */
  121. static public function local($filename, $bodycontent) {
  122. $filepath = ROOT_PATH . 'public/upload/' . $filename;
  123. try {
  124. !is_dir(dirname($filepath)) && mkdir(dirname($filepath), '0755', true);
  125. if (file_put_contents($filepath, $bodycontent)) {
  126. return array(
  127. 'hash' => md5_file($filepath),
  128. 'key' => "upload/{$filename}",
  129. 'url' => pathinfo(request()->baseFile(true), PATHINFO_DIRNAME) . '/upload/' . $filename
  130. );
  131. }
  132. } catch (Exception $err) {
  133. Log::record('本地文件存储失败, ' . var_export($err, true), Log::ERROR);
  134. }
  135. return null;
  136. }
  137. /**
  138. * 七牛云存储
  139. * @param string $filename
  140. * @param string $bodycontent
  141. * @return string
  142. */
  143. static public function qiniu($filename, $bodycontent) {
  144. $auth = new Auth(sysconf('qiniu_accesskey'), sysconf('qiniu_secretkey'));
  145. $token = $auth->uploadToken(sysconf('qiniu_bucket'));
  146. $uploadMgr = new UploadManager();
  147. list($result, $err) = $uploadMgr->put($token, $filename, $bodycontent);
  148. if ($err !== null) {
  149. Log::record('七牛云文件上传失败, ' . var_export($err, true), Log::ERROR);
  150. return null;
  151. } else {
  152. $result['url'] = sysconf('qiniu_protocol') . '://' . sysconf('qiniu_domain') . '/' . $filename;
  153. return $result;
  154. }
  155. }
  156. }