Class: ActiveCanvas::MediaRefBackfill
- Inherits:
-
Object
- Object
- ActiveCanvas::MediaRefBackfill
- Defined in:
- app/services/active_canvas/media_ref_backfill.rb
Overview
One-time, best-effort backfill: rewrites legacy <img> tags whose src is an Active Storage blob redirect URL into stable <img data-ac-media-id=āNā> refs that ContentRenderer can resolve at render time. Idempotent; logs unmatched.
Recovers the blob id by decoding the signed_id payload directly, bypassing the expiry check (this is our own data) so already-rotted URLs are healed.
Constant Summary collapse
- BLOB_URL_RE =
%r{active_storage/blobs/(?:redirect|proxy)/([^/]+)/[^/]+\z}
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(logger) ⇒ MediaRefBackfill
constructor
A new instance of MediaRefBackfill.
- #run ⇒ Object
Constructor Details
#initialize(logger) ⇒ MediaRefBackfill
Returns a new instance of MediaRefBackfill.
18 19 20 |
# File 'app/services/active_canvas/media_ref_backfill.rb', line 18 def initialize(logger) @logger = logger end |
Class Method Details
.run(logger: Rails.logger) ⇒ Object
14 15 16 |
# File 'app/services/active_canvas/media_ref_backfill.rb', line 14 def self.run(logger: Rails.logger) new(logger).run end |
Instance Method Details
#run ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/services/active_canvas/media_ref_backfill.rb', line 22 def run ActiveCanvas::Page.find_each do |page| content = page.content next if content.blank? fragment = Nokogiri::HTML5.fragment(content) changed = false fragment.css("img").each do |img| next if img["data-ac-media-id"].present? src = img["src"].to_s next unless src.match?(BLOB_URL_RE) media_id = media_id_for(src) if media_id img["data-ac-media-id"] = media_id.to_s changed = true else @logger.warn("[ActiveCanvas] backfill: could not match media for #{src} on page #{page.id}") end end # update_columns: skip sanitize + version callbacks; we only add a data-* attr. page.update_columns(content: fragment.to_html) if changed end end |