Skip to content

Commit

Permalink
extract directory cleaning logic to AttachmentFu.cleanup_directory
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Mar 22, 2010
1 parent 317c4e8 commit e90425d
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions lib/attachment_fu.rb
Expand Up @@ -150,6 +150,21 @@ def self.reset
create_task :get_image_size, "attachment_fu/tasks/resize"
end

def self.cleanup_directory(root, file)
FileUtils.rm file if File.exist?(file)
dir_name = File.dirname(file)
default = %w(. ..)
while dir_name != root
dir_exists = File.exists?(dir_name)
if !dir_exists || (Dir.entries(dir_name) - default).empty?
FileUtils.rm_rf(dir_name) if dir_exists
dir_name.sub! /\/\w+$/, ''
else
dir_name = root
end
end
end

# This mixin is included in attachment classes by AttachmentFu::SetupMethods.is_attachment.
module InstanceMethods
# Strips filename of any funny characters.
Expand Down Expand Up @@ -342,20 +357,8 @@ def check_task_progress(stack)
# the empty asset paths.
def delete_attachment
fp = full_path
fa = full_attachment_path
return if fp.blank?
FileUtils.rm fp if File.exist?(fp)
dir_name = File.dirname(fp)
default = %w(. ..)
while dir_name != fa
dir_exists = File.exists?(dir_name)
if !dir_exists || (Dir.entries(dir_name) - default).empty?
FileUtils.rm_rf(dir_name) if dir_exists
dir_name.sub! /\/\w+$/, ''
else
dir_name = fa
end
end
AttachmentFu.cleanup_directory(full_attachment_path, fp)
end

def rename_attachment
Expand Down

0 comments on commit e90425d

Please sign in to comment.