Permalink
Browse files

Close and unlink tempfiles after flush write

Fixes #902
  • Loading branch information...
sikachu committed Jun 15, 2012
1 parent d9de7ba commit b09bb31b8e74ee8647417c39ce8bb05c3fd3cde7
@@ -440,6 +440,10 @@ def flush_errors #:nodoc:
# 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.path.present? && File.exist?(file.path)
+ end
end
def cleanup_filename(filename)
@@ -3,7 +3,7 @@
module Paperclip
class AbstractAdapter
attr_reader :content_type, :original_filename, :size
- delegate :close, :closed?, :eof?, :path, :rewind, :to => :@tempfile
+ delegate :close, :closed?, :eof?, :path, :rewind, :unlink, :to => :@tempfile
def fingerprint
@fingerprint ||= Digest::MD5.file(path).to_s
@@ -33,7 +33,7 @@ def content_type
@adapter.tempfile = stub("Tempfile")
end
- [:close, :closed?, :eof?, :path, :rewind].each do |method|
+ [:close, :closed?, :eof?, :path, :rewind, :unlink].each do |method|
should "delegate #{method} to @tempfile" do
@adapter.tempfile.stubs(method)
@adapter.public_send(method)
@@ -63,14 +63,16 @@ def setup
@attachment.assign(@file)
- @thumb = @attachment.queued_for_write[:thumb]
+ @thumb = Tempfile.new("thumbnail").tap(&:binmode)
+ FileUtils.cp @attachment.queued_for_write[:thumb].path, @thumb.path
@attachment.save
@subject = Paperclip.io_adapters.for(@attachment.styles[:thumb])
end
teardown do
@file.close
+ @thumb.close
end
should "get the original filename" do
@@ -28,10 +28,19 @@ class FileSystemTest < Test::Unit::TestCase
end
should "be rewinded after flush_writes" do
- files = @dummy.avatar.queued_for_write.map{ |style, file| file }
+ @dummy.avatar.instance_eval "def after_flush_writes; end"
+
+ files = @dummy.avatar.queued_for_write.values
@dummy.save
assert files.none?(&:eof?), "Expect all the files to be rewinded."
end
+
+ should "be removed after after_flush_writes" do
+ paths = @dummy.avatar.queued_for_write.values.map(&:path)
+ @dummy.save
+ assert paths.none?{ |path| File.exists?(path) },
+ "Expect all the files to be deleted."
+ end
end
context "with file that has space in file name" do
View
@@ -112,11 +112,20 @@ class FogTest < Test::Unit::TestCase
end
should "be rewinded after flush_writes" do
- files = @dummy.avatar.queued_for_write.map{ |style, file| file }
+ @dummy.avatar.instance_eval "def after_flush_writes; end"
+
+ files = @dummy.avatar.queued_for_write.values
@dummy.save
assert files.none?(&:eof?), "Expect all the files to be rewinded."
end
+ should "be removed after after_flush_writes" do
+ paths = @dummy.avatar.queued_for_write.values.map(&:path)
+ @dummy.save
+ assert paths.none?{ |path| File.exists?(path) },
+ "Expect all the files to be deleted."
+ 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]
View
@@ -586,11 +586,20 @@ def counter
end
should "be rewinded after flush_writes" do
- files = @dummy.avatar.queued_for_write.map{ |style, file| file.tap(&:read) }
+ @dummy.avatar.instance_eval "def after_flush_writes; end"
+
+ files = @dummy.avatar.queued_for_write.values.each(&:read)
@dummy.save
assert files.none?(&:eof?), "Expect all the files to be rewinded."
end
+ should "be removed after after_flush_writes" do
+ paths = @dummy.avatar.queued_for_write.values.map(&:path)
+ @dummy.save
+ assert paths.none?{ |path| File.exists?(path) },
+ "Expect all the files to be deleted."
+ end
+
context "and saved" do
setup do
object = stub

0 comments on commit b09bb31

Please sign in to comment.