Rake posts:remap - 0 posts remapped!

I’m trying to find and replace some broken images left over from our forum migration to Discourse and have had great success (love the remap command!) but it’s not finding anything to replace for these examples and I can’t see anything wrong, can you?

Example content:

<img src="https://londonbikers.com/images/transparent.png" alt="PICT0738.JPG" data-orig-src="upload://1yz82u6bbkaAQ7xP06qNTmX9qy3.JPG" width="666" height="500" class="d-lazyload">

and

<img src="https://londonbikers.com/images/transparent.png" alt="Rhonda.JPG" data-orig-src="upload://vXQ67D17QssTe5cu6lamJ5XB5Ep.JPG" width="590" height="500" class="d-lazyload">

I need to do this in two passes as I need to keep the alt tag contents and rebuild the img element around it. It’s failing on the first remap:

i.e. FIND:
<img src="https://londonbikers.com/images/transparent.png" alt="

and REPLACE with:
<img src="https://londonbikersarchive.blob.core.windows.net/forum-attachments/

I’ve assumed double quotes need to be escaped with a backslash? So have used this command:

rake posts:remap["<img src=\"https://londonbikers.com/images/transparent.png\" alt=\"","<img src=\"https://londonbikersarchive.blob.core.windows.net/forum-attachments/"]

But I get told 0 posts remapped! when there are thousands of such posts with those elements in. Any ideas?

My second pass would then be to remap the end of the tag from:
width="666" height="500" class="d-lazyload">

to:
/>

Oh, hang on, some of these images are working. Is this Discourse attachment functionality? Is there some custom rendering logic going on as opposed to these being HTML strings in the database?

For example, this is showing a photo:

<img src="https://londonbikers.com/uploads/default/original/1X/25b381a9dc03f023a1c5d0af4f2f3132dfbf45c7.jpg" alt="The_rides_0010.jpg" width="640" height="480" class="d-lazyload">

But this one doesn’t. I can have the file in cloud blob storage so would either like to import it into Discourse or just have it referenced from blob storage:

<img src="https://londonbikers.com/images/transparent.png" alt="__2.jpg" data-orig-src="upload://411oUp2Yn6wumJCRAiFptkwTF58.jpg" width="662" height="500" class="d-lazyload">

Are those images in raw?

I think that rather than doing it with remap you’ll be better off using ruby in the rails console.

I’m sorry I didn’t understand any of that Jay :slight_smile:

Raw, like the file format? No just usual web formats. Doing what in rails console?

raw is the field where the text you see in the editor is. The stuff you are replacing is what you see if you edit the post in the ux, right? (cooked is the stuff that’s been processed and is what gets displayed.)

Doing stuff to re-write the posts. This is might be just enough to make you dangerous:

bps = Post.where("raw like '%//SOME-STRING%'")
bps.each do |post|
  post.raw.gsub!(/OLD/,'NEW')
  post.save
end

The advantage over the rake task is that you don’t have multiple levels of espaping to navigate.

2 Likes