Merge pull request #9214 from demarches-simplifiees/renew_signed_id_blob_tasks

Outillage: ajout d'une tache qui permet d'invalider des blob signed_ids
This commit is contained in:
LeSim 2023-06-20 08:32:13 +00:00 committed by GitHub
commit eecd05af68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

37
lib/tasks/blobs.rake Normal file
View file

@ -0,0 +1,37 @@
namespace :blobs do
desc <<~EOD
given a file blob_ids.json with contains { blob_ids: [...] },
download and reattached champ piece_justificative_file to create new blob
and delete the old one, and thus, invalides old signed_ids.
bin/rails 'blobs:renew_signed_ids[blob_ids.json]'
EOD
task :renew_signed_ids, [:file_path] => :environment do |_t, args|
blob_ids = JSON.parse(File.read(args[:file_path]))['blob_ids']
blobs = ActiveStorage::Blob.find(blob_ids)
blobs.each do |blob|
blob.open do |file|
blob.attachments.each do |attachment|
if [attachment.record_type, attachment.name] != ['Champ', 'piece_justificative_file']
raise "not a piece justificative #{attachment.id}"
end
champ = attachment.record
champ_updated_at = champ.updated_at
dossier = champ.dossier
dossier_updated_at = dossier.updated_at
file.rewind
# badly, it updates champ and dossier updated_at attributes
champ.piece_justificative_file.attach(io: file, filename: blob.filename, content_type: blob.content_type)
# it destroys the blob as well
attachment.destroy
champ.update_column(:updated_at, champ_updated_at)
dossier.update_column(:updated_at, dossier_updated_at)
end
end
end
end
end