diff --git a/app/Http/Controllers/MicropubMediaController.php b/app/Http/Controllers/MicropubMediaController.php index 93788e33..e03062e7 100644 --- a/app/Http/Controllers/MicropubMediaController.php +++ b/app/Http/Controllers/MicropubMediaController.php @@ -11,10 +11,12 @@ use App\Models\Media; use App\Services\TokenService; use Exception; use Illuminate\Contracts\Container\BindingResolutionException; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\File; use Illuminate\Http\JsonResponse; use Illuminate\Http\Response; use Illuminate\Http\UploadedFile; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use Intervention\Image\Exception\NotReadableException; @@ -53,7 +55,11 @@ class MicropubMediaController extends Controller } if (request()->input('q') === 'last') { - $media = Media::latest()->firstOrFail(); + try { + $media = Media::latest()->whereDate('created_at', '>=', Carbon::now()->subMinutes(30))->firstOrFail(); + } catch (ModelNotFoundException $exception) { + return response()->json([], 404); + } return response()->json(['url' => $media->url]); } diff --git a/tests/Feature/MicropubMediaTest.php b/tests/Feature/MicropubMediaTest.php index 6a8c1496..132e3346 100644 --- a/tests/Feature/MicropubMediaTest.php +++ b/tests/Feature/MicropubMediaTest.php @@ -15,6 +15,16 @@ class MicropubMediaTest extends TestCase use DatabaseTransactions; use TestToken; + /** @test */ + public function emptyResponseForLastUploadWhenNoneFound() + { + $response = $this->get( + '/api/media?q=last', + ['HTTP_Authorization' => 'Bearer ' . $this->getToken()] + ); + $response->assertStatus(404); + } + /** @test */ public function clientCanListLastUpload() {