From 42f0fa223831d904b9aad08a0d0af1af70eb5e6e Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Sun, 20 Nov 2022 17:10:19 +0000 Subject: [PATCH] Return a published and mime type data with sourced media uploads --- .../Controllers/MicropubMediaController.php | 2 ++ app/Models/Media.php | 24 +++++++++++++++++++ tests/Feature/MicropubMediaTest.php | 4 ++++ 3 files changed, 30 insertions(+) diff --git a/app/Http/Controllers/MicropubMediaController.php b/app/Http/Controllers/MicropubMediaController.php index 5cf865f0..b360228c 100644 --- a/app/Http/Controllers/MicropubMediaController.php +++ b/app/Http/Controllers/MicropubMediaController.php @@ -74,6 +74,8 @@ class MicropubMediaController extends Controller $media->transform(function ($mediaItem) { return [ 'url' => $mediaItem->url, + 'published' => $mediaItem->created_at->toW3cString(), + 'mime_type' => $mediaItem->getMimeType(), ]; }); diff --git a/app/Models/Media.php b/app/Models/Media.php index 4b666d9e..85ec0290 100644 --- a/app/Models/Media.php +++ b/app/Models/Media.php @@ -107,4 +107,28 @@ class Media extends Model return array_pop($parts); } + + /** + * Get the mime type of the media file. + * + * For now we will just use the extension, but this could be improved. + * + * @return string + */ + public function getMimeType(): string + { + $extension = $this->getExtension($this->path); + + return match ($extension) { + 'gif' => 'image/gif', + 'jpeg', 'jpg' => 'image/jpeg', + 'png' => 'image/png', + 'svg' => 'image/svg+xml', + 'tiff' => 'image/tiff', + 'webp' => 'image/webp', + 'mp4' => 'video/mp4', + 'mkv' => 'video/mkv', + default => 'application/octet-stream', + }; + } } diff --git a/tests/Feature/MicropubMediaTest.php b/tests/Feature/MicropubMediaTest.php index 45e530bf..bcf2f187 100644 --- a/tests/Feature/MicropubMediaTest.php +++ b/tests/Feature/MicropubMediaTest.php @@ -137,6 +137,8 @@ class MicropubMediaTest extends TestCase ); $sourceUploadResponse->assertJson(['items' => [[ 'url' => $response->headers->get('Location'), + 'published' => carbon()->toW3cString(), + 'mime_type' => 'image/png', ]]]); // now remove file @@ -168,6 +170,8 @@ class MicropubMediaTest extends TestCase ); $sourceUploadResponse->assertJson(['items' => [[ 'url' => $response->headers->get('Location'), + 'published' => carbon()->toW3cString(), + 'mime_type' => 'image/png', ]]]); // And given our limit of 1 there should only be one result $this->assertCount(1, json_decode($sourceUploadResponse->getContent(), true)['items']);