class Paperclip::UriAdapter

Attributes

content_type[W]

Public Class Methods

new(target, options = {}) click to toggle source
Calls superclass method Paperclip::AbstractAdapter::new
# File lib/paperclip/io_adapters/uri_adapter.rb, line 13
def initialize(target, options = {})
  super
  @content = download_content
  cache_current_values
  @tempfile = copy_to_tempfile(@content)
end
register() click to toggle source
# File lib/paperclip/io_adapters/uri_adapter.rb, line 7
def self.register
  Paperclip.io_adapters.register self do |target|
    target.is_a?(URI)
  end
end

Private Instance Methods

cache_current_values() click to toggle source
# File lib/paperclip/io_adapters/uri_adapter.rb, line 22
def cache_current_values
  self.content_type = content_type_from_content || "text/html"

  self.original_filename = filename_from_content_disposition ||
                           filename_from_path || default_filename
  @size = @content.size
end
content_type_from_content() click to toggle source
# File lib/paperclip/io_adapters/uri_adapter.rb, line 30
def content_type_from_content
  @content.meta["content-type"].presence
end
copy_to_tempfile(src) click to toggle source
# File lib/paperclip/io_adapters/uri_adapter.rb, line 69
def copy_to_tempfile(src)
  while data = src.read(16 * 1024)
    destination.write(data)
  end
  src.close
  destination.rewind
  destination
end
default_filename() click to toggle source
# File lib/paperclip/io_adapters/uri_adapter.rb, line 49
def default_filename
  "index.html"
end
download_content() click to toggle source
# File lib/paperclip/io_adapters/uri_adapter.rb, line 54
def download_content
  options = { read_timeout: Paperclip.options[:read_timeout] }.compact

  # rubocop:disable Security/Open
  open(@target, options)
  # rubocop:enable Security/Open
end
filename_from_content_disposition() click to toggle source
# File lib/paperclip/io_adapters/uri_adapter.rb, line 34
def filename_from_content_disposition
  if @content.meta.key?("content-disposition") && @content.meta["content-disposition"].match(/filename/i)
    # can include both filename and filename* values according to RCF6266. filename should come first
    _, filename = @content.meta["content-disposition"].split(/filename\*?\s*=\s*/i)

    # filename can be enclosed in quotes or not
    matches = filename.match(/"(.*)"/)
    matches ? matches[1] : filename.split(";")[0]
  end
end
filename_from_path() click to toggle source
# File lib/paperclip/io_adapters/uri_adapter.rb, line 45
def filename_from_path
  @target.path.split("/").last
end