Sha256: 31591482422810eefc92c2b0c57f0fff4ea9ba8eb1801b8eef698fe0a6486abd

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module Paperclip
  class UploadedFileAdapter
    def initialize(target)
      @target = target

      if @target.respond_to?(:tempfile)
        @tempfile = copy_to_tempfile(@target.tempfile)
      else
        @tempfile = copy_to_tempfile(@target)
      end
    end

    def original_filename
      @target.original_filename
    end

    def content_type
      @target.content_type
    end

    def fingerprint
      @fingerprint ||= Digest::MD5.file(path).to_s
    end

    def size
      File.size(path)
    end

    def nil?
      false
    end

    def read(length = nil, buffer = nil)
      @tempfile.read(length, buffer)
    end

    # We don't use this directly, but aws/sdk does.
    def rewind
      @tempfile.rewind
    end

    def eof?
      @tempfile.eof?
    end

    def path
      @tempfile.path
    end

    private

    def copy_to_tempfile(src)
      dest = Tempfile.new(original_filename)
      dest.binmode
      FileUtils.cp(src.path, dest.path)
      dest
    end
  end
end

Paperclip.io_adapters.register Paperclip::UploadedFileAdapter do |target|
  target.class.name.include?("UploadedFile")
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
cloudfuji_paperclip-3.0.3 lib/paperclip/io_adapters/uploaded_file_adapter.rb
paperclip-3.0.2 lib/paperclip/io_adapters/uploaded_file_adapter.rb