demarches-normaliennes/app/jobs/pjs_migration_job.rb
simon lehericey 7ec604ced3 typo
2023-06-26 21:32:07 +02:00

31 lines
864 B
Ruby

class PjsMigrationJob < ApplicationJob
queue_as :pj_migration_jobs
def perform(blob_id)
blob = ActiveStorage::Blob.find(blob_id)
return if already_moved?(blob)
service = blob.service
client = service.client
container = service.container
old_key = blob.key
new_key = "#{blob.created_at.year}/#{old_key[0..1]}/#{old_key[2..3]}/#{old_key}"
excon_response = client.copy_object(container,
old_key,
container,
new_key,
{ "Content-Type" => blob.content_type })
if excon_response.status == 201
blob.update_columns(key: new_key)
client.delete_object(container, old_key)
end
rescue Fog::OpenStack::Storage::NotFound
end
def already_moved?(blob)
blob.key.include?('/')
end
end