diff --git a/app/helpers/delayed_jobs_helper.rb b/app/helpers/delayed_jobs_helper.rb
index 8b90b3512..13d75c5f3 100644
--- a/app/helpers/delayed_jobs_helper.rb
+++ b/app/helpers/delayed_jobs_helper.rb
@@ -61,6 +61,9 @@ module DelayedJobsHelper
when "Pool#update_category_pseudo_tags_for_posts"
"update pool category pseudo tags for posts"
+ when "Post.delete_files"
+ "delete old files"
+
else
h(job.name)
end
@@ -122,6 +125,9 @@ module DelayedJobsHelper
when "Pool#update_category_pseudo_tags_for_posts"
%{#{h(job.payload_object.name)}}
+ when "Post.delete_files"
+ %{post ##{job.payload_object.args.first}}
+
else
h(job.handler)
end
diff --git a/app/models/post.rb b/app/models/post.rb
index 9fb84a4e7..9de7bbc0b 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -7,6 +7,8 @@ class Post < ActiveRecord::Base
class RevertError < Exception ; end
class SearchError < Exception ; end
+ DELETION_GRACE_PERIOD = 3.days
+
before_validation :initialize_uploader, :on => :create
before_validation :merge_old_changes
before_validation :normalize_tags
@@ -29,7 +31,6 @@ class Post < ActiveRecord::Base
after_save :expire_essential_tag_string_cache
after_destroy :remove_iqdb_async
after_destroy :delete_files
- after_destroy :delete_remote_files
after_commit :update_iqdb_async, :on => :create
after_commit :notify_pubsub
@@ -60,24 +61,31 @@ class Post < ActiveRecord::Base
attr_accessor :old_tag_string, :old_parent_id, :old_source, :old_rating, :has_constraints, :disable_versioning, :view_count
module FileMethods
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+ def delete_files(post_id, file_path, large_file_path, preview_file_path)
+ # the large file and the preview don't necessarily exist. if so errors will be ignored.
+ FileUtils.rm_f(file_path)
+ FileUtils.rm_f(large_file_path)
+ FileUtils.rm_f(preview_file_path)
+
+ RemoteFileManager.new(file_path).delete
+ RemoteFileManager.new(large_file_path).delete
+ RemoteFileManager.new(preview_file_path).delete
+ end
+ end
+
+ def delete_files
+ Post.delete_files(id, file_path, large_file_path, preview_file_path)
+ end
+
def distribute_files
RemoteFileManager.new(file_path).distribute
RemoteFileManager.new(preview_file_path).distribute if has_preview?
RemoteFileManager.new(large_file_path).distribute if has_large?
end
- def delete_remote_files
- RemoteFileManager.new(file_path).delete
- RemoteFileManager.new(preview_file_path).delete if has_preview?
- RemoteFileManager.new(large_file_path).delete if has_large?
- end
-
- def delete_files
- FileUtils.rm_f(file_path)
- FileUtils.rm_f(large_file_path)
- FileUtils.rm_f(preview_file_path)
- end
-
def file_path_prefix
Rails.env == "test" ? "test." : ""
end
@@ -1400,6 +1408,11 @@ class Post < ActiveRecord::Base
upload.process_upload
upload.update(status: "completed", post_id: id)
+ # queue the deletion *before* updating the post so that we use the old
+ # md5/file_ext to delete the old files. if saving the post fails,
+ # this is rolled back so the job won't run.
+ Post.delay(queue: "default", run_at: Time.now + DELETION_GRACE_PERIOD).delete_files(id, file_path, large_file_path, preview_file_path)
+
self.md5 = upload.md5
self.file_ext = upload.file_ext
self.image_width = upload.image_width