Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

72 lines (58 sloc) 1.562 kb
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
if (@cached_fingerprint.nil?)
rewind # start reading from the beginning
@cached_fingerprint = Digest::MD5.hexdigest(read)
rewind # for later read()
end
@cached_fingerprint
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
Jump to Line
Something went wrong with that request. Please try again.