Module | IOStream |
In: |
lib/paperclip/iostream.rb
|
Copies one read-able object from one place to another in blocks, obviating the need to load the whole thing into memory. Defaults to 8k blocks. Returns a File if a String is passed in as the destination and returns the IO or Tempfile as passed in if one is sent as the destination.
# File lib/paperclip/iostream.rb, line 16 16: def stream_to object, path_or_file, in_blocks_of = 8192 17: dstio = case path_or_file 18: when String then File.new(path_or_file, "wb+") 19: when IO then path_or_file 20: when Tempfile then path_or_file 21: end 22: buffer = "" 23: object.rewind 24: while object.read(in_blocks_of, buffer) do 25: dstio.write(buffer) 26: end 27: dstio.rewind 28: dstio 29: end
Returns a Tempfile containing the contents of the readable object.
# File lib/paperclip/iostream.rb, line 5 5: def to_tempfile(object) 6: return object.to_tempfile if object.respond_to?(:to_tempfile) 7: name = object.respond_to?(:original_filename) ? object.original_filename : (object.respond_to?(:path) ? object.path : "stream") 8: tempfile = Paperclip::Tempfile.new(["stream", File.extname(name)]) 9: tempfile.binmode 10: stream_to(object, tempfile) 11: end