Created
November 7, 2009 19:01
-
-
Save softcraft-development/228845 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fileutils' | |
include FileUtils | |
require 'pathname' | |
namespace :paperclip do | |
desc "Remove Paperclip attachment directories with no entity" | |
task :gc => :environment do | |
current_trash = ((Pathname.new trash) + (Pathname.new Time.now.strftime("%Y-%m-%d.%H:%M:%S"))).cleanpath | |
# This algorithm assumes that the attachments have a :path starting with "#{IMAGES_ROOT}/[class name]/:attachment/:id/" | |
# This matches my setup, but may not match yours. | |
# I wanted to get the attachment-dir-finding algorithm to be implementation-independent, but | |
# it ended up being more time consuming than I wanted. | |
# Change this to match your setup. If you get universal algorithm, please contact me. Thanks! | |
klass = obtain_class.name.downcase | |
klass_root = (Pathname.new "#{IMAGES_ROOT}/#{klass}").realpath | |
Dir.glob("#{klass_root.to_s}/*/*/").each do |filename| | |
path = Pathname.new filename | |
name = path.basename.to_s | |
if !name.starts_with? "." | |
begin | |
photo = Photo.find name | |
rescue ActiveRecord::RecordNotFound | |
klass_trash = current_trash + (path.relative_path_from Pathname.new IMAGES_ROOT) | |
mkdir_p klass_trash.to_s | |
# p "#{path.to_s}, #{klass_trash.to_s}" | |
mv path.to_s, klass_trash.to_s, :force => true, :verbose => true | |
end | |
end | |
end | |
end | |
namespace :trash do | |
desc "Empty the paperclip attachment trash" | |
task :empty => :environment do | |
rm_rf trash, :verbose => true | |
end | |
end | |
end | |
def trash | |
# Change this as appropriate | |
File.expand_path "#{IMAGES_ROOT}/trash" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment