Allow remote storages in Active Storage migration
So forks that connected Paperclip with S3 buckets can migrate files too.
This commit is contained in:
committed by
Javi Martín
parent
5a4921a1af
commit
87e427f433
@@ -23,6 +23,7 @@ namespace :active_storage do
|
|||||||
|
|
||||||
Rails.application.eager_load!
|
Rails.application.eager_load!
|
||||||
models = ActiveRecord::Base.descendants.reject(&:abstract_class?)
|
models = ActiveRecord::Base.descendants.reject(&:abstract_class?)
|
||||||
|
paperclip_storage = Paperclip::Attachment.default_options[:storage]
|
||||||
|
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
models.each do |model|
|
models.each do |model|
|
||||||
@@ -42,11 +43,19 @@ namespace :active_storage do
|
|||||||
attachments.each do |attachment|
|
attachments.each do |attachment|
|
||||||
next if instance.send(:"storage_#{attachment}").attached?
|
next if instance.send(:"storage_#{attachment}").attached?
|
||||||
|
|
||||||
source = instance.send(attachment).path
|
source = if paperclip_storage == :filesystem
|
||||||
|
instance.send(attachment).path
|
||||||
|
else
|
||||||
|
instance.send(attachment).url
|
||||||
|
end
|
||||||
|
|
||||||
next if source.blank?
|
next if source.blank? || source == "/images/original/missing.png"
|
||||||
|
|
||||||
file = File.read(source) if File.exist?(source)
|
file = if paperclip_storage == :filesystem
|
||||||
|
File.read(source) if File.exist?(source)
|
||||||
|
else
|
||||||
|
Net::HTTP.get(URI(source))
|
||||||
|
end
|
||||||
|
|
||||||
connection.exec_prepared(
|
connection.exec_prepared(
|
||||||
statement_name, [
|
statement_name, [
|
||||||
@@ -67,18 +76,23 @@ namespace :active_storage do
|
|||||||
end
|
end
|
||||||
|
|
||||||
ActiveStorage::Attachment.find_each do |attachment|
|
ActiveStorage::Attachment.find_each do |attachment|
|
||||||
dest = ActiveStorage::Blob.service.path_for(attachment.blob.key)
|
blob = attachment.blob
|
||||||
|
|
||||||
next if File.exist?(dest) || !attachment.record
|
next if blob.service.exist?(blob.key) || !attachment.record
|
||||||
|
|
||||||
name = attachment.name.delete_prefix("storage_")
|
name = attachment.name.delete_prefix("storage_")
|
||||||
source = attachment.record.send(name).path
|
paperclip_attachment = attachment.record.send(name)
|
||||||
|
|
||||||
if source && File.exist?(source)
|
next unless paperclip_attachment.exists?
|
||||||
FileUtils.mkdir_p(File.dirname(dest))
|
|
||||||
logger.info "Copying #{source} to #{dest}"
|
source_file = if paperclip_storage == :filesystem
|
||||||
FileUtils.cp(source, dest)
|
paperclip_attachment.path
|
||||||
end
|
else
|
||||||
|
URI.open(paperclip_attachment.url, &:read)
|
||||||
|
end
|
||||||
|
|
||||||
|
logger.info "Copying #{paperclip_attachment.url} to active storage"
|
||||||
|
blob.service.upload(blob.key, source_file)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user