Sha256: 433400e078f77c24d4638530e3129f464eeb429f3867e5d03872c325e0d06436

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require 'open-uri'

module Paperclip
  class UriAdapter < AbstractAdapter
    def initialize(target)
      @target = target
      @content = download_content
      cache_current_values
      @tempfile = copy_to_tempfile(@content)
    end

    attr_writer :content_type

    private

    def download_content
      options = { read_timeout: Paperclip.options[:read_timeout] }.compact

      open(@target, **options)
    end

    def cache_current_values
      @original_filename = @target.path.split("/").last
      @original_filename ||= "index.html"
      self.original_filename = @original_filename.strip

      @content_type = @content.content_type if @content.respond_to?(:content_type)
      @content_type ||= "text/html"

      @size = @content.size
    end

    def copy_to_tempfile(src)
      while data = src.read(16*1024)
        destination.write(data)
      end
      src.close
      destination.rewind
      destination
    end
  end
end

Paperclip.io_adapters.register Paperclip::UriAdapter do |target|
  target.kind_of?(URI)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
paperclip-5.0.0 lib/paperclip/io_adapters/uri_adapter.rb