Sha256: 61c5692ec23b9cee0c05ae194513f94ace9b107681e00e0a5feaf2e4dcbe9943

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module Paperclip
  class StringioAdapter
    def initialize(target)
      @target = target
      @tempfile = copy_to_tempfile(@target)
    end

    attr_writer :original_filename, :content_type

    def original_filename
      @original_filename ||= @target.original_filename if @target.respond_to?(:original_filename)
      @original_filename ||= "stringio.txt"
      @original_filename.strip
    end

    def content_type
      @content_type ||= @target.content_type if @target.respond_to?(:content_type)
      @content_type ||= "text/plain"
      @content_type
    end

    def size
      @target.size
    end

    def fingerprint
      Digest::MD5.hexdigest(read)
    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)
      extension = File.extname(original_filename)
      basename = File.basename(original_filename, extension)
      dest = Tempfile.new([basename, extension])
      dest.binmode
      while data = src.read(16*1024)
        dest.write(data)
      end
      dest.rewind
      dest
    end

  end
end

Paperclip.io_adapters.register Paperclip::StringioAdapter do |target|
  StringIO === target
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
paperclip-3.0.4 lib/paperclip/io_adapters/stringio_adapter.rb
paperclip-3.0.3 lib/paperclip/io_adapters/stringio_adapter.rb