Upload.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://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. use think\admin\storage\TxcosStorage;
  21. /**
  22. * 文件上传接口
  23. * Class Upload
  24. * @package app\admin\controller\api
  25. */
  26. class Upload extends Controller
  27. {
  28. /**
  29. * 文件上传JS支持
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public function index()
  35. {
  36. $data = ['exts' => []];
  37. foreach (explode(',', sysconf('storage.allow_exts')) as $ext) {
  38. $data['exts'][$ext] = Storage::mime($ext);
  39. }
  40. $template = realpath(__DIR__ . '/../../view/api/upload.js');
  41. $data['exts'] = json_encode($data['exts'], JSON_UNESCAPED_UNICODE);
  42. return view($template, $data)->contentType('application/x-javascript');
  43. }
  44. /**
  45. * 检查文件上传已经上传
  46. * @login true
  47. * @throws \think\Exception
  48. * @throws \think\admin\Exception
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function state()
  54. {
  55. [$this->name, $this->safe] = [input('name', null), $this->getSafe()];
  56. $data = ['uptype' => $this->getType(), 'xkey' => input('xkey'), 'safe' => intval($this->safe)];
  57. if ($info = Storage::instance($data['uptype'])->info($data['xkey'], $this->safe, $this->name)) {
  58. $data['url'] = $info['url'];
  59. $this->success('文件已经上传', $data, 200);
  60. } elseif ('local' === $data['uptype']) {
  61. $data['url'] = LocalStorage::instance()->url($data['xkey'], $this->safe, $this->name);
  62. $data['server'] = LocalStorage::instance()->upload();
  63. } elseif ('qiniu' === $data['uptype']) {
  64. $data['url'] = QiniuStorage::instance()->url($data['xkey'], $this->safe, $this->name);
  65. $data['token'] = QiniuStorage::instance()->buildUploadToken($data['xkey'], 3600, $this->name);
  66. $data['server'] = QiniuStorage::instance()->upload();
  67. } elseif ('alioss' === $data['uptype']) {
  68. $token = AliossStorage::instance()->buildUploadToken($data['xkey'], 3600, $this->name);
  69. $data['url'] = $token['siteurl'];
  70. $data['policy'] = $token['policy'];
  71. $data['signature'] = $token['signature'];
  72. $data['OSSAccessKeyId'] = $token['keyid'];
  73. $data['server'] = AliossStorage::instance()->upload();
  74. } elseif ('txcos' === $data['uptype']) {
  75. $token = TxcosStorage::instance()->buildUploadToken($data['xkey'], 3600, $this->name);
  76. $data['url'] = $token['siteurl'];
  77. $data['q-ak'] = $token['q-ak'];
  78. $data['policy'] = $token['policy'];
  79. $data['q-key-time'] = $token['q-key-time'];
  80. $data['q-signature'] = $token['q-signature'];
  81. $data['q-sign-algorithm'] = $token['q-sign-algorithm'];
  82. $data['server'] = TxcosStorage::instance()->upload();
  83. }
  84. $this->success('获取授权参数', $data, 404);
  85. }
  86. /**
  87. * 文件上传入口
  88. * @login true
  89. * @return \think\response\Json
  90. * @throws \think\admin\Exception
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\DbException
  93. * @throws \think\db\exception\ModelNotFoundException
  94. */
  95. public function file()
  96. {
  97. if (!($file = $this->getFile()) || empty($file)) {
  98. return json(['uploaded' => false, 'error' => ['message' => '文件上传异常,文件可能过大或未上传']]);
  99. }
  100. $this->extension = strtolower($file->getOriginalExtension());
  101. if (!in_array($this->extension, explode(',', strtolower(sysconf('storage.allow_exts'))))) {
  102. return json(['uploaded' => false, 'error' => ['message' => '文件上传类型受限,请在后台配置']]);
  103. }
  104. if (in_array($this->extension, ['php', 'sh'])) {
  105. return json(['uploaded' => false, 'error' => ['message' => '可执行文件禁止上传到本地服务器']]);
  106. }
  107. [$this->safe, $this->uptype, $this->name] = [$this->getSafe(), $this->getType(), input('xkey')];
  108. if (empty($this->name)) $this->name = Storage::name($file->getPathname(), $this->extension, '', 'md5_file');
  109. if ($this->uptype === 'local') {
  110. $local = LocalStorage::instance();
  111. $realpath = dirname($realname = $local->path($this->name, $this->safe));
  112. file_exists($realpath) && is_dir($realpath) || mkdir($realpath, 0755, true);
  113. @rename($file->getPathname(), $realname);
  114. $info = $local->info($this->name, $this->safe, $file->getOriginalName());
  115. } else {
  116. $bina = file_get_contents($file->getRealPath());
  117. $info = Storage::instance($this->uptype)->set($this->name, $bina, $this->safe, $file->getOriginalName());
  118. }
  119. if (is_array($info) && isset($info['url'])) {
  120. return json(['uploaded' => true, 'filename' => $this->name, 'url' => $this->safe ? $this->name : $info['url']]);
  121. } else {
  122. return json(['uploaded' => false, 'error' => ['message' => '文件处理失败,请稍候再试!']]);
  123. }
  124. }
  125. /**
  126. * 获取文件上传类型
  127. * @return boolean
  128. */
  129. private function getSafe()
  130. {
  131. return boolval(input('safe', '0'));
  132. }
  133. /**
  134. * 获取文件上传方式
  135. * @return string
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\DbException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. */
  140. private function getType()
  141. {
  142. $this->uptype = strtolower(input('uptype', ''));
  143. if (!in_array($this->uptype, ['local', 'qiniu', 'alioss', 'txcos'])) {
  144. $this->uptype = strtolower(sysconf('storage.type'));
  145. }
  146. return strtolower($this->uptype);
  147. }
  148. /**
  149. * 获取本地文件对象
  150. * @return \think\file\UploadedFile
  151. */
  152. private function getFile()
  153. {
  154. try {
  155. return $this->request->file('file');
  156. } catch (\Exception $exception) {
  157. $this->error(lang($exception->getMessage()));
  158. }
  159. }
  160. }