diff --git a/app/models/foi_attachment.rb b/app/models/foi_attachment.rb index db8481b55a..f0c726d373 100644 --- a/app/models/foi_attachment.rb +++ b/app/models/foi_attachment.rb @@ -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! diff --git a/doc/CHANGES.md b/doc/CHANGES.md index c6f94c3a09..07083a5fd6 100644 --- a/doc/CHANGES.md +++ b/doc/CHANGES.md @@ -2,6 +2,7 @@ ## Highlighted Features +* Reduce amount of storage related background jobs (Graeme Porteous) * Add automatic parsing of emails contain Excel spreadsheets (Graeme Porteous) * Improve rendering of admin hidden request prominence and explanations (Graeme Porteous) diff --git a/spec/models/foi_attachment_spec.rb b/spec/models/foi_attachment_spec.rb index b03d969425..2106a42365 100644 --- a/spec/models/foi_attachment_spec.rb +++ b/spec/models/foi_attachment_spec.rb @@ -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