Sha256: d088527c1328d275937f4fbd5c01abea1a961b5b442cc7b90a25dcc2f9e049af

Contents?: true

Size: 1.74 KB

Versions: 24

Compression:

Stored size: 1.74 KB

Contents

# Provides method that can be included on File-type objects (IO, StringIO, Tempfile, etc) to allow stream copying
# and Tempfile conversion.
module IOStream

  # Returns a Tempfile containing the contents of the readable object.
  def to_tempfile
    name = respond_to?(:original_filename) ? original_filename : (respond_to?(:path) ? path : "stream")
    tempfile = Paperclip::Tempfile.new(File.basename(name))
    tempfile.binmode
    self.stream_to(tempfile)
  end

  # 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.
  def stream_to path_or_file, in_blocks_of = 8192
    dstio = case path_or_file
            when String   then File.new(path_or_file, "wb+")
            when IO       then path_or_file
            when Tempfile then path_or_file
            end
    buffer = ""
    self.rewind
    while self.read(in_blocks_of, buffer) do
      dstio.write(buffer)
    end
    dstio.rewind    
    dstio
  end
end

class IO #:nodoc:
  include IOStream
end

%w( Tempfile StringIO ).each do |klass|
  if Object.const_defined? klass
    Object.const_get(klass).class_eval do
      include IOStream
    end
  end
end

# Corrects a bug in Windows when asking for Tempfile size.
if defined? Tempfile
  class Tempfile
    def size
      if @tmpfile
        @tmpfile.fsync
        @tmpfile.flush
        @tmpfile.stat.size
      else
        0
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 10 rubygems

Version Path
bteitelb-paperclip-2.3.1.16 lib/paperclip/iostream.rb
bteitelb-paperclip-2.3.1.15 lib/paperclip/iostream.rb
paperclip-hacked-2.3.1.3 lib/paperclip/iostream.rb
paperclip-hacked-2.3.1.2 lib/paperclip/iostream.rb
paperclip-hacked-2.3.1.1 lib/paperclip/iostream.rb
paperclip-cloudfiles-2.3.1.1.6 lib/paperclip/iostream.rb
paperclip-cloudfiles-2.3.1.1.5 lib/paperclip/iostream.rb
mwd-paperclip-2.3.1.1b lib/paperclip/iostream.rb
mwd-paperclip-2.3.1.1.mwd.b lib/paperclip/iostream.rb
mwd-paperclip-2.3.1.1 lib/paperclip/iostream.rb
alg-paperclip-2.3.1.2 lib/paperclip/iostream.rb
bteitelb-paperclip-2.3.1.13 lib/paperclip/iostream.rb
bteitelb-paperclip-2.3.1.12 lib/paperclip/iostream.rb
alg-paperclip-2.3.1.1 lib/paperclip/iostream.rb
paperclip-cloudfiles-2.3.1.1.4 lib/paperclip/iostream.rb
paperclip-cloudfiles-2.3.1.1.3 lib/paperclip/iostream.rb
paperclip-cloudfiles-2.3.1.1.2 lib/paperclip/iostream.rb
paperclip-cloudfiles-2.3.1.1.1 lib/paperclip/iostream.rb
tristandunn-paperclip-2.3.1.1 lib/paperclip/iostream.rb
monde-paperclip-2.3.1.1 lib/paperclip/iostream.rb