Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1248 from hasghari/missing-extension

missing file extension
  • Loading branch information...
commit 11e35cd08edfc0ab6682207820158f0cce385b3f 2 parents 2376746 + e3a46e2
@bensie bensie authored
View
16 lib/carrierwave/uploader/download.rb
@@ -17,11 +17,12 @@ def initialize(uri)
end
def original_filename
- if file.meta.include? 'content-disposition'
- match = file.meta['content-disposition'].match(/filename=(\"?)(.+)\1/)
- return match[2] unless match.nil?
+ filename = filename_from_header || File.basename(file.base_uri.path)
+ mime_type = MIME::Types[file.content_type].first
+ unless File.extname(filename).present? || mime_type.blank?
+ filename = "#{filename}.#{mime_type.extensions.first}"
end
- File.basename(file.base_uri.path)
+ filename
end
def respond_to?(*args)
@@ -45,6 +46,13 @@ def file
raise CarrierWave::DownloadError, "could not download file: #{e.message}"
end
+ def filename_from_header
+ if file.meta.include? 'content-disposition'
+ match = file.meta['content-disposition'].match(/filename=(\"?)(.+)\1/)
+ return match[2] unless match.nil?
+ end
+ end
+
def method_missing(*args, &block)
file.send(*args, &block)
end
View
6 spec/uploader/download_spec.rb
@@ -20,6 +20,7 @@
sham_rack_app = ShamRack.at('www.example.com').stub
sham_rack_app.register_resource('/test.jpg', File.read(file_path('test.jpg')), 'image/jpg')
+ sham_rack_app.register_resource('/test-with-no-extension/test', File.read(file_path('test.jpg')), 'image/jpeg')
sham_rack_app.register_resource('/test%20with%20spaces/test.jpg', File.read(file_path('test.jpg')), 'image/jpg')
sham_rack_app.handle do |request|
if request.path_info == '/content-disposition'
@@ -108,6 +109,11 @@
@uploader.url.should == '/uploads/tmp/1369894322-345-2255/another_test.jpg'
end
+ it 'should set file extension based on content-type if missing' do
+ @uploader.download!('http://www.example.com/test-with-no-extension/test')
+ @uploader.url.should == '/uploads/tmp/1369894322-345-2255/test.jpeg'
+ end
+
it 'should not obscure original exception message' do
expect {
@uploader.download!('http://www.example.com/missing.jpg')
Please sign in to comment.
Something went wrong with that request. Please try again.