Skip to content
Snippets Groups Projects
Unverified Commit 20e832d5 authored by Sébastien Gaya's avatar Sébastien Gaya
Browse files

service + task

parent 5e83a464
No related branches found
No related tags found
No related merge requests found
class ActiveStorageKeyConverter
def self.convert(legacy_signed_id)
# Try to find blob with the un-modified legacy_signed_id
blob = ActiveStorage::Blob.find_signed!(legacy_signed_id)
legacy_signed_id
rescue ActiveSupport::MessageVerifier::InvalidSignature
#
key_generator = ActiveSupport::KeyGenerator.new(
Rails.application.secrets.secret_key_base,
iterations: 1000,
hash_digest_class: OpenSSL::Digest::SHA1
)
key_generator = ActiveSupport::CachingKeyGenerator.new(key_generator)
secret = key_generator.generate_key("ActiveStorage")
verifier = ActiveSupport::MessageVerifier.new(secret)
ActiveStorage::Blob.find_by_id(verifier.verify(legacy_signed_id, purpose: :blob_id)).try(:signed_id)
end
end
class BlocksMigration
def self.cleanup
Communication::Block.all.find_each { |block|
self.crawl(block.data)
block.save
}
end
protected
def self.crawl(enumerable)
case enumerable
when Array
enumerable.each do |item|
crawl(item) if [Array, Hash].include?(item.class)
end
when Hash
enumerable.keys.each do |key|
if key == "signed_id"
# Convert value
enumerable[key] = ActiveStorageKeyConverter.convert(enumerable[key]) if key == "signed_id"
elsif [Array, Hash].include?(enumerable[key].class)
crawl(enumerable[key])
end
end
end
end
end
\ No newline at end of file
......@@ -8,6 +8,7 @@ namespace :app do
desc 'Fix things'
task fix: :environment do
BlocksMigration.cleanup
end
namespace :websites do
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment