Only load uploaded media as image when it is an image - #325
Merged
Conversation
`MediaRepository::storeMediaToModel` called `ImageFactory::load()` on every uploaded file to capture the original dimensions for image collections. For non-image uploads (PDFs, ZIPs, etc.) this throws `Spatie\Image\Exceptions\CouldNotLoadImage` from GD's `imagecreatefromstring()` and the request fails with a 500, so non-image files can no longer be attached to media collections. Move the `ImageFactory::load()` call inside the existing mime-type guard so it only runs when the upload actually is an image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MediaRepository::storeMediaToModelcurrently callsImageFactory::load(\$request->media->path())on every uploaded file in order to populateoriginal_dimensionsfor image collections. The mime-type check on the line below only guards using the result — the load itself runs unconditionally.For non-image uploads (PDFs, ZIPs, etc.) this throws
Spatie\Image\Exceptions\CouldNotLoadImagefrom GD'simagecreatefromstring()and the upload fails with a 500. As a result non-image files can no longer be attached to media collections (e.g. a\$form->file('file')->accept('application/pdf')PDF field).Example error from production with a PDF upload to a
filecollection:Change
Move the
ImageFactory::load()call inside the existingStr::startsWith(\$mime, 'image')guard so it only runs when the upload actually is an image. Behavior for image uploads is unchanged.Test plan
\$form->image()field —custom_properties.original_dimensionsis still populated.\$form->file()media field that acceptsapplication/pdf— request succeeds, file is attached.