Permalink
Browse files

Merge pull request #346 from clyfe/master

delete cache garbage dirs issue #338
  • Loading branch information...
2 parents 5289df7 + 43ce339 commit 5c8e345c788ddd7fefaf54a3521e9c02d3477139 @bensie bensie committed Jun 8, 2011
Showing with 31 additions and 0 deletions.
  1. +2 −0 lib/carrierwave/uploader/configuration.rb
  2. +11 −0 lib/carrierwave/uploader/store.rb
  3. +18 −0 spec/uploader/store_spec.rb
@@ -37,6 +37,7 @@ module Configuration
add_config :enable_processing
add_config :ensure_multipart_form
add_config :delete_tmp_file_after_storage
+ add_config :delete_cache_id_after_storage
add_config :remove_previously_stored_files_after_update
# fog
@@ -144,6 +145,7 @@ def reset_config
config.store_dir = 'uploads'
config.cache_dir = 'uploads/tmp'
config.delete_tmp_file_after_storage = true
+ config.delete_cache_id_after_storage = true
config.remove_previously_stored_files_after_update = true
config.ignore_integrity_errors = true
config.ignore_processing_errors = true
@@ -58,11 +58,22 @@ def store!(new_file=nil)
with_callbacks(:store, new_file) do
new_file = storage.store!(@file)
@file.delete if delete_tmp_file_after_storage
+ delete_cache_id if delete_cache_id_after_storage
@file = new_file
@cache_id = nil
end
end
end
+
+ ##
+ # Deletes a cache id (tmp dir in cache)
+ #
+ def delete_cache_id
+ if @cache_id
+ path = File.join(cache_dir, @cache_id)
+ FileUtils.rm_rf(path) if File.exists?(path) && File.directory?(path)
+ end
+ end
##
# Retrieves the file from the storage.
@@ -105,6 +105,24 @@
@uploader.store!
end
end
+
+ it "should delete the old cache_id" do
+ @uploader.cache!(@file)
+ @uploader.should_receive(:delete_cache_id)
+ @uploader.store!
+ end
+
+ context "with the delete_cache_id_after_storage option set to false" do
+ before do
+ @uploader_class.delete_cache_id_after_storage = false
+ end
+
+ it "should not delete the old cache_id" do
+ @uploader.cache!(@file)
+ @uploader.should_not_receive(:delete_cache_id)
+ @uploader.store!
+ end
+ end
it "should do nothing when trying to store an empty file" do
@uploader.store!(nil)

0 comments on commit 5c8e345

Please sign in to comment.