Fix content type detection for JSON files.

This commit is contained in:
Simon Schrape 2022-07-08 15:15:05 +02:00
parent bbaa006765
commit 5e5ee9643a
3 changed files with 14 additions and 1 deletions

View File

@ -315,7 +315,7 @@ module CarrierWave
if type.nil?
type = Marcel::Magic.by_path(path).try(:type)
type = 'invalid/invalid' unless type.nil? || type.start_with?('text/')
type = 'invalid/invalid' unless type.nil? || type.start_with?('text/') || type.start_with?('application/json')
end
type

3
spec/fixtures/bork.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"bork": "bork"
}

View File

@ -264,6 +264,16 @@ describe CarrierWave::SanitizedFile do
expect(sanitized_file.content_type).to eq 'text/plain'
end
it "returns valid content type on JSON file (which is a text file in disguise)" do
file = File.open(file_path('bork.json'))
sanitized_file = CarrierWave::SanitizedFile.new(file)
expect { sanitized_file.content_type }.not_to raise_error
expect(sanitized_file.content_type).to eq 'application/json'
end
it "returns missing content type with unknown extension" do
file = File.open(file_path('bork.ABCDE'))