Skip to content

Commit

Permalink
Merge branch '8181-fix-blob-integrity-error' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Mar 28, 2024
2 parents 9ad45de + 98fc493 commit 722c51e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/models/foi_attachment.rb
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions config/sidekiq.yml-example
Expand Up @@ -7,6 +7,8 @@ production:
:queues:
- default
- xapian
- low

:limits:
xapian: 1
low: 1
12 changes: 11 additions & 1 deletion lib/tasks/storage/storage.rb
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/models/foi_attachment_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 722c51e

Please sign in to comment.