demarches-normaliennes/lib/tasks/2019_08_20_migrate_procedure_logo.rake
2019-08-22 11:28:03 +02:00

33 lines
1 KiB
Ruby

namespace :'2019_08_20_migrate_procedure_logo' do
task run: :environment do
procedures = Procedure.unscope(where: :hidden_at)
.where.not(logo: nil)
.left_joins(:logo_active_storage_attachment)
.where('active_storage_attachments.id IS NULL')
.order(:created_at)
limit = ENV['LIMIT']
if limit
procedures.limit!(limit.to_i)
end
progress = ProgressReport.new(procedures.count)
procedures.find_each do |procedure|
if procedure.logo.present?
uri = URI.parse(URI.escape(procedure.logo_url))
response = Typhoeus.get(uri)
if response.success?
filename = procedure.logo.filename || procedure.logo_identifier
procedure.logo_active_storage.attach(
io: StringIO.new(response.body),
filename: filename,
content_type: procedure.logo.content_type,
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
end
end
progress.inc
end
progress.finish
end
end