Module: Decidim::CarrierWaveMigratorService
- Defined in:
- lib/decidim/carrier_wave_migrator_service.rb
Constant Summary collapse
- MIGRATION_ATTRIBUTES =
{ "Decidim::Organization" => lambda do [ [Decidim::Attachment, "file", Decidim::Cw::AttachmentUploader, "file"], [Decidim::User, "avatar", Decidim::Cw::AvatarUploader, "avatar"], [Decidim::UserGroup, "avatar", Decidim::Cw::AvatarUploader, "avatar"], [Decidim::OAuthApplication, "organization_logo", Decidim::Cw::OAuthApplicationLogoUploader, "organization_logo"], [Decidim::Authorization, "verification_attachment", Decidim::Cw::Verifications::AttachmentUploader, "verification_attachment"], [Decidim::Organization, "official_img_header", Decidim::Cw::OfficialImageHeaderUploader, "official_img_header"], [Decidim::Organization, "official_img_footer", Decidim::Cw::OfficialImageFooterUploader, "official_img_footer"], [Decidim::Organization, "logo", Decidim::Cw::OrganizationLogoUploader, "logo"], [Decidim::Organization, "favicon", Decidim::Cw::OrganizationFaviconUploader, "favicon"], [Decidim::Organization, "highlighted_content_banner_image", Decidim::Cw::ImageUploader, "highlighted_content_banner_image"] ] end, "Decidim::Conference" => lambda do [ [Decidim::Conference, "hero_image", Decidim::Cw::HeroImageUploader, "hero_image"], [Decidim::Conference, "banner_image", Decidim::Cw::HomepageImageUploader, "banner_image"], [Decidim::Conference, "main_logo", Decidim::Cw::Conferences::DiplomaUploader, "main_logo"], [Decidim::Conference, "signature", Decidim::Cw::Conferences::DiplomaUploader, "signature"], [Decidim::ConferenceSpeaker, "avatar", Decidim::Cw::AvatarUploader, "avatar"], [Decidim::Conferences::Partner, "logo", Decidim::Cw::Conferences::PartnerLogoUploader, "logo"] ] end, "Decidim::Consultation" => lambda do [ [Decidim::Consultations::Question, "hero_image", Decidim::Cw::HeroImageUploader, "hero_image"], [Decidim::Consultations::Question, "banner_image", Decidim::Cw::BannerImageUploader, "banner_image"], [Decidim::Consultation, "banner_image", Decidim::Cw::BannerImageUploader, "banner_image"], [Decidim::Consultation, "introductory_image", Decidim::Cw::BannerImageUploader, "introductory_image"] ] end, "Decidim::Votings::Voting" => lambda do [ [Decidim::Votings::Voting, "banner_image", Decidim::Cw::BannerImageUploader, "banner_image"], [Decidim::Votings::Voting, "introductory_image", Decidim::Cw::BannerImageUploader, "introductory_image"] ] end, "Decidim::ParticipatoryProcess" => lambda do [ [Decidim::ParticipatoryProcess, "hero_image", Decidim::Cw::HeroImageUploader, "hero_image"], [Decidim::ParticipatoryProcess, "banner_image", Decidim::Cw::BannerImageUploader, "banner_image"], [Decidim::ParticipatoryProcessGroup, "hero_image", Decidim::Cw::HeroImageUploader, "hero_image"] ] end, "Decidim::Assembly" => lambda do [ [Decidim::Assembly, "hero_image", Decidim::Cw::HeroImageUploader, "hero_image"], [Decidim::Assembly, "banner_image", Decidim::Cw::BannerImageUploader, "banner_image"] ] end, "Decidim::Initiative" => lambda do [ [Decidim::InitiativesType, "banner_image", Decidim::Cw::BannerImageUploader, "banner_image"] ] end }.sum do |main_model, attributes| main_model.constantize.is_a?(Class) ? attributes.call : [] rescue NameError [] end.freeze
Class Method Summary collapse
- .check_content_blocks_attachments(logger:) ⇒ Object
-
.check_migration(klass:, cw_attribute:, cw_uploader:, logger:, as_attribute: cw_attribute) ⇒ Object
rubocop:enable Metrics/ParameterLists.
- .cw_attachments_class(klass, cw_attribute, cw_uploader) ⇒ Object
- .cw_file(attachment) ⇒ Object
- .cw_images_container(block) ⇒ Object
- .downloaded_file_checksum(file) ⇒ Object
-
.migrate_attachment!(klass:, cw_attribute:, cw_uploader:, logger:, as_attribute: cw_attribute, routes_mappings: []) ⇒ Object
rubocop:disable Metrics/ParameterLists.
- .migrate_content_blocks_attachments!(logger:, routes_mappings: []) ⇒ Object
Class Method Details
.check_content_blocks_attachments(logger:) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/decidim/carrier_wave_migrator_service.rb', line 70 def self.(logger:) Decidim::ContentBlock.find_each do |block| next if block.images.blank? block.manifest.images.each do |image_config| = cw_images_container(block).send(image_config[:name]) destination = block.images_container.send(image_config[:name]) next if .file.blank? if destination.attached? .cache_stored_file! file = cw_file() cw_checksum = downloaded_file_checksum(file) as_checksum = destination.blob.checksum logger.info "#{cw_checksum == as_checksum ? "[OK] Checksum identical:" : "[KO] Checksum different:"}" \ " Migrated Decidim::ContentBlock##{block.id} attachment #{image_config[:name]}" \ " from CW attribute #{image_config[:name]} to AS" \ " in Decidim::ContentBlockAttachment##{destination.record.id} file attribute" else logger.info "[SKIP] Pending migration of Decidim::ContentBlock##{block.id} attachment #{image_config[:name]}" \ " from CW attribute #{image_config[:name]} to AS" \ end rescue StandardError logger.info "[ERROR] Exception checking Decidim::ContentBlock##{block.id} attachment #{image_config[:name]}" \ " from CW attribute #{image_config[:name]} to AS file attribute in a Decidim::ContentBlockAttachment instance #{$ERROR_INFO}" end end end |
.check_migration(klass:, cw_attribute:, cw_uploader:, logger:, as_attribute: cw_attribute) ⇒ Object
rubocop:enable Metrics/ParameterLists
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/decidim/carrier_wave_migrator_service.rb', line 194 def self.check_migration(klass:, cw_attribute:, cw_uploader:, logger:, as_attribute: cw_attribute) old_class = (klass, cw_attribute, cw_uploader) old_class.items.each do |item| next if item.send(cw_attribute).blank? copy = klass.find(item.id) if copy.send(as_attribute).attached? = item.send(cw_attribute) .cache_stored_file! file = cw_file() cw_checksum = downloaded_file_checksum(file) as_checksum = copy.send(as_attribute).blob.checksum logger.info "#{cw_checksum == as_checksum ? "[OK] Checksum identical:" : "[KO] Checksum different:"}" \ " Migrated #{klass}##{item.id} from CW attribute #{cw_attribute}" \ " to AS #{as_attribute} attribute" else logger.info "[SKIP] Pending migration of #{klass}##{item.id} from CW attribute #{cw_attribute} to AS #{as_attribute} attribute" end rescue StandardError logger.info "[ERROR] Exception checking #{klass}##{item.id} from CW attribute #{cw_attribute} to AS #{as_attribute} attribute #{$ERROR_INFO}" end end |
.cw_attachments_class(klass, cw_attribute, cw_uploader) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/decidim/carrier_wave_migrator_service.rb', line 221 def self.(klass, cw_attribute, cw_uploader) has_sti = klass.column_names.include?(klass.inheritance_column) = Class.new(ApplicationRecord) do cattr_accessor :klass self.table_name = klass.table_name if has_sti self.inheritance_column = nil scope :items, -> { where(type: klass.name) } else scope :items, -> { all } end mount_uploader cw_attribute, cw_uploader def self.to_s klass.to_s end end .klass = klass end |
.cw_file(attachment) ⇒ Object
287 288 289 290 291 292 293 |
# File 'lib/decidim/carrier_wave_migrator_service.rb', line 287 def self.cw_file() if .file.is_a?(CarrierWave::Storage::Fog::File) URI.parse(.url).open else File.open(.file.file) end end |
.cw_images_container(block) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/decidim/carrier_wave_migrator_service.rb', line 246 def self.cw_images_container(block) manifest = block.manifest images_container_class = Class.new do extend ::CarrierWave::Mount include ActiveModel::Validations cattr_accessor :manifest, :manifest_scope attr_reader :content_block delegate :id, :organization, to: :content_block def self.to_s "decidim/#{manifest.name.to_s.underscore}_#{manifest_scope.to_s.underscore}_content_block" end def initialize(content_block) @content_block = content_block end manifest.images.each do |image_config| mount_uploader image_config[:name], image_config[:uploader].gsub("Decidim::", "Decidim::Cw::").constantize end def read_uploader(column) content_block.images[column.to_s] end end images_container_class.manifest = block.manifest images_container_class.manifest_scope = block.scope_name images_container_class.new(block) end |
.downloaded_file_checksum(file) ⇒ Object
279 280 281 282 283 284 285 |
# File 'lib/decidim/carrier_wave_migrator_service.rb', line 279 def self.downloaded_file_checksum(file) if file.is_a?(StringIO) Digest::MD5.base64digest(file.read) else Digest::MD5.file(file).base64digest end end |
.migrate_attachment!(klass:, cw_attribute:, cw_uploader:, logger:, as_attribute: cw_attribute, routes_mappings: []) ⇒ Object
rubocop:disable Metrics/ParameterLists
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/decidim/carrier_wave_migrator_service.rb', line 151 def self.(klass:, cw_attribute:, cw_uploader:, logger:, as_attribute: cw_attribute, routes_mappings: []) old_class = (klass, cw_attribute, cw_uploader) old_class.items.each do |item| next if item.send(cw_attribute).blank? copy = klass.find(item.id) # Skip record if already been processed if copy.send(as_attribute).attached? logger.info "[SKIP] Migrated #{klass}##{item.id} from CW attribute #{cw_attribute} to AS #{as_attribute} attribute" next end = item.send(cw_attribute) origin_path = .url .cache_stored_file! content_type = .content_type filename = item.attributes[cw_attribute.to_s] file = cw_file() copy.send(as_attribute).attach( io: file, content_type: content_type, filename: filename ) cw_checksum = downloaded_file_checksum(file) as_checksum = copy.send(as_attribute).blob.checksum logger.info "[OK] Migrated - #{cw_checksum == as_checksum ? "[OK] Checksum identical:" : "[KO] Checksum different:"}" \ " #{klass}##{item.id} from CW attribute #{cw_attribute} to AS #{as_attribute} attribute" routes_mappings << { instance: "#{klass}##{item.id}", attachment_origin_attribute: cw_attribute.to_s, origin_path: origin_path, destination_path: Rails.application.routes.url_helpers.rails_blob_url(copy.send(as_attribute).blob, only_path: true) } rescue StandardError logger.info "[ERROR] Exception migrating #{klass}##{item.id} from CW attribute #{cw_attribute} to AS #{as_attribute} attribute #{$ERROR_INFO}" end end |
.migrate_content_blocks_attachments!(logger:, routes_mappings: []) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/decidim/carrier_wave_migrator_service.rb', line 102 def self.(logger:, routes_mappings: []) Decidim::ContentBlock.find_each do |block| next if block.images.blank? block.manifest.images.each do |image_config| destination = block.images_container.send(image_config[:name]) if destination.attached? logger.info "[SKIP] Migrated Decidim::ContentBlock##{block.id} attachment #{image_config[:name]}" \ " from CW attribute #{image_config[:name]} to AS" \ " in Decidim::ContentBlockAttachment##{destination.record.id} file attribute" next end = cw_images_container(block).send(image_config[:name]) next if .file.blank? origin_path = .url .cache_stored_file! content_type = .content_type filename = block.images[image_config[:name].to_s] file = cw_file() destination.attach( io: file, content_type: content_type, filename: filename ) destination.record.save if destination.record.new_record? cw_checksum = downloaded_file_checksum(file) as_checksum = destination.blob.checksum logger.info "[OK] Migrated - #{cw_checksum == as_checksum ? "[OK] Checksum identical:" : "[KO] Checksum different:"}" \ " Decidim::ContentBlock##{block.id} attachment #{image_config[:name]}" \ " from CW attribute #{image_config[:name]} to AS" \ " in Decidim::ContentBlockAttachment##{destination.record.id} file attribute" \ routes_mappings << { instance: "Decidim::ContentBlock##{block.id}", attachment_origin_attribute: image_config[:name], origin_path: origin_path, destination_path: Rails.application.routes.url_helpers.rails_blob_url(destination.blob, only_path: true) } rescue StandardError logger.info "[ERROR] Exception migrating Decidim::ContentBlock##{block.id} attachment #{image_config[:name]}" \ " from CW attribute #{image_config[:name]} to AS file attribute in a Decidim::ContentBlockAttachment instance #{$ERROR_INFO}" end end end |