Skip to content

Commit

Permalink
Update attachment body assignment
Browse files Browse the repository at this point in the history
When masking attachments avoid replacing the blob with a new blob,
instead update which retains the blob key and existing metadata.

This means no `ActiveStorage::PurgeJob` or an additional
`ActiveStorage::AnalyzeJob` for the new blob.
  • Loading branch information
gbp committed Feb 9, 2024
1 parent 2893fee commit 4808dda
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
14 changes: 9 additions & 5 deletions app/models/foi_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,15 @@ def body=(d)
self.hexdigest ||= Digest::MD5.hexdigest(d)

ensure_filename!
file.attach(
io: StringIO.new(d.to_s),
filename: filename,
content_type: content_type
)
if file.attached?
file_blob.upload(StringIO.new(d.to_s), identify: false)
else
file.attach(
io: StringIO.new(d.to_s),
filename: filename,
content_type: content_type
)
end

@cached_body = d.force_encoding("ASCII-8BIT")
update_display_size!
Expand Down
1 change: 1 addition & 0 deletions doc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Highlighted Features

* Reduce amount of storage related background jobs (Graeme Porteous)
* Add admin list of all citations (Gareth Rees)
* Improve redirection flow after user account closure actions (Gareth Rees)
* Fix duplicated attachment masking jobs (Graeme Porteous)
Expand Down
18 changes: 18 additions & 0 deletions spec/models/foi_attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@
ActiveStorage::Blob.services.fetch(blob.service_name).exist?(blob.key)
}.from(false).to(true)
end

it 'does not reset existing blob key' do
attachment = FactoryBot.create(
:foi_attachment, :unmasked, body: 'unmasked'
)

expect { attachment.update(body: 'masked', masked_at: Time.now) }.
to_not change { attachment.file_blob.key }
end

it 'does not reset existing blob metadata' do
attachment = FactoryBot.create(
:foi_attachment, :unmasked, body: 'unmasked'
)

expect { attachment.update(body: 'masked', masked_at: Time.now) }.
to_not change { attachment.file_blob.metadata }
end
end

describe '#body' do
Expand Down

0 comments on commit 4808dda

Please sign in to comment.