diff --git a/src/Conversions/ImageGenerators/ImageGeneratorFactory.php b/src/Conversions/ImageGenerators/ImageGeneratorFactory.php index 577da0501..a0adc600f 100644 --- a/src/Conversions/ImageGenerators/ImageGeneratorFactory.php +++ b/src/Conversions/ImageGenerators/ImageGeneratorFactory.php @@ -24,12 +24,20 @@ public static function getImageGenerators(): Collection public static function forExtension(?string $extension): ?ImageGenerator { + if (is_null($extension)) { + return null; + } + return static::getImageGenerators() ->first(fn (ImageGenerator $imageGenerator) => $imageGenerator->canHandleExtension(strtolower($extension))); } public static function forMimeType(?string $mimeType): ?ImageGenerator { + if (is_null($mimeType)) { + return null; + } + return static::getImageGenerators() ->first(fn (ImageGenerator $imageGenerator) => $imageGenerator->canHandleMime($mimeType)); } diff --git a/tests/Feature/FileAdder/IntegrationTest.php b/tests/Feature/FileAdder/IntegrationTest.php index 422c3ddf1..cf4a884db 100644 --- a/tests/Feature/FileAdder/IntegrationTest.php +++ b/tests/Feature/FileAdder/IntegrationTest.php @@ -2,6 +2,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Facades\Storage; +use Spatie\MediaLibrary\Conversions\ImageGenerators\ImageGeneratorFactory; use Spatie\MediaLibrary\MediaCollections\Exceptions\DiskCannotBeAccessed; use Spatie\MediaLibrary\MediaCollections\Exceptions\DiskDoesNotExist; use Spatie\MediaLibrary\MediaCollections\Exceptions\FileDoesNotExist; @@ -653,3 +654,11 @@ expect(Media::count())->toBe(1); }); + +it('will return null instead of an ImageGeneratorFactory when mimetype is null', function(){ + expect(ImageGeneratorFactory::forMimeType(null))->toBeNull(); +}); + +it('will return null instead of an ImageGeneratorFactory when extension is null', function(){ + expect(ImageGeneratorFactory::forExtension(null))->toBeNull(); +});