Upload.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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-sign-algorithm'] = $token['q-sign-algorithm'];
  81. $data['server'] = TxcosStorage::instance()->upload();
  82. }
  83. $this->success('获取授权参数', $data, 404);
  84. }
  85. /**
  86. * 文件上传入口
  87. * @login true
  88. * @return \think\response\Json
  89. * @throws \think\admin\Exception
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\DbException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. */
  94. public function file()
  95. {
  96. if (!($file = $this->getFile()) || empty($file)) {
  97. return json(['uploaded' => false, 'error' => ['message' => '文件上传异常,文件可能过大或未上传']]);
  98. }
  99. $this->extension = strtolower($file->getOriginalExtension());
  100. if (!in_array($this->extension, explode(',', strtolower(sysconf('storage.allow_exts'))))) {
  101. return json(['uploaded' => false, 'error' => ['message' => '文件上传类型受限,请在后台配置']]);
  102. }
  103. if (in_array($this->extension, ['php', 'sh'])) {
  104. return json(['uploaded' => false, 'error' => ['message' => '可执行文件禁止上传到本地服务器']]);
  105. }
  106. [$this->safe, $this->uptype, $this->name] = [$this->getSafe(), $this->getType(), input('xkey')];
  107. if (empty($this->name)) $this->name = Storage::name($file->getPathname(), $this->extension, '', 'md5_file');
  108. if ($this->uptype === 'local') {
  109. $local = LocalStorage::instance();
  110. $realpath = dirname($realname = $local->path($this->name, $this->safe));
  111. file_exists($realpath) && is_dir($realpath) || mkdir($realpath, 0755, true);
  112. @rename($file->getPathname(), $realname);
  113. $info = $local->info($this->name, $this->safe, $file->getOriginalName());
  114. } else {
  115. $bina = file_get_contents($file->getRealPath());
  116. $info = Storage::instance($this->uptype)->set($this->name, $bina, $this->safe, $file->getOriginalName());
  117. }
  118. if (is_array($info) && isset($info['url'])) {
  119. return json(['uploaded' => true, 'filename' => $this->name, 'url' => $this->safe ? $this->name : $info['url']]);
  120. } else {
  121. return json(['uploaded' => false, 'error' => ['message' => '文件处理失败,请稍候再试!']]);
  122. }
  123. }
  124. /**
  125. * 获取文件上传类型
  126. * @return boolean
  127. */
  128. private function getSafe()
  129. {
  130. return boolval(input('safe', '0'));
  131. }
  132. /**
  133. * 获取文件上传方式
  134. * @return string
  135. * @throws \think\db\exception\DataNotFoundException
  136. * @throws \think\db\exception\DbException
  137. * @throws \think\db\exception\ModelNotFoundException
  138. */
  139. private function getType()
  140. {
  141. $this->uptype = strtolower(input('uptype', ''));
  142. if (!in_array($this->uptype, ['local', 'qiniu', 'alioss', 'txcos'])) {
  143. $this->uptype = strtolower(sysconf('storage.type'));
  144. }
  145. return strtolower($this->uptype);
  146. }
  147. /**
  148. * 获取本地文件对象
  149. * @return \think\file\UploadedFile
  150. */
  151. private function getFile()
  152. {
  153. try {
  154. return $this->request->file('file');
  155. } catch (\Exception $exception) {
  156. $this->error(lang($exception->getMessage()));
  157. }
  158. }
  159. }