From 3b2b9bcf8c1a8e56645c62aed0730883ba0d6364 Mon Sep 17 00:00:00 2001 From: Kelley Reynolds Date: Mon, 6 Feb 2012 12:43:14 -0500 Subject: [PATCH] Added rewind to to_file where appropriate --- lib/paperclip/storage/filesystem.rb | 7 ++++++- lib/paperclip/storage/fog.rb | 1 + lib/paperclip/storage/s3.rb | 5 ++++- test/storage/filesystem_test.rb | 6 ++++++ test/storage/fog_test.rb | 6 ++++++ test/storage/s3_test.rb | 8 +++++++- 6 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/paperclip/storage/filesystem.rb b/lib/paperclip/storage/filesystem.rb index d55ca075a..41d4fb1a3 100644 --- a/lib/paperclip/storage/filesystem.rb +++ b/lib/paperclip/storage/filesystem.rb @@ -30,7 +30,12 @@ def exists?(style_name = default_style) # Returns representation of the data of the file assigned to the given # style, in the format most representative of the current storage. def to_file style_name = default_style - @queued_for_write[style_name] || (File.new(path(style_name), 'rb') if exists?(style_name)) + if @queued_for_write[style_name] + @queued_for_write[style_name].rewind + @queued_for_write[style_name] + elsif exists?(style_name) + File.new(path(style_name), 'rb') + end end def flush_writes #:nodoc: diff --git a/lib/paperclip/storage/fog.rb b/lib/paperclip/storage/fog.rb index 015a56b79..3d7f1b916 100644 --- a/lib/paperclip/storage/fog.rb +++ b/lib/paperclip/storage/fog.rb @@ -110,6 +110,7 @@ def flush_deletes # style, in the format most representative of the current storage. def to_file(style = default_style) if @queued_for_write[style] + @queued_for_write[style].rewind @queued_for_write[style] else body = directory.files.get(path(style)).body diff --git a/lib/paperclip/storage/s3.rb b/lib/paperclip/storage/s3.rb index 4f8c75948..98b99fa95 100644 --- a/lib/paperclip/storage/s3.rb +++ b/lib/paperclip/storage/s3.rb @@ -257,7 +257,10 @@ def s3_protocol(style = default_style) # Returns representation of the data of the file assigned to the given # style, in the format most representative of the current storage. def to_file style = default_style - return @queued_for_write[style] if @queued_for_write[style] + if @queued_for_write[style] + @queued_for_write[style].rewind + return @queued_for_write[style] + end filename = path(style) extname = File.extname(filename) basename = File.basename(filename, extname) diff --git a/test/storage/filesystem_test.rb b/test/storage/filesystem_test.rb index 48a8159c1..704b4b7ad 100644 --- a/test/storage/filesystem_test.rb +++ b/test/storage/filesystem_test.rb @@ -31,6 +31,12 @@ class FileSystemTest < Test::Unit::TestCase @dummy.save! end + should "always be rewound when returning from #to_file" do + assert_equal 0, @dummy.avatar.to_file.pos + @dummy.avatar.to_file.seek(10) + assert_equal 0, @dummy.avatar.to_file.pos + end + context "with file that has space in file name" do setup do rebuild_model :styles => { :thumbnail => "25x25#" } diff --git a/test/storage/fog_test.rb b/test/storage/fog_test.rb index 102ce6c42..f0fc42c85 100644 --- a/test/storage/fog_test.rb +++ b/test/storage/fog_test.rb @@ -110,6 +110,12 @@ class FogTest < Test::Unit::TestCase directory.destroy end + should "always be rewound when returning from #to_file" do + assert_equal 0, @dummy.avatar.to_file.pos + @dummy.avatar.to_file.seek(10) + assert_equal 0, @dummy.avatar.to_file.pos + end + should "pass the content type to the Fog::Storage::AWS::Files instance" do Fog::Storage::AWS::Files.any_instance.expects(:create).with do |hash| hash[:content_type] diff --git a/test/storage/s3_test.rb b/test/storage/s3_test.rb index 3d0b1c85b..df0116afe 100644 --- a/test/storage/s3_test.rb +++ b/test/storage/s3_test.rb @@ -335,7 +335,13 @@ def counter should "return a relative URL for Rails to calculate assets host" do assert_match %r{^avatars/stringio\.txt}, @dummy.avatar.url end - end + + should "always be rewound when returning from #to_file" do + assert_equal 0, @dummy.avatar.to_file.pos + @dummy.avatar.to_file.seek(10) + assert_equal 0, @dummy.avatar.to_file.pos + end + end context "Generating a secure url with an expiration" do setup do