namespace :debshots do desc "Convert images from Paperclip schema to ActiveStorage schema" task paperclip_to_activestorage: :environment do klass = Screenshot attachment = 'image' name_field = :"#{attachment}_file_name" klass.where.not(name_field => nil).find_each do |instance| # This step helps us catch any attachments we might have uploaded that # don't have an explicit file extension in the filename puts "----------" filename = instance.send("#{attachment}_file_name") puts filename next if filename.blank? id = instance.id id_partition = ("%09d".freeze % id).scan(/\d{3}/).join("/".freeze) path = "storage/#{klass.table_name}/#{attachment.pluralize}/#{id_partition}/original/#{filename}" puts path #url = "https://nyc3.digitaloceanspaces.com/gorails/#{klass.table_name}/#{attachment.pluralize}/#{id_partition}/original/#{filename}" instance.send(attachment.to_sym).attach( # io: open(url), io: File.open(path), filename: instance.send(name_field), content_type: instance.send(:"#{attachment}_content_type") ) end end end