Navigation Menu

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

Commit

Permalink
Added rewind to to_file where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelley Reynolds committed Feb 6, 2012
1 parent 772c8e9 commit 3b2b9bc
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/paperclip/storage/filesystem.rb
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions lib/paperclip/storage/fog.rb
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/paperclip/storage/s3.rb
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions test/storage/filesystem_test.rb
Expand Up @@ -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#" }
Expand Down
6 changes: 6 additions & 0 deletions test/storage/fog_test.rb
Expand Up @@ -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]
Expand Down
8 changes: 7 additions & 1 deletion test/storage/s3_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 3b2b9bc

Please sign in to comment.