Skip to content

Commit

Permalink
Active Storage blob redirect refresher
Browse files Browse the repository at this point in the history
  • Loading branch information
abartov committed Dec 21, 2023
1 parent f15452b commit 48e3e75
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/tasks/refresh_blob_urls.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
desc "Refresh active storage blob URLs in markdown"
task :refresh_blob_urls => :environment do
puts "Refreshing blob urls due to signed_id change..."
mm_ids = ActiveStorage::Attachment.where(name: 'images').pluck(:record_id).uniq
total = mm_ids.count
i = 0
replaced = 0
missing_imgs = []
Chewy.strategy(:atomic) do
mm_ids.each do |mid|
puts "Processing item #{i} of #{total}" if i % 50 == 0
m = Manifestation.find(mid)
m.markdown = m.markdown.gsub(/!\[.*?\]\(\/rails\/active_storage\/blobs\/.*\/(.*)\)/) do |match|
img = m.images.joins(:blob).where(blob: {filename: $1}).first
if img.present?
replaced += 1
"![#{$1}](#{Rails.application.routes.url_helpers.url_for(img).gsub('https://benyehuda.org','')})"
# Rails.application.routes.url_helpers.rails_blob_path(img, only_path: true)
else
puts "missing image in mid #{mid}"
missing_imgs << mid
match
end
end
m.save
i += 1
end
puts "Done. Replaced #{replaced} image urls."
puts "Missing images in #{missing_imgs.count} manifestations: #{missing_imgs.join(', ')}"
end
end

0 comments on commit 48e3e75

Please sign in to comment.