Module: CarrierWave::Uploader::Download

Extended by:
ActiveSupport::Concern
Includes:
Cache, Callbacks, Configuration
Included in:
Base
Defined in:
vendor/carrierwave/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) ⇒ Object

Caches the file by downloading it from the given URL.

=== Parameters

[url (String)] The URL where the remote file is stored



66
67
68
69
70
71
# File 'vendor/carrierwave/lib/carrierwave/uploader/download.rb', line 66

def download!(uri)
  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

#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



80
81
82
83
84
85
86
87
88
# File 'vendor/carrierwave/lib/carrierwave/uploader/download.rb', line 80

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