123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkAdmin
- // +----------------------------------------------------------------------
- // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
- // +----------------------------------------------------------------------
- // | 官方网站: https://thinkadmin.top
- // +----------------------------------------------------------------------
- // | 开源协议 ( https://mit-license.org )
- // | 免费声明 ( https://thinkadmin.top/disclaimer )
- // +----------------------------------------------------------------------
- // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
- // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
- // +----------------------------------------------------------------------
- namespace app\admin\controller\api;
- use think\admin\Controller;
- use think\admin\Storage;
- use think\admin\storage\AliossStorage;
- use think\admin\storage\LocalStorage;
- use think\admin\storage\QiniuStorage;
- use think\admin\storage\TxcosStorage;
- use think\exception\HttpResponseException;
- use think\file\UploadedFile;
- use think\Response;
- use think\response\Json;
- /**
- * 文件上传接口
- * Class Upload
- * @package app\admin\controller\api
- */
- class Upload extends Controller
- {
- /**
- * 文件上传方式
- * @var string
- */
- private $type;
- /**
- * 文件上传模式
- * @var boolean
- */
- private $safe;
- /**
- * 文件上传脚本
- * @return Response
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function index(): Response
- {
- $data = ['exts' => []];
- foreach (str2arr(sysconf('storage.allow_exts')) as $ext) {
- $data['exts'][$ext] = Storage::mime($ext);
- }
- $template = realpath(__DIR__ . '/../../view/api/upload.js');
- $data['exts'] = json_encode($data['exts'], JSON_UNESCAPED_UNICODE);
- return view($template, $data)->contentType('application/x-javascript');
- }
- /**
- * 文件上传检查
- * @login true
- * @throws \think\admin\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function state()
- {
- [$this->name, $this->safe] = [input('name', null), $this->getSafe()];
- $data = ['uptype' => $this->getType(), 'safe' => intval($this->safe), 'key' => input('key')];
- if ($info = Storage::instance($data['uptype'])->info($data['key'], $this->safe, $this->name)) {
- $data['url'] = $info['url'];
- $this->success('文件已经上传', $data, 200);
- } elseif ('local' === $data['uptype']) {
- $data['url'] = LocalStorage::instance()->url($data['key'], $this->safe, $this->name);
- $data['server'] = LocalStorage::instance()->upload();
- } elseif ('qiniu' === $data['uptype']) {
- $data['url'] = QiniuStorage::instance()->url($data['key'], $this->safe, $this->name);
- $data['token'] = QiniuStorage::instance()->buildUploadToken($data['key'], 3600, $this->name);
- $data['server'] = QiniuStorage::instance()->upload();
- } elseif ('alioss' === $data['uptype']) {
- $token = AliossStorage::instance()->buildUploadToken($data['key'], 3600, $this->name);
- $data['url'] = $token['siteurl'];
- $data['policy'] = $token['policy'];
- $data['signature'] = $token['signature'];
- $data['OSSAccessKeyId'] = $token['keyid'];
- $data['server'] = AliossStorage::instance()->upload();
- } elseif ('txcos' === $data['uptype']) {
- $token = TxcosStorage::instance()->buildUploadToken($data['key'], 3600, $this->name);
- $data['url'] = $token['siteurl'];
- $data['q-ak'] = $token['q-ak'];
- $data['policy'] = $token['policy'];
- $data['q-key-time'] = $token['q-key-time'];
- $data['q-signature'] = $token['q-signature'];
- $data['q-sign-algorithm'] = $token['q-sign-algorithm'];
- $data['server'] = TxcosStorage::instance()->upload();
- }
- $this->success('获取上传授权参数', $data, 404);
- }
- /**
- * 文件上传入口
- * @login true
- * @return Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function file(): Json
- {
- if (!($file = $this->getFile())->isValid()) {
- return json(['uploaded' => false, 'error' => ['message' => '文件上传异常,文件过大或未上传!']]);
- }
- $extension = strtolower($file->getOriginalExtension());
- [$pathname, $original] = [$file->getPathname(), $file->getOriginalName()];
- if (!in_array($extension, str2arr(sysconf('storage.allow_exts')))) {
- return json(['uploaded' => false, 'error' => ['message' => '文件类型受限,请在后台配置规则!']]);
- }
- if (in_array($extension, ['sh', 'asp', 'bat', 'cmd', 'exe', 'php'])) {
- return json(['uploaded' => false, 'error' => ['message' => '文件安全保护,可执行文件禁止上传!']]);
- }
- [$this->type, $this->safe] = [$this->getType(), $this->getSafe()];
- $this->name = input('key') ?: Storage::name($pathname, $extension, '', 'md5_file');
- try {
- if ($this->type === 'local') {
- $local = LocalStorage::instance();
- $distname = $local->path($this->name, $this->safe);
- $file->move(dirname($distname), basename($distname));
- $info = $local->info($this->name, $this->safe, $original);
- if (in_array($extension, ['jpg', 'gif', 'png', 'bmp', 'jpeg', 'wbmp'])) {
- [$width, $height] = getimagesize($distname);
- if ($width < 1 || $height < 1 && $local->del($this->name)) {
- return json(['uploaded' => false, 'error' => ['message' => '图片尺寸读取失败!']]);
- }
- }
- } else {
- $bina = file_get_contents($pathname);
- $info = Storage::instance($this->type)->set($this->name, $bina, $this->safe, $original);
- }
- } catch (HttpResponseException $exception) {
- throw $exception;
- } catch (\Exception $exception) {
- return json(['uploaded' => false, 'error' => ['message' => $exception->getMessage()]]);
- }
- if (is_array($info) && isset($info['url'])) {
- return json(['uploaded' => true, 'filename' => $this->name, 'url' => $this->safe ? $this->name : $info['url']]);
- } else {
- return json(['uploaded' => false, 'error' => ['message' => '文件处理失败,请稍候再试!']]);
- }
- }
- /**
- * 获取文件上传类型
- * @return boolean
- */
- private function getSafe(): bool
- {
- return boolval(input('safe', '0'));
- }
- /**
- * 获取文件上传方式
- * @return string
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- private function getType(): string
- {
- $this->type = strtolower(input('uptype', ''));
- if (!in_array($this->type, ['local', 'qiniu', 'alioss', 'txcos'])) {
- $this->type = strtolower(sysconf('storage.type'));
- }
- return strtolower($this->type);
- }
- /**
- * 获取本地文件对象
- * @return UploadedFile
- */
- private function getFile(): UploadedFile
- {
- try {
- $file = $this->request->file('file');
- if ($file instanceof UploadedFile) {
- return $file;
- } else {
- $this->error('未获取到上传的文件对象!');
- }
- } catch (HttpResponseException $exception) {
- throw $exception;
- } catch (\Exception $exception) {
- $this->error(lang($exception->getMessage()));
- }
- }
- }
|