diff --git a/lib/carrierwave/downloader/remote_file.rb b/lib/carrierwave/downloader/remote_file.rb index 6cca1e3bc..654e5a2bb 100644 --- a/lib/carrierwave/downloader/remote_file.rb +++ b/lib/carrierwave/downloader/remote_file.rb @@ -23,10 +23,12 @@ def respond_to?(*args) private def filename_from_header - if file.meta.include? 'content-disposition' - match = file.meta['content-disposition'].match(/filename=(?:"([^"]+)"|([^";]+))/) - match[1].presence || match[2].presence - end + return nil unless file.meta.include? 'content-disposition' + + match = file.meta['content-disposition'].match(/filename=(?:"([^"]+)"|([^";]+))/) + return nil unless match + + match[1].presence || match[2].presence end def filename_from_uri diff --git a/spec/downloader/remote_file_spec.rb b/spec/downloader/remote_file_spec.rb index edbd75ec1..5ea9915a9 100644 --- a/spec/downloader/remote_file_spec.rb +++ b/spec/downloader/remote_file_spec.rb @@ -6,9 +6,12 @@ end subject { CarrierWave::Downloader::RemoteFile.new(file) } - it 'sets file extension based on content-type if missing' do + before do subject.base_uri = URI.parse 'http://example.com/test' subject.meta_add_field 'content-type', 'image/jpeg' + end + + it 'sets file extension based on content-type if missing' do expect(subject.original_filename).to eq "test.jpeg" end @@ -25,6 +28,22 @@ end end + context 'when filename is quoted and empty' do + let(:content_disposition){ 'filename=""' } + + it "sets file extension based on content-type if missing" do + expect(subject.original_filename).to eq 'test.jpeg' + end + end + + context 'when filename is not quoted and empty' do + let(:content_disposition){ 'filename=' } + + it "reads filename correctly" do + expect(subject.original_filename).to eq 'test.jpeg' + end + end + context 'when filename is not quoted' do let(:content_disposition){ 'filename=another_test.jpg' }