Upload.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /**
  18. * 文件上传接口
  19. * Class Upload
  20. * @package app\admin\controller\api
  21. */
  22. class Upload extends Controller
  23. {
  24. /**
  25. * 上传安全检查
  26. * @login true
  27. * @throws \think\Exception
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\DbException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. */
  32. public function check()
  33. {
  34. $diff1 = explode(',', strtolower(input('exts', '')));
  35. $diff2 = explode(',', strtolower(sysconf('storage.allow_exts')));
  36. $exts = array_intersect($diff1, $diff2);
  37. $this->success('获取文件上传参数', [
  38. 'type' => $this->getType(), 'data' => $this->getData(),
  39. 'exts' => join('|', $exts), 'mime' => Storage::mime($exts),
  40. ]);
  41. }
  42. /**
  43. * 文件上传入口
  44. * @login true
  45. * @return \think\response\Json
  46. * @throws \think\Exception
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function file()
  52. {
  53. if (!($file = $this->getFile()) || empty($file)) {
  54. return json(['uploaded' => false, 'error' => ['message' => '文件上传异常,文件可能过大或未上传']]);
  55. }
  56. $this->extension = $file->getOriginalExtension();
  57. if (!in_array($this->extension, explode(',', sysconf('storage.allow_exts')))) {
  58. return json(['uploaded' => false, 'error' => ['message' => '文件上传类型受限,请在后台配置']]);
  59. }
  60. if (in_array($this->extension, ['php', 'sh'])) {
  61. return json(['uploaded' => false, 'error' => ['message' => '可执行文件禁止上传到本地服务器']]);
  62. }
  63. list($this->safe, $this->uptype) = [boolval(input('safe')), $this->getType()];
  64. $name = Storage::name($file->getPathname(), $this->extension, '', 'md5_file');
  65. $info = Storage::instance($this->uptype)->set($name, file_get_contents($file->getRealPath()), $this->safe);
  66. if (is_array($info) && isset($info['url'])) {
  67. return json(['uploaded' => true, 'filename' => $name, 'url' => $this->safe ? $name : $info['url']]);
  68. } else {
  69. return json(['uploaded' => false, 'error' => ['message' => '文件处理失败,请稍候再试!']]);
  70. }
  71. }
  72. /**
  73. * 获取文件上传参数
  74. * @return array
  75. * @throws \think\Exception
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. private function getData()
  81. {
  82. if ($this->getType() === 'qiniu') {
  83. $file = Storage::instance('qiniu');
  84. return ['url' => $file->upload(), 'token' => $file->buildUploadToken(), 'uptype' => $this->getType()];
  85. } else {
  86. $file = Storage::instance('local');
  87. return ['url' => $file->upload(), 'token' => uniqid('local_upload_'), 'uptype' => $this->getType()];
  88. }
  89. }
  90. /**
  91. * 获取文件上传方式
  92. * @return string
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. private function getType()
  98. {
  99. $this->uptype = input('uptype');
  100. if (!in_array($this->uptype, ['local', 'qiniu'])) {
  101. $this->uptype = sysconf('storage.type');
  102. }
  103. return $this->uptype;
  104. }
  105. /**
  106. * 获取本地文件对象
  107. * @return \think\file\UploadedFile
  108. */
  109. private function getFile()
  110. {
  111. try {
  112. return $this->request->file('file');
  113. } catch (\Exception $e) {
  114. $this->error(lang($e->getMessage()));
  115. }
  116. }
  117. }