# 本地获取 onebox 图片

**URL:** <https://meta.discourse.org/t/fetching-onebox-images-locally/172344>\
**Category:** Development\
**Created:** [2020年十二月5日 19:08 UTC](https://meta.discourse.org/t/fetching-onebox-images-locally/172344 "2020-12-05T19:08:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrispanag](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chrispanag/32/105853_2.png) [@chrispanag](https://meta.discourse.org/u/chrispanag)\
**Post date:** [2020年十二月5日 19:08 UTC](https://meta.discourse.org/t/fetching-onebox-images-locally/172344/1 "2020-12-05T19:08:29Z")

</div>

大家好，

我开发了一个插件，用于扩展我们论坛许多用户常用网站的 onebox 功能。

该 onebox 还会显示来自上述网站的图片。请问有没有办法 **将图片本地存储** ，这样即使源网站上的图片发生变化，onebox 也不会失效？

我注意到论坛本身具备此类功能，但无法找到如何在所创建的插件中访问该功能。

非常感谢！😃

* * *

附：[**此处**](https://github.com/chrispanag/rebetiko-sealabs-onebox) 是我开发的插件的 GitHub 仓库。非常欢迎对我的代码提出任何反馈。

（这是我第一次从 NodeJS 后端背景转向使用 Ruby 进行编程。）

---

<div class="post-metadata">

**Author:** ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)\
**Post date:** [2020年十二月5日 21:02 UTC](https://meta.discourse.org/t/fetching-onebox-images-locally/172344/2 "2020-12-05T21:02:24Z")

</div>

Onebox 和 Discourse 原生即可实现此功能，无需任何额外插件。

你只需确保目标网站生成正确的 og 元标签。如果需要修改，应针对目标网站进行调整，而非 Discourse，这样才能实现预期效果。

例如：

> **[Brexit: UK-EU trade talks to resume over 'critical issues'](https://www.bbc.co.uk/news/uk-politics-55201085)**
>
> Negotiators will talk on Sunday, but "significant differences" between the two sides in post-Brexit trade talks remain.

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

BBC 网站为每个页面生成这些元标签。Discourse 和 Onebox 将提取 site\_name、image、description 和 title，并自动生成 Onebox 预览。

只要确保你的目标网站遵循该标准，通常无需再做其他操作。

这样做是合理的，因为这样你的网站在其他平台（如 Facebook）的预览也能得到覆盖。

我假设你对该网站拥有控制权或影响力，但这可能是一个较大的假设！

如果无法实现上述方案，你则需要在 Onebox 中开发一个定制引擎，以识别该网站的独特性并抓取相应的元数据。Nokogiri 库是完成此项工作的合适工具。

---

<div class="post-metadata">

**Author:** ![chrispanag](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chrispanag/32/105853_2.png) [@chrispanag](https://meta.discourse.org/u/chrispanag)\
**Post date:** [2020年十二月5日 21:13 UTC](https://meta.discourse.org/t/fetching-onebox-images-locally/172344/3 "2020-12-05T21:13:41Z")

</div>

> [@merefield](#):
>
> 你只需确保目标网站生成正确的 og 元标签。

它并没有：sweat\_smile:

> [@merefield](#):
>
> 我假设你拥有或能影响该网站的控制权，不过这可能是一个很大的假设！

不幸的是不行：/ 此外，我还想稍微自定义一下 onebox 视图。如果使用默认引擎，这是无法实现的。

不过……  
我已经开发了 [onebox 扩展](https://github.com/chrispanag/rebetiko-sealabs-onebox)。它完成了所有获取工作，运行效果很好！

问题是： **如何确保获取到的图片会被本地存储？** 我猜需要在插件中编写一些额外的代码。

---

<div class="post-metadata">

**Author:** ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)\
**Post date:** [2020年十二月5日 21:39 UTC](https://meta.discourse.org/t/fetching-onebox-images-locally/172344/4 "2020-12-05T21:39:25Z")

</div>

一些可供探索的途径：

Cooked Post Processor 会为此排队一个任务，详见：

> <https://github.com/discourse/discourse/blob/e98c7b15d6ce864f1b5e83b6881700eaa808bdd1/lib/cooked_post_processor.rb#L663>

相关任务代码位于：[discourse/app/jobs/regular/pull\_hotlinked\_images.rb at e98c7b15d6ce864f1b5e83b6881700eaa808bdd1 · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/e98c7b15d6ce864f1b5e83b6881700eaa808bdd1/app/jobs/regular/pull_hotlinked_images.rb)

如果你已成功将热链图片恢复至帖子中，且图片符合相关条件，同时设置已确保外部图片被上传，那么此功能应能正常工作。

![image](https://global.discourse-cdn.com/meta/original/3X/6/4/64ee7aa37153c77509a67839ecc7a836cad74f16.png)

相关条件见：[discourse/lib/cooked\_post\_processor.rb at e98c7b15d6ce864f1b5e83b6881700eaa808bdd1 · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/e98c7b15d6ce864f1b5e83b6881700eaa808bdd1/lib/cooked_post_processor.rb#L201)

如果此功能尚未生效，但你已能在一框（onebox）中成功显示外部图片，我的推测是这些图片尚未满足上传所需的条件。
