diff --git a/lib/tasks/refresh_blob_urls.rake b/lib/tasks/refresh_blob_urls.rake new file mode 100644 index 00000000..b3e4c6d9 --- /dev/null +++ b/lib/tasks/refresh_blob_urls.rake @@ -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 \ No newline at end of file