Module | IOStream |
In: |
lib/dm-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. If this module is included in both StringIO and Tempfile, then either can have its data copied anywhere else without typing worries or memory overhead worries. 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/dm-paperclip/iostream.rb, line 18 18: def stream_to path_or_file, in_blocks_of = 8192 19: dstio = case path_or_file 20: when String then File.new(path_or_file, "wb+") 21: when IO then path_or_file 22: when Tempfile then path_or_file 23: end 24: buffer = "" 25: self.rewind 26: while self.read(in_blocks_of, buffer) do 27: dstio.write(buffer) 28: end 29: dstio.rewind 30: dstio 31: end
Returns a Tempfile containing the contents of the readable object.
# File lib/dm-paperclip/iostream.rb, line 6 6: def to_tempfile 7: name = respond_to?(:original_filename) ? original_filename : (respond_to?(:path) ? path : "stream") 8: tempfile = Paperclip::Tempfile.new(File.basename(name)) 9: tempfile.binmode 10: self.stream_to(tempfile) 11: end