diff --git a/lib/carrierwave/sanitized_file.rb b/lib/carrierwave/sanitized_file.rb index 3a954b627..5d7fab40a 100644 --- a/lib/carrierwave/sanitized_file.rb +++ b/lib/carrierwave/sanitized_file.rb @@ -324,7 +324,7 @@ def sanitize(name) def existing_content_type if @file.respond_to?(:content_type) && @file.content_type - @file.content_type.to_s.chomp + Marcel::MimeType.for(declared_type: @file.content_type.to_s.chomp) end end diff --git a/spec/sanitized_file_spec.rb b/spec/sanitized_file_spec.rb index 6c94d08da..0fb1681e2 100644 --- a/spec/sanitized_file_spec.rb +++ b/spec/sanitized_file_spec.rb @@ -274,6 +274,33 @@ expect { sanitized_file.content_type }.not_to raise_error end + + it "uses the first one when multiple mime types are given using a semicolon" do + file = File.open(file_path("bork.txt")) + allow(file).to receive(:content_type) { 'image/png; text/html' } + + sanitized_file = CarrierWave::SanitizedFile.new(file) + + expect(sanitized_file.content_type).to eq("image/png") + end + + it "uses the first one when multiple mime types are given using a comma" do + file = File.open(file_path("bork.txt")) + allow(file).to receive(:content_type) { 'image/png, text/html' } + + sanitized_file = CarrierWave::SanitizedFile.new(file) + + expect(sanitized_file.content_type).to eq("image/png") + end + + it "drops content type parameters" do + file = File.open(file_path("bork.txt")) + allow(file).to receive(:content_type) { 'text/html; charset=utf-8' } + + sanitized_file = CarrierWave::SanitizedFile.new(file) + + expect(sanitized_file.content_type).to eq("text/html") + end end describe "#content_type=" do diff --git a/spec/uploader/content_type_whitelist_spec.rb b/spec/uploader/content_type_whitelist_spec.rb index 428802465..b2943b110 100644 --- a/spec/uploader/content_type_whitelist_spec.rb +++ b/spec/uploader/content_type_whitelist_spec.rb @@ -87,22 +87,6 @@ expect { uploader.cache!(bork_file) }.to raise_error(CarrierWave::IntegrityError) end end - - context "when the allowlist contains charset" do - before do - allow(uploader).to receive(:content_type_allowlist).and_return(%r{text/plain;\s*charset=utf-8}) - end - - it "accepts the content with allowed charset" do - allow(bork_file).to receive(:content_type).and_return('text/plain; charset=utf-8') - expect { uploader.cache!(bork_file) }.not_to raise_error - end - - it "rejects the content without charset" do - allow(bork_file).to receive(:content_type).and_return('text/plain') - expect { uploader.cache!(bork_file) }.to raise_error(CarrierWave::IntegrityError) - end - end end context "when there is a whitelist" do