FileService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace service;
  14. use Exception;
  15. use Qiniu\Auth;
  16. use Qiniu\Storage\BucketManager;
  17. use Qiniu\Storage\UploadManager;
  18. use think\Config;
  19. use think\Log;
  20. /**
  21. * 文件服务处理
  22. * @class FileService
  23. * @author Anyon <zoujingli@qq.com>
  24. * @date 2017/03/15 15:17
  25. */
  26. class FileService {
  27. /**
  28. * 获取文件MINE信息
  29. * @param string $exts
  30. * @return string
  31. */
  32. static public function getFileMine($exts) {
  33. $_exts = is_string($exts) ? explode(',', $exts) : $exts;
  34. $_mines = [];
  35. $mines = Config::get('mines');
  36. foreach ($_exts as $_e) {
  37. if (isset($mines[strtolower($_e)])) {
  38. $_exinfo = $mines[strtolower($_e)];
  39. $_mines[] = is_array($_exinfo) ? join(',', $_exinfo) : $_exinfo;
  40. }
  41. }
  42. return join(',', $_mines);
  43. }
  44. /**
  45. * 获取文件当前URL地址
  46. * @param string $filename
  47. * @param string|null $storage
  48. * @return bool|string
  49. */
  50. static public function getFileUrl($filename, $storage = null) {
  51. if (self::hasFile($filename, $storage) === false) {
  52. return false;
  53. }
  54. switch (empty($storage) ? sysconf('storage_type') : $storage) {
  55. case 'local':
  56. return self::getBaseUriLocal() . $filename;
  57. case 'qiniu':
  58. return self::getBaseUriQiniu() . $filename;
  59. }
  60. return false;
  61. }
  62. /**
  63. * 获取服务器URL前缀
  64. * @return string
  65. */
  66. static public function getBaseUriLocal() {
  67. $request = request();
  68. $base = $request->root();
  69. $root = strpos($base, '.') ? ltrim(dirname($base), DS) : $base;
  70. if ('' != $root) {
  71. $root = '/' . ltrim($root, '/');
  72. }
  73. return ($request->isSsl() ? 'https' : 'http') . '://' . $request->host() . "{$root}/upload/";
  74. }
  75. /**
  76. * 获取七牛云URL前缀
  77. * @return string
  78. */
  79. static public function getBaseUriQiniu() {
  80. return (sysconf('storage_qiniu_is_https') ? 'https' : 'http') . '://' . sysconf('storage_qiniu_domain') . '/';
  81. }
  82. /**
  83. * 检查文件是否已经存在
  84. * @param string $filename
  85. * @param string|null $storage
  86. * @return bool
  87. */
  88. static public function hasFile($filename, $storage = null) {
  89. switch (empty($storage) ? sysconf('storage_type') : $storage) {
  90. case 'local':
  91. return file_exists(ROOT_PATH . 'public/upload/' . $filename);
  92. case 'qiniu':
  93. $auth = new Auth(sysconf('storage_qiniu_access_key'), sysconf('storage_qiniu_secret_key'));
  94. $bucketMgr = new BucketManager($auth);
  95. list($ret, $err) = $bucketMgr->stat(sysconf('storage_qiniu_bucket'), $filename);
  96. return $err === null;
  97. }
  98. return false;
  99. }
  100. /**
  101. * 根据Key读取文件内容
  102. * @param string $filename
  103. * @param string|null $storage
  104. * @return string|null
  105. */
  106. static public function readFile($filename, $storage = null) {
  107. switch (empty($storage) ? sysconf('storage_type') : $storage) {
  108. case 'local':
  109. if (file_exists(ROOT_PATH . 'public/upload/' . $filename)) {
  110. return file_get_contents(ROOT_PATH . 'public/upload/' . $filename);
  111. }
  112. case 'qiniu':
  113. $auth = new Auth(sysconf('storage_qiniu_access_key'), sysconf('storage_qiniu_secret_key'));
  114. return file_get_contents($auth->privateDownloadUrl(self::getBaseUriQiniu() . $filename));
  115. }
  116. return null;
  117. }
  118. /**
  119. * 根据当前配置存储文件
  120. * @param string $filename
  121. * @param string $bodycontent
  122. * @param string|null $file_storage
  123. * @return array|null
  124. */
  125. static public function save($filename, $bodycontent, $file_storage = null) {
  126. $type = empty($file_storage) ? sysconf('storage_type') : $file_storage;
  127. if (!method_exists(__CLASS__, $type)) {
  128. Log::error("保存存储失败,调用{$type}存储引擎不存在!");
  129. return null;
  130. }
  131. return self::$type($filename, $bodycontent);
  132. }
  133. /**
  134. * 文件储存在本地
  135. * @param string $filename
  136. * @param string $bodycontent
  137. * @return string
  138. */
  139. static public function local($filename, $bodycontent) {
  140. $filepath = ROOT_PATH . 'public/upload/' . $filename;
  141. try {
  142. !is_dir(dirname($filepath)) && mkdir(dirname($filepath), '0755', true);
  143. if (file_put_contents($filepath, $bodycontent)) {
  144. return [
  145. 'hash' => md5_file($filepath),
  146. 'key' => "upload/{$filename}",
  147. 'url' => pathinfo(request()->baseFile(true), PATHINFO_DIRNAME) . '/upload/' . $filename
  148. ];
  149. }
  150. } catch (Exception $err) {
  151. Log::error('本地文件存储失败, ' . var_export($err, true));
  152. }
  153. return null;
  154. }
  155. /**
  156. * 七牛云存储
  157. * @param string $filename
  158. * @param string $bodycontent
  159. * @return string
  160. */
  161. static public function qiniu($filename, $bodycontent) {
  162. $auth = new Auth(sysconf('storage_qiniu_access_key'), sysconf('storage_qiniu_secret_key'));
  163. $token = $auth->uploadToken(sysconf('storage_qiniu_bucket'));
  164. $uploadMgr = new UploadManager();
  165. list($result, $err) = $uploadMgr->put($token, $filename, $bodycontent);
  166. if ($err !== null) {
  167. Log::error('七牛云文件上传失败, ' . var_export($err, true));
  168. return null;
  169. }
  170. $result['url'] = self::getBaseUriQiniu() . $filename;
  171. return $result;
  172. }
  173. }