Active Storage 预览¶ ↑
某些非图像 blob 可以被预览:也就是说,它们可以呈现为图像。视频 blob 可以通过提取其第一帧来预览,PDF blob 可以通过提取其第一页来预览。
Previewer 从 blob 中提取预览图像。Active Storage 为视频和 PDF 提供了 previewer。 ActiveStorage::Previewer::VideoPreviewer 用于视频,而 ActiveStorage::Previewer::PopplerPDFPreviewer 和 ActiveStorage::Previewer::MuPDFPreviewer 用于 PDF。通过继承 ActiveStorage::Previewer 并实现必要的方法来构建自定义 previewer。有关 previewer 所需内容的更多详细信息,请参阅 ActiveStorage::Previewer 文档。
要选择 blob 的 previewer,Active Storage 会按顺序调用每个已注册 previewer 的 accept? 方法。它会使用第一个 accept? 方法在给定 blob 时返回 true 的 previewer。在 Rails 应用程序中,通过在初始化器中操作 Rails.application.config.active_storage.previewers 来添加或删除 previewer。
Rails.application.config.active_storage.previewers # => [ ActiveStorage::Previewer::PopplerPDFPreviewer, ActiveStorage::Previewer::MuPDFPreviewer, ActiveStorage::Previewer::VideoPreviewer ] # Add a custom previewer for Microsoft Office documents: Rails.application.config.active_storage.previewers << DOCXPreviewer # => [ ActiveStorage::Previewer::PopplerPDFPreviewer, ActiveStorage::Previewer::MuPDFPreviewer, ActiveStorage::Previewer::VideoPreviewer, DOCXPreviewer ]
在 Rails 应用程序之外,请改用 ActiveStorage.previewers。
内置的 previewer 依赖于第三方系统库。具体来说,内置视频 previewer 需要 FFmpeg。提供了两个 PDF previewer:一个需要 Poppler,另一个需要 muPDF(1.8 或更高版本)。要预览 PDF,请安装 Poppler 或 muPDF。
这些库不是由 Rails 提供的。您必须自行安装它们才能使用内置的 previewer。在安装和使用第三方软件之前,请确保您了解这样做的许可影响。
- D
- I
- K
- N
- P
- U
Attributes
| [R] | blob | |
| [R] | 变体 (variation) |
类公共方法
new(blob, variation_or_variation_key) 链接
Source: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/preview.rb, line 42 def initialize(blob, variation_or_variation_key) @blob, @variation = blob, ActiveStorage::Variation.wrap(variation_or_variation_key) end
实例公共方法
download(&block) 链接
下载与此预览的变体关联的文件。如果未提供块,则整个文件将被读取到内存中并返回。对于非常大的文件,这将占用大量内存。如果提供了块,则下载将以块的形式流式传输并给出。如果预览尚未处理,则引发 ActiveStorage::Preview::UnprocessedError。
Source: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/preview.rb, line 90 def download(&block) if processed? presentation.download(&block) else raise UnprocessedError end end
image() 链接
返回 blob 的已附加预览图像。
Source: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/preview.rb, line 59 def image blob.preview_image end
key() 链接
返回 blob 和变体的组合键,它们共同标识特定的变体。
Source: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/preview.rb, line 77 def key if processed? presentation.key else raise UnprocessedError end end
processed() 链接
如果预览尚未处理,则处理预览。为了方便起见,返回接收到的 ActiveStorage::Preview 实例。
blob.preview(resize_to_limit: [100, 100]).processed.url
处理预览会从其 blob 生成图像,并将预览图像附加到 blob。由于预览图像与 blob 一起存储,因此仅生成一次。
Source: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/preview.rb, line 52 def processed process unless processed? variant.processed if variant? self end
url(**options) 链接
返回服务上预览的变体的 URL。如果预览尚未处理,则引发 ActiveStorage::Preview::UnprocessedError。
此方法会同步处理预览图像的变体,因此请勿在视图中调用它。而是生成一个稳定的 URL,该 URL 会重定向到此方法返回的 URL。
Source: 显示 | 在 GitHub 上
# File activestorage/app/models/active_storage/preview.rb, line 68 def url(**options) if processed? presentation.url(**options) else raise UnprocessedError end end