Skip to content

Commit

Permalink
Merge pull request #2419 from fukayatsu/fix-remote-file-error
Browse files Browse the repository at this point in the history
Fix `remote_x_url=` raise undefined method `[]' for nil:NilClass (NoMethodError)
  • Loading branch information
mshibuya committed Sep 10, 2019
2 parents 5494bf4 + 449397a commit b2bd43b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/carrierwave/downloader/remote_file.rb
Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion spec/downloader/remote_file_spec.rb
Expand Up @@ -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

Expand All @@ -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' }

Expand Down

0 comments on commit b2bd43b

Please sign in to comment.