lib/carrierwave/uploader/download.rb in carrierwave-0.9.0 vs lib/carrierwave/uploader/download.rb in carrierwave-0.10.0
- old
+ new
@@ -15,15 +15,16 @@
def initialize(uri)
@uri = 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)
super or file.respond_to?(*args)
end
@@ -43,10 +44,17 @@
rescue Exception => e
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="?([^"]+)/)
+ return match[1] unless match.nil? || match[1].empty?
+ end
+ end
+
def method_missing(*args, &block)
file.send(*args, &block)
end
end
@@ -56,15 +64,13 @@
# === Parameters
#
# [url (String)] The URL where the remote file is stored
#
def download!(uri)
- unless uri.blank?
- processed_uri = process_uri(uri)
- file = RemoteFile.new(processed_uri)
- raise CarrierWave::DownloadError, "trying to download a file which is not served over HTTP" unless file.http?
- cache!(file)
- end
+ processed_uri = process_uri(uri)
+ file = RemoteFile.new(processed_uri)
+ raise CarrierWave::DownloadError, "trying to download a file which is not served over HTTP" unless file.http?
+ cache!(file)
end
##
# Processes the given URL by parsing and escaping it. Public to allow overriding.
#