Sha256: 5bf3005287f9aaea6911cd44564603cb4e58ff871ba7aae3881467df9052563e

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 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

    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
      FileUtils.cp(src.path(:original), dest.path)
      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
paperclip-3.0.2 lib/paperclip/io_adapters/attachment_adapter.rb