Upload.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller\api;
  15. use think\admin\Controller;
  16. use think\admin\Storage;
  17. use think\admin\storage\AliossStorage;
  18. use think\admin\storage\LocalStorage;
  19. use think\admin\storage\QiniuStorage;
  20. /**
  21. * 文件上传接口
  22. * Class Upload
  23. * @package app\admin\controller\api
  24. */
  25. class Upload extends Controller
  26. {
  27. /**
  28. * 上传安全检查
  29. * @throws \think\Exception
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public function check()
  35. {
  36. $exts = array_intersect(explode(',', input('exts', '')), explode(',', sysconf('storage.allow_exts')));
  37. $this->success('获取文件上传参数', ['exts' => join('|', $exts), 'mime' => Storage::mime($exts)]);
  38. }
  39. /**
  40. * 检查文件上传已经上传
  41. * @throws \think\Exception
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. */
  46. public function state()
  47. {
  48. $data = ['uptype' => $this->getType(), 'xkey' => input('xkey')];
  49. if ($info = Storage::instance($data['uptype'])->info($data['xkey'])) {
  50. $data['url'] = $info['url'];
  51. $data['pathinfo'] = $info['file'];
  52. $this->success('文件已经上传', $data, 200);
  53. } elseif ('local' === $data['uptype']) {
  54. $data['url'] = LocalStorage::instance()->url($data['xkey']);
  55. $data['server'] = LocalStorage::instance()->upload();
  56. } elseif ('qiniu' === $data['uptype']) {
  57. $data['url'] = QiniuStorage::instance()->url($data['xkey']);
  58. $data['token'] = QiniuStorage::instance()->buildUploadToken($data['xkey']);
  59. $data['server'] = QiniuStorage::instance()->upload();
  60. } elseif ('alioss' === $data['uptype']) {
  61. $token = AliossStorage::instance()->buildUploadToken($data['xkey']);
  62. $data['server'] = AliossStorage::instance()->upload();
  63. $data['url'] = $token['siteurl'];
  64. $data['policy'] = $token['policy'];
  65. $data['signature'] = $token['signature'];
  66. $data['OSSAccessKeyId'] = $token['keyid'];
  67. }
  68. $this->success('获取上传参数', $data, 404);
  69. }
  70. /**
  71. * 文件上传入口
  72. * @return \think\response\Json
  73. * @throws \think\Exception
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. */
  78. public function file()
  79. {
  80. if (!($file = $this->getFile()) || empty($file)) {
  81. return json(['uploaded' => false, 'error' => ['message' => '文件上传异常,文件可能过大或未上传']]);
  82. }
  83. $this->extension = $file->getOriginalExtension();
  84. if (!in_array($this->extension, explode(',', sysconf('storage.allow_exts')))) {
  85. return json(['uploaded' => false, 'error' => ['message' => '文件上传类型受限,请在后台配置']]);
  86. }
  87. if (in_array($this->extension, ['php', 'sh'])) {
  88. return json(['uploaded' => false, 'error' => ['message' => '可执行文件禁止上传到本地服务器']]);
  89. }
  90. list($this->safe, $this->uptype) = [boolval(input('safe')), $this->getType()];
  91. $name = Storage::name($file->getPathname(), $this->extension, '', 'md5_file');
  92. $info = Storage::instance($this->uptype)->set($name, file_get_contents($file->getRealPath()), $this->safe);
  93. if (is_array($info) && isset($info['url'])) {
  94. return json(['uploaded' => true, 'filename' => $name, 'url' => $this->safe ? $name : $info['url']]);
  95. } else {
  96. return json(['uploaded' => false, 'error' => ['message' => '文件处理失败,请稍候再试!']]);
  97. }
  98. }
  99. /**
  100. * 获取文件上传方式
  101. * @return string
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. private function getType()
  107. {
  108. $this->uptype = input('uptype');
  109. if (!in_array($this->uptype, ['local', 'qiniu', 'alioss'])) {
  110. $this->uptype = sysconf('storage.type');
  111. }
  112. return $this->uptype;
  113. }
  114. /**
  115. * 获取本地文件对象
  116. * @return \think\file\UploadedFile
  117. */
  118. private function getFile()
  119. {
  120. try {
  121. return $this->request->file('file');
  122. } catch (\Exception $e) {
  123. $this->error(lang($e->getMessage()));
  124. }
  125. }
  126. }