carrierwave: when migrating, create an empty blob if file is missing
This commit is contained in:
parent
44c410d40d
commit
10df7b70ee
3 changed files with 66 additions and 7 deletions
|
@ -79,7 +79,7 @@ class CarrierwaveActiveStorageMigrationService
|
||||||
|
|
||||||
ActiveStorage::Blob.create(
|
ActiveStorage::Blob.create(
|
||||||
filename: filename || uploader.filename,
|
filename: filename || uploader.filename,
|
||||||
content_type: uploader.content_type,
|
content_type: content_type,
|
||||||
byte_size: uploader.size,
|
byte_size: uploader.size,
|
||||||
checksum: checksum(uploader),
|
checksum: checksum(uploader),
|
||||||
created_at: created_at,
|
created_at: created_at,
|
||||||
|
@ -87,6 +87,20 @@ class CarrierwaveActiveStorageMigrationService
|
||||||
)
|
)
|
||||||
end
|
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)
|
def checksum(uploader)
|
||||||
hex_to_base64(uploader.file.send(:file).etag)
|
hex_to_base64(uploader.file.send(:file).etag)
|
||||||
end
|
end
|
||||||
|
|
|
@ -75,12 +75,18 @@ class PieceJustificativeToChampPieceJointeMigrationService
|
||||||
end
|
end
|
||||||
|
|
||||||
def convert_pj_to_champ!(pj, champ)
|
def convert_pj_to_champ!(pj, champ)
|
||||||
blob = make_blob(pj)
|
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
|
# Upload the file before creating the attachment to make sure MIME type
|
||||||
# identification doesn’t fail.
|
# identification doesn’t fail.
|
||||||
storage_service.copy_from_carrierwave_to_active_storage!(pj.content.path, blob)
|
storage_service.copy_from_carrierwave_to_active_storage!(pj.content.path, blob)
|
||||||
attachment = storage_service.make_attachment(champ, 'piece_justificative_file', 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
|
# 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,
|
# that one exists now. We do this so that, if we need to roll back and destroy the champ,
|
||||||
|
@ -112,4 +118,8 @@ class PieceJustificativeToChampPieceJointeMigrationService
|
||||||
def make_blob(pj)
|
def make_blob(pj)
|
||||||
storage_service.make_blob(pj.content, pj.updated_at.iso8601, filename: pj.original_filename)
|
storage_service.make_blob(pj.content, pj.updated_at.iso8601, filename: pj.original_filename)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def make_empty_blob(pj)
|
||||||
|
storage_service.make_empty_blob(pj.content, pj.updated_at.iso8601, filename: pj.original_filename)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@ describe CarrierwaveActiveStorageMigrationService do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '.make_blob' do
|
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 }
|
let(:identify) { false }
|
||||||
|
|
||||||
before do
|
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) }
|
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
|
it 'marks the blob as already scanned by the antivirus' do
|
||||||
expect(blob.metadata[:virus_scan_result]).to eq(ActiveStorage::VirusScanner::SAFE)
|
expect(blob.metadata[:virus_scan_result]).to eq(ActiveStorage::VirusScanner::SAFE)
|
||||||
end
|
end
|
||||||
|
@ -34,4 +36,37 @@ describe CarrierwaveActiveStorageMigrationService do
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue