59 lines
1.8 KiB
PHP
59 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Exceptions\UploadException;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\User;
|
|
use App\Services\ImageService;
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ImageController extends Controller
|
|
{
|
|
/**
|
|
* @throws AuthenticationException
|
|
*/
|
|
public function upload(Request $request, ImageService $service): Response
|
|
{
|
|
if ($request->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'
|
|
));
|
|
}
|
|
}
|