Skip to content

Commit b2bd43b

Browse files
authored
Merge pull request #2419 from fukayatsu/fix-remote-file-error
Fix `remote_x_url=` raise undefined method `[]' for nil:NilClass (NoMethodError)
2 parents 5494bf4 + 449397a commit b2bd43b

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

lib/carrierwave/downloader/remote_file.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ def respond_to?(*args)
2323
private
2424

2525
def filename_from_header
26-
if file.meta.include? 'content-disposition'
27-
match = file.meta['content-disposition'].match(/filename=(?:"([^"]+)"|([^";]+))/)
28-
match[1].presence || match[2].presence
29-
end
26+
return nil unless file.meta.include? 'content-disposition'
27+
28+
match = file.meta['content-disposition'].match(/filename=(?:"([^"]+)"|([^";]+))/)
29+
return nil unless match
30+
31+
match[1].presence || match[2].presence
3032
end
3133

3234
def filename_from_uri

spec/downloader/remote_file_spec.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
end
77
subject { CarrierWave::Downloader::RemoteFile.new(file) }
88

9-
it 'sets file extension based on content-type if missing' do
9+
before do
1010
subject.base_uri = URI.parse 'http://example.com/test'
1111
subject.meta_add_field 'content-type', 'image/jpeg'
12+
end
13+
14+
it 'sets file extension based on content-type if missing' do
1215
expect(subject.original_filename).to eq "test.jpeg"
1316
end
1417

@@ -25,6 +28,22 @@
2528
end
2629
end
2730

31+
context 'when filename is quoted and empty' do
32+
let(:content_disposition){ 'filename=""' }
33+
34+
it "sets file extension based on content-type if missing" do
35+
expect(subject.original_filename).to eq 'test.jpeg'
36+
end
37+
end
38+
39+
context 'when filename is not quoted and empty' do
40+
let(:content_disposition){ 'filename=' }
41+
42+
it "reads filename correctly" do
43+
expect(subject.original_filename).to eq 'test.jpeg'
44+
end
45+
end
46+
2847
context 'when filename is not quoted' do
2948
let(:content_disposition){ 'filename=another_test.jpg' }
3049

0 commit comments

Comments
 (0)