Skip to content

Commit

Permalink
Merge pull request rails#42225 from ghiculescu/as-content-type-crash
Browse files Browse the repository at this point in the history
Active Storage: don't crash if mini_mime doesn't recognize content_type
  • Loading branch information
pixeltrix authored May 15, 2021
2 parents cc37c69 + fbcac36 commit 44ed158
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def format
if filename.extension.present? && MiniMime.lookup_by_extension(filename.extension)&.content_type == content_type
filename.extension
else
MiniMime.lookup_by_content_type(content_type).extension
MiniMime.lookup_by_content_type(content_type)&.extension
end
end

Expand Down
14 changes: 14 additions & 0 deletions activestorage/test/models/variant_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ class ActiveStorage::VariantTest < ActiveSupport::TestCase
end
end

test "doesn't crash content_type not recognized by mini_mime" do
blob = create_file_blob(filename: "racecar.jpg")

# image/jpg is not recognised by mini_mime (image/jpeg is correct)
blob.update(content_type: "image/jpg")

assert_nothing_raised do
blob.variant(resize: "100x100")
end

assert_nil blob.send(:format)
assert_equal :png, blob.send(:default_variant_format)
end

private
def process_variants_with(processor)
previous_processor, ActiveStorage.variant_processor = ActiveStorage.variant_processor, processor
Expand Down

0 comments on commit 44ed158

Please sign in to comment.