To migrate the application as of April 2020: • leave models/screenshot.rb with “has_attached_file” • bundle exec rake db:migrate • bin/move_paperclip_to_activestorage • set models/screenshot.rb to “has_one_attached”
34 lines
1.1 KiB
Ruby
34 lines
1.1 KiB
Ruby
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
|