diff --git a/activestorage/app/models/active_storage/blob/representable.rb b/activestorage/app/models/active_storage/blob/representable.rb index e2c2663331a3b..65d7d64b9e5c4 100644 --- a/activestorage/app/models/active_storage/blob/representable.rb +++ b/activestorage/app/models/active_storage/blob/representable.rb @@ -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 diff --git a/activestorage/test/models/variant_test.rb b/activestorage/test/models/variant_test.rb index 050d2b7deccb8..7e8f792fa1e33 100644 --- a/activestorage/test/models/variant_test.rb +++ b/activestorage/test/models/variant_test.rb @@ -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