clean up some of the delete code, fix space_used bug (hopefully)

This commit is contained in:
Kyle Drake 2015-10-15 16:37:47 -07:00
parent b98d982e5b
commit 96478b25bb
4 changed files with 56 additions and 32 deletions

View file

@ -2,4 +2,48 @@ class SiteFile < Sequel::Model
unrestrict_primary_key
plugin :update_primary_key
many_to_one :site
def before_destroy
if is_directory
site.site_files_dataset.where(path: /^#{path}\//, is_directory: true).all.each do |site_file|
begin
site_file.destroy
rescue Sequel::NoExistingObject
end
end
site.site_files_dataset.where(path: /^#{path}\//, is_directory: false).all.each do |site_file|
site_file.destroy
end
begin
FileUtils.remove_dir site.files_path(path)
rescue Errno::ENOENT
end
else
begin
FileUtils.rm site.files_path(path)
rescue Errno::ENOENT
end
ext = File.extname(path).gsub(/^./, '')
site.screenshots_delete(path) if ext.match Site::HTML_REGEX
site.thumbnails_delete(path) if ext.match Site::IMAGE_REGEX
end
super
end
def after_destroy
super
unless is_directory
DB['update sites set space_used=space_used-? where id=?', size, site_id].first
end
site.delete_cache site.files_path(path)
SiteChangeFile.filter(site_id: site_id, filename: path).delete
end
end