From 61ed6e0a3d4cd1aa792f11698c1ebc0c578d1955 Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Fri, 10 Mar 2023 12:47:14 +0100 Subject: [PATCH] Add icon to file --- .gitignore | 2 ++ src/MediaData.php | 10 ++++++++++ tests/Feature/IndexTest.php | 1 + 3 files changed, 13 insertions(+) diff --git a/.gitignore b/.gitignore index 57872d0..2ca5cc8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /vendor/ +/.php-cs-fixer.cache +/.phpunit.result.cache diff --git a/src/MediaData.php b/src/MediaData.php index 1613515..d459f57 100644 --- a/src/MediaData.php +++ b/src/MediaData.php @@ -3,6 +3,7 @@ namespace Zoomyboy\MedialibraryHelper; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; use Spatie\LaravelData\Attributes\MapInputName; use Spatie\LaravelData\Attributes\MapOutputName; use Spatie\LaravelData\Data; @@ -48,6 +49,15 @@ class MediaData extends Data ]); } + public function with(): array + { + $mime = Str::slug($this->mimeType); + + return [ + 'icon' => Storage::disk('public')->url("filetypes/{$mime}.svg"), + ]; + } + public static function defaultFromCollection(HasMedia $parent, MediaCollection $collection): ?self { $default = $collection->runCallback('withFallback', $parent); diff --git a/tests/Feature/IndexTest.php b/tests/Feature/IndexTest.php index 8d0aced..5f51989 100644 --- a/tests/Feature/IndexTest.php +++ b/tests/Feature/IndexTest.php @@ -16,6 +16,7 @@ test('it gets all medias', function () { $response->assertJsonPath('0.id', $firstMedia->id); $response->assertJsonPath('1.id', $secondMedia->id); $response->assertJsonPath('1.properties.test', 'old'); + $response->assertJsonPath('1.icon', url('storage/filetypes/applicationpdf.svg')); }); test('it gets media for single', function () {