post replacement: delete old files after image is replaced.

This commit is contained in:
evazion
2017-05-02 19:47:47 -05:00
parent e2b988a562
commit ca01539c4b
2 changed files with 32 additions and 13 deletions

View File

@@ -61,6 +61,9 @@ module DelayedJobsHelper
when "Pool#update_category_pseudo_tags_for_posts"
"<strong>update pool category pseudo tags for posts</strong>"
when "Post.delete_files"
"<strong>delete old files</strong>"
else
h(job.name)
end
@@ -122,6 +125,9 @@ module DelayedJobsHelper
when "Pool#update_category_pseudo_tags_for_posts"
%{<a href="/pools/#{job.payload_object.id}">#{h(job.payload_object.name)}</a>}
when "Post.delete_files"
%{<a href="/posts/#{job.payload_object.args.first}">post ##{job.payload_object.args.first}</a>}
else
h(job.handler)
end

View File

@@ -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