From b0d51b739ea90b555ae241bf9ecb7504dda6f849 Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Thu, 28 Mar 2024 09:14:39 +0000 Subject: [PATCH 1/2] Update attachment masking We need to persist the attachment blob as the checksum and byte size can change after masking. --- app/models/foi_attachment.rb | 2 ++ spec/models/foi_attachment_spec.rb | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/app/models/foi_attachment.rb b/app/models/foi_attachment.rb index 4c20e0abe5..35e4259dea 100644 --- a/app/models/foi_attachment.rb +++ b/app/models/foi_attachment.rb @@ -96,6 +96,8 @@ def body=(d) ensure_filename! if file.attached? file_blob.upload(StringIO.new(d.to_s), identify: false) + file_blob.save + else file.attach( io: StringIO.new(d.to_s), diff --git a/spec/models/foi_attachment_spec.rb b/spec/models/foi_attachment_spec.rb index 2106a42365..01491c4f07 100644 --- a/spec/models/foi_attachment_spec.rb +++ b/spec/models/foi_attachment_spec.rb @@ -125,6 +125,16 @@ expect { attachment.update(body: 'masked', masked_at: Time.now) }. to_not change { attachment.file_blob.metadata } end + + it 'persists changes to existing blob checksum' do + attachment = FactoryBot.create( + :foi_attachment, :unmasked, body: 'unmasked' + ) + + expect { attachment.update(body: 'masked', masked_at: Time.now) }. + to change { attachment.file_blob.checksum } + expect(attachment.file_blob.changed?).to eq false + end end describe '#body' do From 98fc4937184530a2189bc9e73e7e41489001bd71 Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Thu, 28 Mar 2024 09:15:59 +0000 Subject: [PATCH 2/2] Update storage mirror task Fix existing integrity errors preventing mirroring of attachments to the secondary storage services. --- config/sidekiq.yml-example | 2 ++ lib/tasks/storage/storage.rb | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/config/sidekiq.yml-example b/config/sidekiq.yml-example index c2323f19b0..10d5d514ae 100644 --- a/config/sidekiq.yml-example +++ b/config/sidekiq.yml-example @@ -7,6 +7,8 @@ production: :queues: - default - xapian + - low :limits: xapian: 1 + low: 1 diff --git a/lib/tasks/storage/storage.rb b/lib/tasks/storage/storage.rb index a9bcc3e315..33447cb263 100644 --- a/lib/tasks/storage/storage.rb +++ b/lib/tasks/storage/storage.rb @@ -46,7 +46,17 @@ def mirror count = mirrorable_blobs.count mirrorable_blobs.find_each.with_index do |blob, index| - mirror_service.mirror(blob.key, checksum: blob.checksum) + begin + mirror_service.mirror(blob.key, checksum: blob.checksum) + rescue ActiveStorage::IntegrityError => ex + raise ex unless @klass == FoiAttachment + + # Fix for https://github.com/mysociety/alaveteli/issues/8181 + attachment = FoiAttachment.joins(:file_blob). + find_by(active_storage_blobs: { id: blob }) + # Running the attachment masking will also mirror the file + FoiAttachmentMaskJob.set(queue: :low).perform_later(attachment) + end print "#{prefix}: Mirrored #{index + 1}/#{count}" end