diff --git a/app/Http/Controllers/Api/V1/ImageController.php b/app/Http/Controllers/Api/V1/ImageController.php new file mode 100644 index 00000000..9522015b --- /dev/null +++ b/app/Http/Controllers/Api/V1/ImageController.php @@ -0,0 +1,58 @@ +hasHeader('Authorization')) { + $guards = array_keys(config('auth.guards')); + + if (empty($guards)) { + $guards = [null]; + } + + foreach ($guards as $guard) { + if (Auth::guard($guard)->check()) { + Auth::shouldUse($guard); + break; + } + } + + if (! Auth::check()) { + throw new AuthenticationException('Authentication failed.'); + } + } + + try { + /** @var User $user */ + $user = Auth::user(); + $image = $service->store($request, $user); + } catch (UploadException $e) { + return $this->error($e->getMessage()); + } catch (\Throwable $e) { + Log::error("Api 上传文件时发生异常,", ['message' => $e->getMessage(), 'trace' => $e->getTraceAsString()]); + if (config('app.debug')) { + return $this->error($e->getMessage()); + } + return $this->error('服务异常,请稍后再试'); + } + return $this->success('上传成功', $image->setAppends(['pathname', 'links'])->only( + 'key', 'name', 'extension', 'pathname', 'origin_name', 'size', 'mimetype', 'md5', 'sha1', 'links' + )); + } +} diff --git a/resources/views/common/api.blade.php b/resources/views/common/api.blade.php index bb6ce1fb..bc40bb31 100644 --- a/resources/views/common/api.blade.php +++ b/resources/views/common/api.blade.php @@ -14,8 +14,6 @@
验证方式
如果未设置 Authorization 的情况下请求上传接口,将被视为游客上传。
上传图片
-Headers
@@ -113,9 +111,19 @@