# PullHotlinkedImages 在帖子编辑后不再重新替换图片

**URL:** <https://meta.discourse.org/t/pullhotlinkedimages-does-not-replace-images-again-after-post-is-edited/154184>\
**Category:** Bug\
**Created:** [2020年六月8日 13:33 UTC](https://meta.discourse.org/t/pullhotlinkedimages-does-not-replace-images-again-after-post-is-edited/154184 "2020-06-08T13:33:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)\
**Post date:** [2020年六月8日 13:33 UTC](https://meta.discourse.org/t/pullhotlinkedimages-does-not-replace-images-again-after-post-is-edited/154184/1 "2020-06-08T13:33:12Z")

</div>

嘿，

![image](https://cdck-file-uploads-global.s3.dualstack.us-west-2.amazonaws.com/meta/original/3X/9/e/9e75dcba3407e60c6487c99cbb45eabafbf7c698.png)  
相关于：

> <https://github.com/discourse/discourse/commit/28f46c171c18c7c4eb20a591141d42288136c173>
>
> Previously the pull hotlinked images job was skipped after system edits. This en…sured that we never had an infinite loop of system-edit/pull-hotlinked/system-edit/pull-hotlinked etc.
> 
> A side effect was that edits made by system for any other reason (e.g. API, removing full quotes) would prevent pulling hotlinked images. This commit removes the system edit check, and replaces it with another method to avoid an infinite job scheduling loop.

实际上，在已编辑的话题中下载带有 `system` 标记的图片的问题似乎并未完全解决。很抱歉我之前没有注意到这一点。例如这个 [话题](https://zuzu.chat/t/tenda-mw6-mesh-45-32/64875) 或这个 [话题](https://zuzu.chat/t/topic/64467)。

提醒一下，在我们的案例中，这是通过 API 进行的自动分类变更。

我的客户仍然有一些话题，无论怎么做 `PullHotlinkedImages` 都没有任何效果：

- 无论是简单的 `Save Edit`，甚至是
- `Post.update_all(baked_version: nil)` + `Jobs.enqueue_in(10, :pull_hotlinked_images, post_id: <id>)`

不过，如果你用相同的内容创建一个新话题，它就能正常工作。

@david

---

<div class="post-metadata">

**Author:** ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)\
**Post date:** [2020年六月8日 14:07 UTC](https://meta.discourse.org/t/pullhotlinkedimages-does-not-replace-images-again-after-post-is-edited/154184/2 "2020-06-08T14:07:16Z")

</div>

> [@Arkshine](#):
>
> 不过，如果你用相同的内容创建一个新主题，它就能正常工作。

这很奇怪，通过 API 创建的主题一定有什么不同的地方。

帖子具体是怎么创建的？你能分享一下你使用的 API 参数吗？

---

<div class="post-metadata">

**Author:** ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)\
**Post date:** [2020年六月8日 14:24 UTC](https://meta.discourse.org/t/pullhotlinkedimages-does-not-replace-images-again-after-post-is-edited/154184/3 "2020-06-08T14:24:30Z")

</div>

当我提到“新主题”时，我指的是直接在 Discourse 上创建，而不是通过 API。

最初，该主题是由 _WP-Discourse_ 创建的；因此相关代码应该是这个：

> <https://github.com/discourse/wp-discourse/blob/main/lib/discourse-publish.php#L244-L293>

例如，如果你查看上面链接的主题的首次编辑，你会清楚地看到系统替换了图片。因此，WP-Discourse 很可能不是问题所在。

 ![image](https://global.discourse-cdn.com/meta/original/3X/b/0/b00b8edbf4ad97d8cbcf9a98f5ad979cbeb94c52.png)

后来，原始作者更新了 WordPress 帖子，Discourse 主题也随之更新，原始图片又回来了：

 ![image](https://global.discourse-cdn.com/meta/original/3X/1/2/1290adc41ca2c54e9bc792e9a64005fbfe84969a.png)

随后发生了分类变更，从那时起，PullGotlinkedImages 就不再执行任何操作：

 ![image](https://global.discourse-cdn.com/meta/original/3X/d/1/d1d65a4d34cfd746b8dff9efa7555d406c9bea55.png)

* * *

如果你想知道用于分类变更的代码，大致如下：

 ![image](https://global.discourse-cdn.com/meta/original/3X/6/1/61bab4596098c3c4f1189b36dc42ce2e7d8d266d.png)

---

<div class="post-metadata">

**Author:** ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)\
**Post date:** [2020年六月8日 14:34 UTC](https://meta.discourse.org/t/pullhotlinkedimages-does-not-replace-images-again-after-post-is-edited/154184/4 "2020-06-08T14:34:22Z")

</div>

太好了，感谢详细的总结！我认为问题出在这里：

> [@Arkshine](#):
>
> 稍后，原作者更新了 WordPress 帖子，Discourse 话题也随之更新，原始图片也重新出现了：

我成功复现了一个失败的测试用例。看来我们只在图片 **首次** 下载时才执行 URL 替换。如果图片之前已经下载过，就不会再次替换。

这是一个展示该问题的失败测试：

[https://github.com/davidtaylorhq/discourse/blob/27ccf99d162005f82bf4d4e69f860cf78affcfcb/spec/jobs/pull\_hotlinked\_images\_spec.rb#L63-L80](https://github.com/davidtaylorhq/discourse/blob/27ccf99d162005f82bf4d4e69f860cf78affcfcb/spec/jobs/pull_hotlinked_images_spec.rb#L63-L80)

我会将其列入待修复清单。我预计在未来几周内能够着手处理。

---

<div class="post-metadata">

**Author:** ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)\
**Post date:** [2020年六月8日 14:41 UTC](https://meta.discourse.org/t/pullhotlinkedimages-does-not-replace-images-again-after-post-is-edited/154184/5 "2020-06-08T14:41:47Z")

</div>

太棒了！

看来你说得对，我查看了其他主题，发现系统之前确实能正确替换图片。

---

<div class="post-metadata">

**Author:** ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)\
**Post date:** [2020年七月10日 00:16 UTC](https://meta.discourse.org/t/pullhotlinkedimages-does-not-replace-images-again-after-post-is-edited/154184/6 "2020-07-10T00:16:31Z")

</div>

> [@david](#):
>
> 我会把它加入待修复列表。我应该在接下来的几周内能处理。

关于此事有什么最新消息或状态更新吗？

---

<div class="post-metadata">

**Author:** ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)\
**Post date:** [2020年七月10日 08:19 UTC](https://meta.discourse.org/t/pullhotlinkedimages-does-not-replace-images-again-after-post-is-edited/154184/7 "2020-07-10T08:19:00Z")

</div>

还没有，但它仍在列表中！

---

<div class="post-metadata">

**Author:** ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)\
**Post date:** [2020年八月6日 09:03 UTC](https://meta.discourse.org/t/pullhotlinkedimages-does-not-replace-images-again-after-post-is-edited/154184/8 "2020-08-06T09:03:22Z")

</div>

这应该在以下提交后得到解决

> <https://github.com/discourse/discourse/commit/cb12a721c4dd8187dab3eb2b915054a9ac76caa0>
>
> This commit should cause no functional change
> \- Split into functions to avoid d…eep nesting
> \- Register custom field type, and remove manual json parse/serialize
> \- Recover from deleted upload records
> 
> Also adds a test to ensure pull\_hotlinked\_images redownloads secure images only once

我为这种情况[添加了一个测试规范](https://github.com/discourse/discourse/commit/df39e372d707d60812481d9167a8c1086de3b327)，以防止将来出现回归。

---

<div class="post-metadata">

**Author:** ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)\
**Post date:** [2020年八月7日 00:50 UTC](https://meta.discourse.org/t/pullhotlinkedimages-does-not-replace-images-again-after-post-is-edited/154184/9 "2020-08-07T00:50:12Z")

</div>

我刚刚手动快速编辑并保存了几个由 `system` 编辑的主题，看起来 PullHotlinkedImages 现在可以正常工作了。稍后我会尝试命令行操作，但毫无疑问它也会运行正常。

谢谢！

---

<div class="post-metadata">

**Author:** ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)\
**Post date:** [2020年八月8日 17:00 UTC](https://meta.discourse.org/t/pullhotlinkedimages-does-not-replace-images-again-after-post-is-edited/154184/10 "2020-08-08T17:00:02Z")

</div>

本主题在 2 天后自动关闭。不再允许新的回复。
