diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb index 9f4149d2ae2..d745bc2d1b6 100644 --- a/app/models/optimized_image.rb +++ b/app/models/optimized_image.rb @@ -36,6 +36,18 @@ class OptimizedImage < ActiveRecord::Base thumbnail end + def destroy + OptimizedImage.transaction do + remove_file + super + end + end + + def remove_file + File.delete path + rescue Errno::ENOENT + end + def url "#{Upload.base_url}/#{optimized_path}/#{filename}" end diff --git a/app/models/upload.rb b/app/models/upload.rb index f935e924101..6257f0653ea 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -11,7 +11,7 @@ class Upload < ActiveRecord::Base has_many :post_uploads has_many :posts, through: :post_uploads - has_many :optimized_images + has_many :optimized_images, dependent: :destroy validates_presence_of :filesize validates_presence_of :original_filename @@ -36,7 +36,7 @@ class Upload < ActiveRecord::Base optimized_images << thumbnail if thumbnail end - def delete + def destroy Upload.transaction do Upload.remove_file url super diff --git a/lib/tasks/images.rake b/lib/tasks/images.rake index 4fcac3e67e9..c8d15eebc89 100644 --- a/lib/tasks/images.rake +++ b/lib/tasks/images.rake @@ -43,7 +43,7 @@ task "images:clean_orphans" => :environment do Upload.joins("LEFT OUTER JOIN post_uploads ON uploads.id = post_uploads.upload_id") .where("post_uploads.upload_id IS NULL") .find_each do |u| - u.delete + u.destroy putc "." end end