posts: move expunged image to trash folder.
When a post is expunged, move the image to a trash folder so it can be recovered if needed.
This commit is contained in:
@@ -50,6 +50,24 @@ class StorageManager
|
|||||||
raise NotImplementedError, "open not implemented"
|
raise NotImplementedError, "open not implemented"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Copy the file from src_path to dest_path.
|
||||||
|
#
|
||||||
|
# @param src_path [String] The file to copy.
|
||||||
|
# @param dest_path [String] The location to copy the file to.
|
||||||
|
def copy(src_path, dest_path)
|
||||||
|
file = open(src_path)
|
||||||
|
store(file, dest_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Move the file from src_path to dest_path.
|
||||||
|
#
|
||||||
|
# @param src_path [String] The file to move.
|
||||||
|
# @param dest_path [String] The location to move the file to.
|
||||||
|
def move(src_path, dest_path)
|
||||||
|
copy(src_path, dest_path)
|
||||||
|
delete(src_path)
|
||||||
|
end
|
||||||
|
|
||||||
# Return the full URL of the file at the given path, or nil if the file
|
# Return the full URL of the file at the given path, or nil if the file
|
||||||
# doesn't have an URL.
|
# doesn't have an URL.
|
||||||
# @return [String, nil] the file URL
|
# @return [String, nil] the file URL
|
||||||
|
|||||||
@@ -24,8 +24,7 @@ class MediaAsset < ApplicationRecord
|
|||||||
|
|
||||||
# Processing: The asset's files are currently being resized and distributed to the backend servers.
|
# Processing: The asset's files are currently being resized and distributed to the backend servers.
|
||||||
# Active: The asset has been successfully uploaded and is ready to use.
|
# Active: The asset has been successfully uploaded and is ready to use.
|
||||||
# Deleted: The asset's files have been deleted by moving them to a trash folder. They can be undeleted
|
# Deleted: The asset's files have been deleted by moving them to a trash folder. They can be undeleted by moving them out of the trash folder.
|
||||||
# by moving them out of the trash folder. (Not implemented yet).
|
|
||||||
# Expunged: The asset's files have been permanently deleted.
|
# Expunged: The asset's files have been permanently deleted.
|
||||||
# Failed: The asset failed to upload. The asset may be in a partially uploaded state, with some
|
# Failed: The asset failed to upload. The asset may be in a partially uploaded state, with some
|
||||||
# files missing or incompletely transferred.
|
# files missing or incompletely transferred.
|
||||||
@@ -62,6 +61,11 @@ class MediaAsset < ApplicationRecord
|
|||||||
backup_storage_service.store(file, file_path)
|
backup_storage_service.store(file, file_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def trash_file!
|
||||||
|
storage_service.move(file_path, "/trash/#{file_path}")
|
||||||
|
backup_storage_service.move(file_path, "/trash/#{file_path}")
|
||||||
|
end
|
||||||
|
|
||||||
def delete_file!
|
def delete_file!
|
||||||
storage_service.delete(file_path)
|
storage_service.delete(file_path)
|
||||||
backup_storage_service.delete(file_path)
|
backup_storage_service.delete(file_path)
|
||||||
@@ -276,6 +280,14 @@ class MediaAsset < ApplicationRecord
|
|||||||
raise
|
raise
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def trash!
|
||||||
|
variants.each(&:trash_file!)
|
||||||
|
update!(status: :deleted)
|
||||||
|
rescue
|
||||||
|
update!(status: :failed)
|
||||||
|
raise
|
||||||
|
end
|
||||||
|
|
||||||
def delete_files!
|
def delete_files!
|
||||||
variants.each(&:delete_file!)
|
variants.each(&:delete_file!)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -756,7 +756,7 @@ class Post < ApplicationRecord
|
|||||||
decrement_tag_post_counts
|
decrement_tag_post_counts
|
||||||
remove_from_all_pools
|
remove_from_all_pools
|
||||||
remove_from_fav_groups
|
remove_from_fav_groups
|
||||||
media_asset.expunge!
|
media_asset.trash!
|
||||||
destroy
|
destroy
|
||||||
update_parent_on_destroy
|
update_parent_on_destroy
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -46,10 +46,10 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
assert_raise(StandardError) { @post.file(:original) }
|
assert_raise(StandardError) { @post.file(:original) }
|
||||||
end
|
end
|
||||||
|
|
||||||
should "mark the media asset as expunged" do
|
should "mark the media asset as deleted" do
|
||||||
@post.expunge!
|
@post.expunge!
|
||||||
|
|
||||||
assert_equal("expunged", @post.media_asset.status)
|
assert_equal("deleted", @post.media_asset.status)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "remove all favorites" do
|
should "remove all favorites" do
|
||||||
@@ -199,16 +199,16 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
context "Expunging a post with" do
|
context "Expunging a post with" do
|
||||||
context "a parent" do
|
context "a parent" do
|
||||||
should "reset the has_children flag of the parent" do
|
should "reset the has_children flag of the parent" do
|
||||||
p1 = FactoryBot.create(:post)
|
p1 = create(:post)
|
||||||
c1 = FactoryBot.create(:post, :parent_id => p1.id)
|
c1 = create(:post_with_file, parent_id: p1.id)
|
||||||
c1.expunge!
|
c1.expunge!
|
||||||
p1.reload
|
p1.reload
|
||||||
assert_equal(false, p1.has_children?)
|
assert_equal(false, p1.has_children?)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "update the parent's has_children flag" do
|
should "update the parent's has_children flag" do
|
||||||
p1 = FactoryBot.create(:post)
|
p1 = create(:post)
|
||||||
c1 = FactoryBot.create(:post, :parent_id => p1.id)
|
c1 = create(:post_with_file, parent_id: p1.id)
|
||||||
c1.expunge!
|
c1.expunge!
|
||||||
p1.reload
|
p1.reload
|
||||||
assert(!p1.has_children?, "Parent should not have children")
|
assert(!p1.has_children?, "Parent should not have children")
|
||||||
@@ -217,8 +217,8 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
context "one child" do
|
context "one child" do
|
||||||
should "remove the parent of that child" do
|
should "remove the parent of that child" do
|
||||||
p1 = FactoryBot.create(:post)
|
p1 = create(:post_with_file)
|
||||||
c1 = FactoryBot.create(:post, :parent_id => p1.id)
|
c1 = create(:post, parent_id: p1.id)
|
||||||
p1.expunge!
|
p1.expunge!
|
||||||
c1.reload
|
c1.reload
|
||||||
assert_nil(c1.parent)
|
assert_nil(c1.parent)
|
||||||
@@ -229,10 +229,10 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
setup do
|
setup do
|
||||||
# ensure initial post versions won't be merged.
|
# ensure initial post versions won't be merged.
|
||||||
travel_to(1.day.ago) do
|
travel_to(1.day.ago) do
|
||||||
@p1 = FactoryBot.create(:post)
|
@p1 = create(:post_with_file)
|
||||||
@c1 = FactoryBot.create(:post, :parent_id => @p1.id)
|
@c1 = create(:post, parent_id: @p1.id)
|
||||||
@c2 = FactoryBot.create(:post, :parent_id => @p1.id)
|
@c2 = create(:post, parent_id: @p1.id)
|
||||||
@c3 = FactoryBot.create(:post, :parent_id => @p1.id)
|
@c3 = create(:post, parent_id: @p1.id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user