Module: CarrierWave::Uploader::Download

Extended by:
ActiveSupport::Concern
Includes:
Cache, Callbacks, Configuration
Included in:
Base
Defined in:
lib/carrierwave/uploader/download.rb

Defined Under Namespace

Classes: RemoteFile

Instance Method Summary collapse

Methods included from Cache

#cache!, #cache_name, #cache_path, #cache_stored_file!, #cached?, #retrieve_from_cache!, #sanitized_file

Methods included from Callbacks

#with_callbacks

Instance Method Details

#download!(uri, remote_headers = {}) ⇒ Object

Caches the file by downloading it from the given URL.

Parameters

url (String)

The URL where the remote file is stored

remote_headers (Hash)

Request headers



76
77
78
79
80
81
# File 'lib/carrierwave/uploader/download.rb', line 76

def download!(uri, remote_headers = {})
  processed_uri = process_uri(uri)
  file = RemoteFile.new(processed_uri, remote_headers)
  raise CarrierWave::DownloadError, "trying to download a file which is not served over HTTP" unless file.http?
  cache!(file)
end

#process_uri(uri) ⇒ Object

Processes the given URL by parsing and escaping it. Public to allow overriding.

Parameters

url (String)

The URL where the remote file is stored



90
91
92
93
94
95
96
97
98
# File 'lib/carrierwave/uploader/download.rb', line 90

def process_uri(uri)
  URI.parse(uri)
rescue URI::InvalidURIError
  uri_parts = uri.split('?')
  # regexp from Ruby's URI::Parser#regexp[:UNSAFE], with [] specifically removed
  encoded_uri = URI.encode(uri_parts.shift, /[^\-_.!~*'()a-zA-Z\d;\/?:@&=+$,]/)
  encoded_uri << '?' << URI.encode(uri_parts.join('?')) if uri_parts.any?
  URI.parse(encoded_uri) rescue raise CarrierWave::DownloadError, "couldn't parse URL"
end