|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "alchemy/shell" |
| 4 | +require "benchmark" |
| 5 | +require "active_storage/service/disk_service" |
| 6 | + |
| 7 | +module Alchemy |
| 8 | + class Upgrader::SevenPointThree < Upgrader |
| 9 | + extend Alchemy::Shell |
| 10 | + DEFAULT_CONTENT_TYPE = "application/octet-stream" |
| 11 | + DISK_SERVICE = ActiveStorage::Service::DiskService |
| 12 | + SERVICE_NAME = :alchemy_cms |
| 13 | + |
| 14 | + # Prevents (down)loading the original file |
| 15 | + METADATA = { |
| 16 | + identified: true, # Skip identifying file type |
| 17 | + analyzed: true, # Skip analyze job |
| 18 | + composed: true # Skip checksum check |
| 19 | + } |
| 20 | + |
| 21 | + class << self |
| 22 | + def migrate_pictures_to_active_storage |
| 23 | + pictures_without_as_attachment = Alchemy::Picture.where.missing(:image_file_attachment) |
| 24 | + count = pictures_without_as_attachment.count |
| 25 | + if count > 0 |
| 26 | + log "Migrating #{count} Dragonfly image file(s) to ActiveStorage." |
| 27 | + realtime = Benchmark.realtime do |
| 28 | + pictures_without_as_attachment.find_each do |picture| |
| 29 | + Alchemy::Deprecation.silence do |
| 30 | + uid = picture.legacy_image_file_uid |
| 31 | + key = key_for_uid(uid) |
| 32 | + content_type = Mime::Type.lookup_by_extension(picture.legacy_image_file_format) || DEFAULT_CONTENT_TYPE |
| 33 | + Alchemy::Picture.transaction do |
| 34 | + blob = ActiveStorage::Blob.create!( |
| 35 | + key: key, |
| 36 | + filename: picture.legacy_image_file_name, |
| 37 | + byte_size: picture.legacy_image_file_size, |
| 38 | + content_type: content_type, |
| 39 | + metadata: METADATA.merge( |
| 40 | + width: picture.legacy_image_file_width, |
| 41 | + height: picture.legacy_image_file_height |
| 42 | + ), |
| 43 | + service_name: SERVICE_NAME |
| 44 | + ) |
| 45 | + picture.create_image_file_attachment!( |
| 46 | + name: :image_file, |
| 47 | + record: picture, |
| 48 | + blob: blob |
| 49 | + ) |
| 50 | + end |
| 51 | + move_file(Rails.root.join("uploads/pictures", uid), key) |
| 52 | + end |
| 53 | + print "." |
| 54 | + end |
| 55 | + end |
| 56 | + puts "\nDone in #{realtime.round(2)}s!" |
| 57 | + else |
| 58 | + log "No Dragonfly image files for migration found.", :skip |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + def migrate_attachments_to_active_storage |
| 63 | + attachments_without_as_attachment = Alchemy::Attachment.where.missing(:file_attachment) |
| 64 | + count = attachments_without_as_attachment.count |
| 65 | + if count > 0 |
| 66 | + log "Migrating #{count} Dragonfly attachment file(s) to ActiveStorage." |
| 67 | + realtime = Benchmark.realtime do |
| 68 | + attachments_without_as_attachment.find_each do |attachment| |
| 69 | + Alchemy::Deprecation.silence do |
| 70 | + uid = attachment.legacy_file_uid |
| 71 | + key = key_for_uid(uid) |
| 72 | + Alchemy::Attachment.transaction do |
| 73 | + blob = ActiveStorage::Blob.create!( |
| 74 | + key: key, |
| 75 | + filename: attachment.legacy_file_name, |
| 76 | + byte_size: attachment.legacy_file_size, |
| 77 | + content_type: attachment.file_mime_type.presence || DEFAULT_CONTENT_TYPE, |
| 78 | + metadata: METADATA, |
| 79 | + service_name: SERVICE_NAME |
| 80 | + ) |
| 81 | + attachment.create_file_attachment!( |
| 82 | + record: attachment, |
| 83 | + name: :file, |
| 84 | + blob: blob |
| 85 | + ) |
| 86 | + end |
| 87 | + move_file(Rails.root.join("uploads/attachments", uid), key) |
| 88 | + end |
| 89 | + print "." |
| 90 | + end |
| 91 | + end |
| 92 | + puts "\nDone in #{realtime.round(2)}s!" |
| 93 | + else |
| 94 | + log "No Dragonfly attachment files for migration found.", :skip |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + private |
| 99 | + |
| 100 | + # ActiveStorage::Service::DiskService stores files in a folder structure |
| 101 | + # based on the first two characters of the file uid. |
| 102 | + def key_for_uid(uid) |
| 103 | + case service |
| 104 | + when DISK_SERVICE |
| 105 | + uid.split("/").last |
| 106 | + else |
| 107 | + uid |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + # ActiveStorage::Service::DiskService stores files in a folder structure |
| 112 | + # based on the first two characters of the file uid. |
| 113 | + def move_file(uid, key) |
| 114 | + case service |
| 115 | + when DISK_SERVICE |
| 116 | + if File.exist?(uid) |
| 117 | + service.send(:make_path_for, key) |
| 118 | + FileUtils.mv uid, service.send(:path_for, key) |
| 119 | + end |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + def service |
| 124 | + ActiveStorage::Blob.services.fetch(SERVICE_NAME) |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | +end |
0 commit comments