carrierwave: when migrating, create an empty blob if file is missing

This commit is contained in:
Pierre de La Morinerie 2019-05-28 18:19:33 +02:00
parent 44c410d40d
commit 10df7b70ee
3 changed files with 66 additions and 7 deletions

View file

@ -79,7 +79,7 @@ class CarrierwaveActiveStorageMigrationService
ActiveStorage::Blob.create(
filename: filename || uploader.filename,
content_type: uploader.content_type,
content_type: content_type,
byte_size: uploader.size,
checksum: checksum(uploader),
created_at: created_at,
@ -87,6 +87,20 @@ class CarrierwaveActiveStorageMigrationService
)
end
def make_empty_blob(uploader, created_at, filename: nil)
content_type = uploader.content_type || 'text/plain'
blob = ActiveStorage::Blob.build_after_upload(
io: StringIO.new('File not found when migrating from CarrierWave.'),
filename: filename || uploader.filename,
content_type: content_type || 'text/plain',
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
blob.created_at = created_at
blob.save!
blob
end
def checksum(uploader)
hex_to_base64(uploader.file.send(:file).etag)
end

View file

@ -75,6 +75,8 @@ class PieceJustificativeToChampPieceJointeMigrationService
end
def convert_pj_to_champ!(pj, champ)
actual_file_exists = pj.content.file.send(:file)
if actual_file_exists
blob = make_blob(pj)
# Upload the file before creating the attachment to make sure MIME type
@ -82,6 +84,10 @@ class PieceJustificativeToChampPieceJointeMigrationService
storage_service.copy_from_carrierwave_to_active_storage!(pj.content.path, blob)
attachment = storage_service.make_attachment(champ, 'piece_justificative_file', blob)
else
make_empty_blob(pj)
end
# By reloading, we force ActiveStorage to look at the attachment again, and see
# that one exists now. We do this so that, if we need to roll back and destroy the champ,
# the blob, the attachment and the actual file on OpenStack also get deleted.
@ -112,4 +118,8 @@ class PieceJustificativeToChampPieceJointeMigrationService
def make_blob(pj)
storage_service.make_blob(pj.content, pj.updated_at.iso8601, filename: pj.original_filename)
end
def make_empty_blob(pj)
storage_service.make_empty_blob(pj.content, pj.updated_at.iso8601, filename: pj.original_filename)
end
end

View file

@ -8,7 +8,7 @@ describe CarrierwaveActiveStorageMigrationService do
end
describe '.make_blob' do
let(:pj) { create(:piece_justificative, :rib) }
let(:pj) { create(:piece_justificative, :rib, updated_at: Time.zone.local(2019, 01, 01, 12, 00)) }
let(:identify) { false }
before do
@ -17,6 +17,8 @@ describe CarrierwaveActiveStorageMigrationService do
subject(:blob) { service.make_blob(pj.content, pj.updated_at.iso8601, filename: pj.original_filename, identify: identify) }
it { expect(blob.created_at).to eq pj.updated_at }
it 'marks the blob as already scanned by the antivirus' do
expect(blob.metadata[:virus_scan_result]).to eq(ActiveStorage::VirusScanner::SAFE)
end
@ -34,4 +36,37 @@ describe CarrierwaveActiveStorageMigrationService do
end
end
end
describe '.make_empty_blob' do
let(:pj) { create(:piece_justificative, :rib, updated_at: Time.zone.local(2019, 01, 01, 12, 00)) }
before 'set the underlying stored file as missing' do
allow(pj.content.file).to receive(:file).and_return(nil)
end
subject(:blob) { service.make_empty_blob(pj.content, pj.updated_at.iso8601, filename: pj.original_filename) }
it { expect(blob.created_at).to eq pj.updated_at }
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 the file metadata are also missing' do
before do
allow(pj).to receive(:original_filename).and_return(nil)
allow(pj.content).to receive(:content_type).and_return(nil)
end
it 'fallbacks on default values' do
expect(blob.filename).to eq pj.content.filename
expect(blob.content_type).to eq 'text/plain'
end
end
end
end