Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Don't monkeypatch IO, Tempfile, StringIO.
Browse files Browse the repository at this point in the history
For compatibility with Rails > 3.0.0.
(cherry picked from commit 2e6d337)
  • Loading branch information
nicksieger authored and Jon Yurek committed Oct 12, 2010
1 parent 7a09892 commit ef7233d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 36 deletions.
3 changes: 2 additions & 1 deletion lib/paperclip/attachment.rb
Expand Up @@ -4,6 +4,7 @@ module Paperclip
# when the model saves, deletes when the model is destroyed, and processes # when the model saves, deletes when the model is destroyed, and processes
# the file upon assignment. # the file upon assignment.
class Attachment class Attachment
include IOStream


def self.default_options def self.default_options
@default_options ||= { @default_options ||= {
Expand Down Expand Up @@ -88,7 +89,7 @@ def assign uploaded_file


return nil if uploaded_file.nil? return nil if uploaded_file.nil?


@queued_for_write[:original] = uploaded_file.to_tempfile @queued_for_write[:original] = to_tempfile(uploaded_file)
instance_write(:file_name, uploaded_file.original_filename.strip) instance_write(:file_name, uploaded_file.original_filename.strip)
instance_write(:content_type, uploaded_file.content_type.to_s.strip) instance_write(:content_type, uploaded_file.content_type.to_s.strip)
instance_write(:file_size, uploaded_file.size.to_i) instance_write(:file_size, uploaded_file.size.to_i)
Expand Down
32 changes: 9 additions & 23 deletions lib/paperclip/iostream.rb
@@ -1,48 +1,34 @@
# Provides method that can be included on File-type objects (IO, StringIO, Tempfile, etc) to allow stream copying # Provides method that can be included on File-type objects (IO, StringIO, Tempfile, etc) to allow stream copying
# and Tempfile conversion. # and Tempfile conversion.
module IOStream module IOStream

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


# Copies one read-able object from one place to another in blocks, obviating the need to load # 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 # the whole thing into memory. Defaults to 8k blocks. Returns a File if a String is passed
# StringIO and Tempfile, then either can have its data copied anywhere else without typing # in as the destination and returns the IO or Tempfile as passed in if one is sent as the destination.
# worries or memory overhead worries. Returns a File if a String is passed in as the destination def stream_to object, path_or_file, in_blocks_of = 8192
# 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 dstio = case path_or_file
when String then File.new(path_or_file, "wb+") when String then File.new(path_or_file, "wb+")
when IO then path_or_file when IO then path_or_file
when Tempfile then path_or_file when Tempfile then path_or_file
end end
buffer = "" buffer = ""
self.rewind object.rewind
while self.read(in_blocks_of, buffer) do while object.read(in_blocks_of, buffer) do
dstio.write(buffer) dstio.write(buffer)
end end
dstio.rewind dstio.rewind
dstio dstio
end end
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. # Corrects a bug in Windows when asking for Tempfile size.
if defined? Tempfile if defined? Tempfile
class Tempfile class Tempfile
Expand Down
17 changes: 5 additions & 12 deletions test/iostream_test.rb
@@ -1,14 +1,7 @@
require 'test/helper' require 'test/helper'


class IOStreamTest < Test::Unit::TestCase class IOStreamTest < Test::Unit::TestCase
context "IOStream" do include IOStream
should "be included in IO, File, Tempfile, and StringIO" do
[IO, File, Tempfile, StringIO].each do |klass|
assert klass.included_modules.include?(IOStream), "Not in #{klass}"
end
end
end

context "A file" do context "A file" do
setup do setup do
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb') @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
Expand All @@ -21,7 +14,7 @@ class IOStreamTest < Test::Unit::TestCase
context "and given a String" do context "and given a String" do
setup do setup do
FileUtils.mkdir_p(File.join(ROOT, 'tmp')) FileUtils.mkdir_p(File.join(ROOT, 'tmp'))
assert @result = @file.stream_to(File.join(ROOT, 'tmp', 'iostream.string.test')) assert @result = stream_to(@file, File.join(ROOT, 'tmp', 'iostream.string.test'))
end end


should "return a File" do should "return a File" do
Expand All @@ -38,7 +31,7 @@ class IOStreamTest < Test::Unit::TestCase
setup do setup do
tempfile = Tempfile.new('iostream.test') tempfile = Tempfile.new('iostream.test')
tempfile.binmode tempfile.binmode
assert @result = @file.stream_to(tempfile) assert @result = stream_to(@file, tempfile)
end end


should "return a Tempfile" do should "return a Tempfile" do
Expand All @@ -53,9 +46,9 @@ class IOStreamTest < Test::Unit::TestCase


end end


context "that is sent #to_tempfile" do context "that is converted #to_tempfile" do
setup do setup do
assert @tempfile = @file.to_tempfile assert @tempfile = to_tempfile(@file)
end end


should "convert it to a Paperclip Tempfile" do should "convert it to a Paperclip Tempfile" do
Expand Down

0 comments on commit ef7233d

Please sign in to comment.