services: mark attachments migrated from CarrierWave as safe

This avoids to enqueue thousands of scans when migrating the PJs of
a whole procedure.
This commit is contained in:
Pierre de La Morinerie 2019-05-28 08:45:24 +00:00
parent 01e6af47a7
commit 52b7a82932
2 changed files with 33 additions and 4 deletions

View file

@ -75,14 +75,15 @@ class CarrierwaveActiveStorageMigrationService
# but when the first attachment that references this blob is created.
def make_blob(uploader, created_at, filename: nil, identify: false)
content_type = uploader.content_type
identified = content_type.present? && !identify
ActiveStorage::Blob.create(
filename: filename || uploader.filename,
content_type: uploader.content_type,
identified: content_type.present? && !identify,
byte_size: uploader.size,
checksum: checksum(uploader),
created_at: created_at
created_at: created_at,
metadata: { identified: identified, virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
end

View file

@ -1,9 +1,37 @@
require 'spec_helper'
describe CarrierwaveActiveStorageMigrationService do
describe '#hex_to_base64' do
let(:service) { CarrierwaveActiveStorageMigrationService.new }
let(:service) { CarrierwaveActiveStorageMigrationService.new }
describe '#hex_to_base64' do
it { expect(service.hex_to_base64('deadbeef')).to eq('3q2+7w==') }
end
describe '.make_blob' do
let(:pj) { create(:piece_justificative, :rib) }
let(:identify) { false }
before do
allow(service).to receive(:checksum).and_return('cafe')
end
subject(:blob) { service.make_blob(pj.content, pj.updated_at.iso8601, filename: pj.original_filename, identify: identify) }
it 'marks the blob as already scanned by the antivirus' do
expect(blob.metadata[:virus_scan_result]).to eq(ActiveStorage::VirusScanner::SAFE)
end
it 'sets the blob MIME type from the file' do
expect(blob.identified).to be true
expect(blob.content_type).to eq 'application/pdf'
end
context 'when asking for explicit MIME type identification' do
let(:identify) { true }
it 'marks the file as needing MIME type detection' do
expect(blob.identified).to be false
end
end
end
end