diff --git a/app/Http/Controllers/MicropubMediaController.php b/app/Http/Controllers/MicropubMediaController.php index 52cc252d..38e7451b 100644 --- a/app/Http/Controllers/MicropubMediaController.php +++ b/app/Http/Controllers/MicropubMediaController.php @@ -13,6 +13,7 @@ use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\File; use Illuminate\Http\JsonResponse; +use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Http\UploadedFile; use Illuminate\Support\Carbon; @@ -33,11 +34,11 @@ class MicropubMediaController extends Controller $this->tokenService = $tokenService; } - public function getHandler(): JsonResponse + public function getHandler(Request $request): JsonResponse { try { - $tokenData = $this->tokenService->validateToken(request()->input('access_token')); - } catch (RequiredConstraintsViolated | InvalidTokenStructure $exception) { + $tokenData = $this->tokenService->validateToken($request->input('access_token')); + } catch (RequiredConstraintsViolated | InvalidTokenStructure) { $micropubResponses = new MicropubResponses(); return $micropubResponses->invalidTokenResponse(); @@ -55,19 +56,19 @@ class MicropubMediaController extends Controller return $micropubResponses->insufficientScopeResponse(); } - if (request()->input('q') === 'last') { - try { - $media = Media::latest()->whereDate('created_at', '>=', Carbon::now()->subMinutes(30))->firstOrFail(); - } catch (ModelNotFoundException $exception) { - return response()->json(['url' => null]); - } + if ($request->input('q') === 'last') { + $media = Media::where('created_at', '>=', Carbon::now()->subMinutes(30)) + ->where('token', $request->input('access_token')) + ->latest() + ->first(); + $mediaUrl = $media?->url; - return response()->json(['url' => $media->url]); + return response()->json(['url' => $mediaUrl]); } - if (request()->input('q') === 'source') { - $limit = request()->input('limit', 10); - $offset = request()->input('offset', 0); + if ($request->input('q') === 'source') { + $limit = $request->input('limit', 10); + $offset = $request->input('offset', 0); $media = Media::latest()->offset($offset)->limit($limit)->get(); @@ -80,12 +81,12 @@ class MicropubMediaController extends Controller return response()->json(['items' => $media]); } - if (request()->has('q')) { + if ($request->has('q')) { return response()->json([ 'error' => 'invalid_request', 'error_description' => sprintf( 'This server does not know how to handle this q parameter (%s)', - request()->input('q') + $request->input('q') ), ], 400); } diff --git a/tests/Feature/MicropubMediaTest.php b/tests/Feature/MicropubMediaTest.php index e8eb24d0..45e530bf 100644 --- a/tests/Feature/MicropubMediaTest.php +++ b/tests/Feature/MicropubMediaTest.php @@ -84,21 +84,27 @@ class MicropubMediaTest extends TestCase Queue::fake(); Storage::fake('s3'); $file = __DIR__ . '/../aaron.png'; + $token = $this->getToken(); + config(['filesystems.disks.s3.url' => 'https://s3.example.com']); $response = $this->post( '/api/media', [ 'file' => new UploadedFile($file, 'aaron.png', 'image/png', null, true), ], - ['HTTP_Authorization' => 'Bearer ' . $this->getToken()] + ['HTTP_Authorization' => 'Bearer ' . $token] ); + $location = $response->headers->get('Location'); + + $this->assertStringStartsWith('https://s3.example.com/', $location); + $path = parse_url($response->headers->get('Location'), PHP_URL_PATH); $filename = substr($path, 7); $lastUploadResponse = $this->get( '/api/media?q=last', - ['HTTP_Authorization' => 'Bearer ' . $this->getToken()] + ['HTTP_Authorization' => 'Bearer ' . $token] ); $lastUploadResponse->assertJson(['url' => $response->headers->get('Location')]); @@ -112,13 +118,14 @@ class MicropubMediaTest extends TestCase Queue::fake(); Storage::fake('s3'); $file = __DIR__ . '/../aaron.png'; + $token = $this->getToken(); $response = $this->post( '/api/media', [ 'file' => new UploadedFile($file, 'aaron.png', 'image/png', null, true), ], - ['HTTP_Authorization' => 'Bearer ' . $this->getToken()] + ['HTTP_Authorization' => 'Bearer ' . $token] ); $path = parse_url($response->headers->get('Location'), PHP_URL_PATH); @@ -126,7 +133,7 @@ class MicropubMediaTest extends TestCase $sourceUploadResponse = $this->get( '/api/media?q=source', - ['HTTP_Authorization' => 'Bearer ' . $this->getToken()] + ['HTTP_Authorization' => 'Bearer ' . $token] ); $sourceUploadResponse->assertJson(['items' => [[ 'url' => $response->headers->get('Location'), @@ -142,13 +149,14 @@ class MicropubMediaTest extends TestCase Queue::fake(); Storage::fake('s3'); $file = __DIR__ . '/../aaron.png'; + $token = $this->getToken(); $response = $this->post( '/api/media', [ 'file' => new UploadedFile($file, 'aaron.png', 'image/png', null, true), ], - ['HTTP_Authorization' => 'Bearer ' . $this->getToken()] + ['HTTP_Authorization' => 'Bearer ' . $token] ); $path = parse_url($response->headers->get('Location'), PHP_URL_PATH); @@ -156,7 +164,7 @@ class MicropubMediaTest extends TestCase $sourceUploadResponse = $this->get( '/api/media?q=source&limit=1', - ['HTTP_Authorization' => 'Bearer ' . $this->getToken()] + ['HTTP_Authorization' => 'Bearer ' . $token] ); $sourceUploadResponse->assertJson(['items' => [[ 'url' => $response->headers->get('Location'),