Upload.php 4.0 KB

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