Skip to content

Commit

Permalink
properly handle null extension and mimetype with ImageGeneratorFactory (
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech authored Jul 27, 2023
1 parent e7c9fc3 commit bac3280
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Conversions/ImageGenerators/ImageGeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Feature/FileAdder/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});

0 comments on commit bac3280

Please sign in to comment.