2023-09-07 15:13:58 +02:00
|
|
|
require 'fog/openstack'
|
|
|
|
|
2023-06-26 10:24:52 +02:00
|
|
|
class PjsMigrationJob < ApplicationJob
|
|
|
|
queue_as :pj_migration_jobs
|
|
|
|
|
|
|
|
def perform(blob_id)
|
2023-06-26 21:32:07 +02:00
|
|
|
blob = ActiveStorage::Blob.find(blob_id)
|
2023-06-26 10:24:52 +02:00
|
|
|
|
|
|
|
return if already_moved?(blob)
|
|
|
|
|
|
|
|
service = blob.service
|
|
|
|
container = service.container
|
|
|
|
old_key = blob.key
|
2023-07-10 10:51:06 +02:00
|
|
|
new_key = "#{blob.created_at.strftime('%Y/%m/%d')}/#{old_key[0..1]}/#{old_key}"
|
2023-06-26 10:24:52 +02:00
|
|
|
|
|
|
|
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
|
2023-09-07 15:13:58 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def client
|
|
|
|
@client ||= begin
|
|
|
|
credentials = Rails.application.config.active_storage
|
|
|
|
.service_configurations['openstack']['credentials']
|
|
|
|
Fog::OpenStack::Storage.new(credentials)
|
|
|
|
end
|
|
|
|
end
|
2023-06-26 10:24:52 +02:00
|
|
|
end
|