Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix remote_x_url= raise undefined method `[]' for nil:NilClass (NoMethodError) #2419

Merged
merged 1 commit into from Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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