Sha256: 0502b8e585c6eeddb494c467c9b080401d367078d9deea011340a32725c646fb

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

module Paperclip
  class AttachmentAdapter
    def initialize(target)
      @target = target
      cache_current_values
    end

    def original_filename
      @original_filename
    end

    def content_type
      @content_type
    end

    def size
      @size
    end

    def nil?
      false
    end

    def fingerprint
      @fingerprint ||= Digest::MD5.file(path).to_s
    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 cache_current_values
      @tempfile = copy_to_tempfile(@target)
      @original_filename = @target.original_filename
      @content_type = @target.content_type
      @size = @tempfile.size || @target.size
    end

    def copy_to_tempfile(src)
      dest = Tempfile.new(src.original_filename)
      dest.binmode
      if src.respond_to? :copy_to_local_file
        src.copy_to_local_file(:original, dest.path)
      else
        FileUtils.cp(src.path(:original), dest.path)
      end
      dest
    end
  end
end

Paperclip.io_adapters.register Paperclip::AttachmentAdapter do |target|
  Paperclip::Attachment === target
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cloudfuji_paperclip-3.0.3 lib/paperclip/io_adapters/attachment_adapter.rb