From 2727978c64330b6b0148c2fc3069960cdcf921ba Mon Sep 17 00:00:00 2001 From: wispx <1591788658@qq.com> Date: Thu, 6 Dec 2018 12:30:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=9B=BE=E7=89=87=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/http/middleware/Auth.php | 3 +- application/index/controller/Upload.php | 177 +++++++++++--------- application/index/controller/api/Base.php | 8 +- application/index/controller/api/Upload.php | 16 ++ 4 files changed, 117 insertions(+), 87 deletions(-) diff --git a/application/http/middleware/Auth.php b/application/http/middleware/Auth.php index 695b5c3d..dc1c6874 100644 --- a/application/http/middleware/Auth.php +++ b/application/http/middleware/Auth.php @@ -7,7 +7,7 @@ use think\facade\Session; class Auth { /** - * 无需登录可访问的方法 + * 无需登录可访问的方法(除分层控制器) * * @var array */ @@ -15,7 +15,6 @@ class Auth 'Index/index', 'Upload/upload', 'Auth/*', - 'Api/*' ]; public function handle($request, \Closure $next) diff --git a/application/index/controller/Upload.php b/application/index/controller/Upload.php index 391ba454..bd55332d 100644 --- a/application/index/controller/Upload.php +++ b/application/index/controller/Upload.php @@ -21,87 +21,8 @@ class Upload extends Base if ($this->request->isPost()) { Db::startTrans(); try { - if (!$this->config['allowed_tourist_upload'] && !$this->user) { - throw new Exception('管理员关闭了游客上传!'); - } - $image = $this->getImage(); - $size = $image->getSize(); - $mime = $image->getMime(); - $sha1 = $image->hash('sha1'); - $md5 = $image->hash('md5'); - - if ($this->user) { - if (($this->user->use_quota + $size) > $this->user->quota) { - throw new Exception('保存失败!您的储存容量不足,请联系管理员!'); - } - } - - // 当前储存策略 - $currentStrategy = strtolower($this->config['storage_strategy']); - // 获取当前储存策略配置 - $strategyConfig = $this->currentStrategyConfig; - // 获取当前驱动实例 - $strategy = $this->getStrategyInstance(); - - $pathname = strtolower($this->makePathname($image->getInfo('name'))); - if (!$strategy->create($pathname, $image->getPathname())) { - if (Config::get('app.app_debug')) { - throw new Exception($strategy->getError()); - } - throw new Exception('上传失败'); - } - - $cdnDomain = $currentStrategy . '_cdn_domain'; - $domain = $this->request->domain(); - if (array_key_exists($cdnDomain, $strategyConfig)) { - if ($strategyConfig[$cdnDomain]) { - $domain = $strategyConfig[$cdnDomain]; - } - } - $url = make_url($domain, $pathname); - - // 图片鉴黄 - if ($this->config['open_audit']) { - $client = new Client(); - $response = $client->get("https://www.moderatecontent.com/api/v2?key={$this->config['audit_key']}&url={$url}"); - if (200 == $response->getStatusCode()) { - $result = json_decode($response->getBody()); - if (0 == $result->error_code) { - if ($result->rating_index >= $this->config['audit_index']) { - $strategy->delete($pathname); - throw new Exception('图片[' . $image->getInfo('name') . ']涉嫌违规,禁止上传!'); - } - } else { - $strategy->delete($pathname); - throw new Exception($result->error); - } - } - } - - if (!Images::create([ - 'user_id' => $this->user ? $this->user->id : 0, - 'strategy' => $currentStrategy, - 'path' => dirname($pathname), - 'name' => basename($pathname), - 'pathname' => $pathname, - 'size' => $size, - 'mime' => $mime, - 'sha1' => $sha1, - 'md5' => $md5 - ])) { - $strategy->delete($pathname); - throw new Exception('图片数据保存失败'); - } - - $data = [ - 'name' => $image->getInfo('name'), - 'url' => $url, - ]; - if ($this->user) { - $data['quota'] = sprintf('%.2f', (float)$this->user->quota); - $data['use_quota'] = sprintf('%.2f', (float)$this->user->use_quota); - } + $data = $this->execute(); Db::commit(); } catch (Exception $e) { @@ -113,6 +34,99 @@ class Upload extends Base } } + /** + * 执行上传,成功返回数据,否则直接抛出异常 + * + * @return array + * @throws Exception + */ + public function execute() + { + if (!$this->config['allowed_tourist_upload'] && !$this->user) { + throw new Exception('管理员关闭了游客上传!'); + } + + $image = $this->getImage(); + $size = $image->getSize(); + $mime = $image->getMime(); + $sha1 = $image->hash('sha1'); + $md5 = $image->hash('md5'); + + if ($this->user) { + if (($this->user->use_quota + $size) > $this->user->quota) { + throw new Exception('保存失败!您的储存容量不足,请联系管理员!'); + } + } + + // 当前储存策略 + $currentStrategy = strtolower($this->config['storage_strategy']); + // 获取当前储存策略配置 + $strategyConfig = $this->currentStrategyConfig; + // 获取当前驱动实例 + $strategy = $this->getStrategyInstance(); + + $pathname = strtolower($this->makePathname($image->getInfo('name'))); + if (!$strategy->create($pathname, $image->getPathname())) { + if (Config::get('app.app_debug')) { + throw new Exception($strategy->getError()); + } + throw new Exception('上传失败'); + } + + $cdnDomain = $currentStrategy . '_cdn_domain'; + $domain = $this->request->domain(); + if (array_key_exists($cdnDomain, $strategyConfig)) { + if ($strategyConfig[$cdnDomain]) { + $domain = $strategyConfig[$cdnDomain]; + } + } + $url = make_url($domain, $pathname); + + // 图片鉴黄 + if ($this->config['open_audit']) { + $client = new Client(); + $response = $client->get("https://www.moderatecontent.com/api/v2?key={$this->config['audit_key']}&url={$url}"); + if (200 == $response->getStatusCode()) { + $result = json_decode($response->getBody()); + if (0 == $result->error_code) { + if ($result->rating_index >= $this->config['audit_index']) { + $strategy->delete($pathname); + throw new Exception('图片[' . $image->getInfo('name') . ']涉嫌违规,禁止上传!'); + } + } else { + $strategy->delete($pathname); + throw new Exception($result->error); + } + } + } + + if (!Images::create([ + 'user_id' => $this->user ? $this->user->id : 0, + 'strategy' => $currentStrategy, + 'path' => dirname($pathname), + 'name' => basename($pathname), + 'pathname' => $pathname, + 'size' => $size, + 'mime' => $mime, + 'sha1' => $sha1, + 'md5' => $md5 + ])) { + $strategy->delete($pathname); + throw new Exception('图片数据保存失败'); + } + + $data = [ + 'name' => $image->getInfo('name'), + 'url' => $url, + ]; + if ($this->user) { + $data['quota'] = sprintf('%.2f', (float)$this->user->quota); + $data['use_quota'] = sprintf('%.2f', (float)$this->user->use_quota); + } + + return $data; + } + /** * 获取图片资源 * @@ -123,7 +137,7 @@ class Upload extends Base { $image = $this->request->file('image'); if (null === $image) { - throw new Exception('图片资源获取失败!'); + throw new Exception('图片资源获取失败'); } if (!is_uploaded_file($image->getPathname())) { throw new Exception('file is not uploaded via HTTP POST'); @@ -134,6 +148,7 @@ class Upload extends Base ])) { throw new Exception($image->getError()); } + return $image; } diff --git a/application/index/controller/api/Base.php b/application/index/controller/api/Base.php index ff773a1e..7075df1d 100644 --- a/application/index/controller/api/Base.php +++ b/application/index/controller/api/Base.php @@ -70,12 +70,12 @@ class Base extends Controller /** * 返回数据给客户端并中断输出 * - * @param $msg 提示信息 - * @param int $code 状态码 - * @param array $data 数据 + * @param string $msg 提示信息 + * @param int $code 状态码 + * @param array $data 数据 * */ - protected function response($msg, $code = 200, $data = []) + protected function response($msg = '', $code = 200, $data = []) { $response = Response::create([ 'code' => $code, diff --git a/application/index/controller/api/Upload.php b/application/index/controller/api/Upload.php index 73c97b85..9e1d1018 100644 --- a/application/index/controller/api/Upload.php +++ b/application/index/controller/api/Upload.php @@ -8,6 +8,9 @@ namespace app\index\controller\api; +use think\Db; +use think\Exception; + class Upload extends Base { public function initialize($auth = false) @@ -21,8 +24,21 @@ class Upload extends Base } } + /** + * 上传 + */ public function index() { + Db::startTrans(); + try { + $data = (new \app\index\controller\Upload)->execute(); + + Db::commit(); + } catch (Exception $e) { + Db::rollback(); + return $this->response($e->getMessage(), 500); + } + return $this->response('success', 200, $data); } }