Skip to content

Commit

Permalink
Merge pull request #5939 from avalonmediasystem/child_file_delete
Browse files Browse the repository at this point in the history
Delete supplemental files when parent object is deleted
  • Loading branch information
masaball authored Aug 27, 2024
2 parents cf83eb0 + 68581f3 commit fd5f15b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/jobs/bulk_action_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def perform(documents, _params)
successes = []
documents.each do |id|
media_object = MediaObject.find(id)
supplemental_files = media_object.supplemental_files
DeleteChildFiles.perform_now(supplemental_files, nil)

if media_object.destroy
successes += [media_object]
else
Expand All @@ -144,6 +147,19 @@ def perform(documents, _params)
end
end

class DeleteChildFiles < ActiveJob::Base
def perform(documents, _params)
documents.each do |doc|
begin
doc.destroy
rescue
logger.error("Failed to delete supplemental file #{doc.id}")
next
end
end
end
end

class Move < ActiveJob::Base
def perform(documents, params)
collection = Admin::Collection.find( params[:target_collection_id] )
Expand Down
11 changes: 11 additions & 0 deletions app/models/master_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def error
after_save :update_stills_from_offset!, if: Proc.new { |mf| mf.previous_changes.include?("poster_offset") || mf.previous_changes.include?("thumbnail_offset") }
before_destroy :stop_processing!
before_destroy :update_parent!
before_destroy :remove_child_files
define_hooks :after_transcoding, :after_processing
after_update_index { |mf| mf.media_object&.enqueue_long_indexing }

Expand Down Expand Up @@ -562,6 +563,12 @@ def stop_processing!
ActiveEncodeJobs::CancelEncodeJob.perform_later(workflow_id, id) if workflow_id.present? && !finished_processing?
end

# Delete does not trigger callbacks so override method to ensure deletion of child supplemental files
def delete
remove_child_files
super
end

protected

def mediainfo
Expand Down Expand Up @@ -792,6 +799,10 @@ def update_parent!
end
end

def remove_child_files
BulkActionJobs::DeleteChildFiles.perform_later(supplemental_files, nil)
end

private

def generate_waveform
Expand Down
16 changes: 16 additions & 0 deletions spec/controllers/master_files_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,22 @@ class << file
expect(controller.current_ability.can?(:destroy, master_file)).to be_falsey
expect(post(:destroy, params: { id: master_file.id })).to render_template('errors/restricted_pid')
end

context "master file with child supplemental files" do
let!(:master_file) { FactoryBot.create(:master_file, :with_media_object, :cancelled_processing, supplemental_files: [supplemental_file]) }
let(:supplemental_file) { FactoryBot.create(:supplemental_file) }

around(:example) do |example|
perform_enqueued_jobs { example.run }
end

it "deletes child files" do
login_as :administrator
expect(SupplementalFile.exists?(supplemental_file.id)).to be_truthy
post :destroy, params: { id: master_file.id }
expect(SupplementalFile.exists?(supplemental_file.id)).to be_falsey
end
end
end

describe "#show" do
Expand Down
17 changes: 17 additions & 0 deletions spec/controllers/media_objects_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,23 @@
expect(flash[:notice]).to include('3 media objects')
media_objects.each {|mo| expect(MediaObject.exists?(mo.id)).to be_falsey }
end

it "should remove child supplemental files" do
supplemental_file = FactoryBot.create(:supplemental_file)
media_object = FactoryBot.create(:media_object, collection: collection, supplemental_files: [supplemental_file] )
expect(SupplementalFile.exists?(supplemental_file.id)).to be_truthy
delete :destroy, params: { id: media_object.id }
expect(SupplementalFile.exists?(supplemental_file.id)).to be_falsey
end

it "should remove section supplemental files" do
supplemental_file = FactoryBot.create(:supplemental_file)
media_object = FactoryBot.create(:media_object, collection: collection )
master_file = FactoryBot.create(:master_file, media_object: media_object, supplemental_files: [supplemental_file])
expect(SupplementalFile.exists?(supplemental_file.id)).to be_truthy
delete :destroy, params: { id: media_object.id }
expect(SupplementalFile.exists?(supplemental_file.id)).to be_falsey
end
end

describe "#confirm_remove" do
Expand Down

0 comments on commit fd5f15b

Please sign in to comment.