posts: mark media asset as expunged when post is expunged.
Fix it so that when a post is expunged, the media asset is also marked as expunged. This way the files will be deleted, but the media asset will still remain as a record of what was expunged. The media asset will have the md5, width, height, file ext, and file size of the deleted file.
This commit is contained in:
@@ -179,6 +179,14 @@ class MediaAsset < ApplicationRecord
|
||||
self.pixiv_ugoira_frame_data = PixivUgoiraFrameData.new(data: media_file.frame_data, content_type: "image/jpeg") if is_ugoira?
|
||||
end
|
||||
|
||||
def expunge!
|
||||
delete_files!
|
||||
update!(status: :expunged)
|
||||
rescue
|
||||
update!(status: :failed)
|
||||
raise
|
||||
end
|
||||
|
||||
def delete_files!
|
||||
variants.each(&:delete_file!)
|
||||
end
|
||||
|
||||
@@ -32,7 +32,6 @@ class Post < ApplicationRecord
|
||||
after_save :create_version
|
||||
after_save :update_parent_on_save
|
||||
after_save :apply_post_metatags
|
||||
after_commit :delete_files, :on => :destroy
|
||||
|
||||
belongs_to :approver, class_name: "User", optional: true
|
||||
belongs_to :uploader, :class_name => "User", :counter_cache => "post_upload_count"
|
||||
@@ -75,30 +74,6 @@ class Post < ApplicationRecord
|
||||
module FileMethods
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
module ClassMethods
|
||||
def delete_files(post_id, md5, file_ext, force: false)
|
||||
if Post.exists?(md5: md5) && !force
|
||||
raise DeletionError, "Files still in use; skipping deletion."
|
||||
end
|
||||
|
||||
Danbooru.config.storage_manager.delete_file(post_id, md5, file_ext, :original)
|
||||
Danbooru.config.storage_manager.delete_file(post_id, md5, file_ext, :large)
|
||||
Danbooru.config.storage_manager.delete_file(post_id, md5, file_ext, :preview)
|
||||
|
||||
Danbooru.config.backup_storage_manager.delete_file(post_id, md5, file_ext, :original)
|
||||
Danbooru.config.backup_storage_manager.delete_file(post_id, md5, file_ext, :large)
|
||||
Danbooru.config.backup_storage_manager.delete_file(post_id, md5, file_ext, :preview)
|
||||
end
|
||||
end
|
||||
|
||||
def delete_files
|
||||
Post.delete_files(id, md5, file_ext, force: true)
|
||||
end
|
||||
|
||||
def backup_storage_manager
|
||||
Danbooru.config.backup_storage_manager
|
||||
end
|
||||
|
||||
def storage_manager
|
||||
Danbooru.config.storage_manager
|
||||
end
|
||||
@@ -815,6 +790,7 @@ class Post < ApplicationRecord
|
||||
decrement_tag_post_counts
|
||||
remove_from_all_pools
|
||||
remove_from_fav_groups
|
||||
media_asset.expunge!
|
||||
destroy
|
||||
update_parent_on_destroy
|
||||
end
|
||||
|
||||
@@ -25,8 +25,7 @@ FactoryBot.define do
|
||||
image_height { media_file.height }
|
||||
file_ext { media_file.file_ext }
|
||||
file_size { media_file.file_size }
|
||||
|
||||
before(:create) { |post, evaluator| MediaAsset.upload!(evaluator.media_file) }
|
||||
media_asset { MediaAsset.upload!(media_file) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -47,6 +47,12 @@ class PostTest < ActiveSupport::TestCase
|
||||
assert_raise(StandardError) { @post.file(:original) }
|
||||
end
|
||||
|
||||
should "mark the media asset as expunged" do
|
||||
@post.expunge!
|
||||
|
||||
assert_equal("expunged", @post.media_asset.status)
|
||||
end
|
||||
|
||||
should "remove all favorites" do
|
||||
@post.expunge!
|
||||
|
||||
|
||||
Reference in New Issue
Block a user