Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding tests and logic to ensure that tempfiles are closed and unlink…

…ed after queued_for_writes finishes uploading/saving
  • Loading branch information...
commit 7696b88db1f1be8a3f260f5639544d8acbf9b219 1 parent 7478455
@danielevans danielevans authored
View
8 lib/paperclip/attachment.rb
@@ -420,5 +420,13 @@ def flush_errors #:nodoc:
end
end
+ # called by storage after the writes are flushed and before @queued_for_writes is cleared
+ def after_flush_writes
+ @queued_for_write.each do |style, file|
+ file.close unless file.closed?
+ file.unlink if file.respond_to?(:unlink) && File.exist?(file.path)
+ end
+ end
+
end
end
View
3  lib/paperclip/storage/filesystem.rb
@@ -46,6 +46,9 @@ def flush_writes #:nodoc:
end
FileUtils.chmod(0666&~File.umask, path(style_name))
end
+
+ after_flush_writes # allows attachment to clean up temp files
+
@queued_for_write = {}
end
View
3  lib/paperclip/storage/fog.rb
@@ -82,6 +82,9 @@ def flush_writes
retry
end
end
+
+ after_flush_writes # allows attachment to clean up temp files
+
@queued_for_write = {}
end
View
3  lib/paperclip/storage/s3.rb
@@ -196,6 +196,9 @@ def flush_writes #:nodoc:
raise
end
end
+
+ after_flush_writes # allows attachment to clean up temp files
+
@queued_for_write = {}
end
View
8 test/fog_test.rb
@@ -24,6 +24,14 @@ class FogTest < Test::Unit::TestCase
assert_equal File.expand_path(File.join(File.dirname(__FILE__), "../public/avatars/5k.png")),
@dummy.avatar.path
end
+
+ should "clean up file objects" do
+ File.stubs(:exist?).returns(true)
+ Paperclip::Tempfile.any_instance.expects(:close).at_least_once()
+ Paperclip::Tempfile.any_instance.expects(:unlink).at_least_once()
+
+ @dummy.save!
+ end
end
setup do
View
17 test/storage_test.rb
@@ -29,6 +29,14 @@ def rails_env(env)
@dummy.save
assert File.exists?(@dummy.avatar.path(:thumbnail))
end
+
+ should "clean up file objects" do
+ File.stubs(:exist?).returns(true)
+ Paperclip::Tempfile.any_instance.expects(:close).at_least_once()
+ Paperclip::Tempfile.any_instance.expects(:unlink).at_least_once()
+
+ @dummy.save!
+ end
end
context "Parsing S3 credentials" do
@@ -349,6 +357,15 @@ def rails_env(env)
end
end
+ should "delete tempfiles" do
+ AWS::S3::S3Object.stubs(:store).with(@dummy.avatar.path, anything, 'testing', :content_type => 'image/png', :access => :public_read)
+ File.stubs(:exist?).returns(true)
+ Paperclip::Tempfile.any_instance.expects(:close).at_least_once()
+ Paperclip::Tempfile.any_instance.expects(:unlink).at_least_once()
+
+ @dummy.save!
+ end
+
context "and saved without a bucket" do
setup do
class AWS::S3::NoSuchBucket < AWS::S3::ResponseError
Please sign in to comment.
Something went wrong with that request. Please try again.