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(
|
||||
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
|
||||
|
|
|
@ -75,12 +75,18 @@ class PieceJustificativeToChampPieceJointeMigrationService
|
|||
end
|
||||
|
||||
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
|
||||
# identification doesn’t fail.
|
||||
storage_service.copy_from_carrierwave_to_active_storage!(pj.content.path, blob)
|
||||
attachment = storage_service.make_attachment(champ, 'piece_justificative_file', blob)
|
||||
# Upload the file before creating the attachment to make sure MIME type
|
||||
# identification doesn’t fail.
|
||||
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,
|
||||
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue