更改CDN后更新投票中的URL

我在论坛里创建了一个这样的投票:

[poll name=freestyle type=multiple results=always min=1 max=14 public=true chartType=bar]
* **#1** ![DSC_2281.jpg|332x500, 33%](upload://ujMDcnd4syevNGdEN2O1qUTeWeK)
* **#2** ![IMG_3248.jpg|690x459, 33%](upload://tsecEjlruXEpg3faidWmENBupM4)
[/poll]

我从 CDN 迁移到了不使用 CDN,URL 从 cdn.unicyclist.com 改为了 unicyclist.com

但是,尽管图片在帖子预览中显示正常,这里的图片链接仍然失效:

重新烘焙帖子并运行 rake 'posts:remap[cdn.unicyclist.com,unicyclist.com]' 并没有解决问题。

ask.discourse.org 建议:

可能发生了什么

你的投票选项不仅存在于帖子原始内容中。在你的站点架构中,投票还将渲染后的选项 HTML 存储在:

  • polls
  • poll_options.html

因此,帖子预览可能看起来没问题,但投票渲染器可能仍在使用包含旧 CDN 主机名的过时 HTML。

相关文档:

重要警告

请先进行完整备份。

此外,如果投票已有投票结果,请避免“直接编辑”投票,因为投票编辑通常会清除投票结果:

更安全的做法:先检查一个损坏的投票

在容器内:

cd /var/discourse
./launcher enter app
rails c

然后找到该帖子的投票:

poll = Poll.find_by(post_id: POST_ID, name: "freestyle")
PollOption.where(poll_id: poll.id).pluck(:id, :html)

如果你在该 HTML 中看到 cdn.unicyclist.com,那就证实了这一点。

修复该投票

poll = Poll.find_by(post_id: POST_ID, name: "freestyle")

PollOption.where(poll_id: poll.id).find_each do |opt|
  new_html = opt.html.gsub("cdn.unicyclist.com", "unicyclist.com")
  opt.update_columns(html: new_html, updated_at: Time.zone.now)
end

然后重新烘焙该帖子:

Post.find(POST_ID).rebake!

如果有效,批量修复所有投票选项

PollOption.find_each do |opt|
  next unless opt.html&.include?("cdn.unicyclist.com")
  opt.update_columns(
    html: opt.html.gsub("cdn.unicyclist.com", "unicyclist.com"),
    updated_at: Time.zone.now
  )
end

为什么 posts:remap 没有帮助

因为那个 rake 任务是为帖子内容设计的,而你损坏的图片引用可能存储在投票选项的 HTML 缓存中:

建议

  1. 先在一个损坏的投票上测试
  2. 确认图片加载正常
  3. 然后再运行批量修复

如果你愿意,我可以为你编写一个复制粘贴脚本,你只需替换 POST_ID 即可,先在一个投票上安全运行。

你觉得这样行吗?我当然会先做备份,但我更倾向于避免不得不恢复论坛的情况。

我需要保留所有的投票结果 :slight_smile:

额外问题:Discourse 应该原生处理这些情况吗?