Active Storage Previewer
这是一个预览器的抽象基类,它从二进制大对象生成图像。有关具体子类的示例,请参阅 ActiveStorage::Previewer::MuPDFPreviewer
和 ActiveStorage::Previewer::VideoPreviewer
。
- 类 ActiveStorage::Previewer::MuPDFPreviewer
- 类 ActiveStorage::Previewer::PopplerPDFPreviewer
- 类 ActiveStorage::Previewer::VideoPreviewer
属性
[R] | blob |
类公共方法
accept?(blob) 链接
在具体子类中实现此方法。当给定预览器可以从中生成图像的二进制大对象时,让它返回 true。
来源: 显示 | 在 GitHub 上
# File activestorage/lib/active_storage/previewer.rb, line 14 def self.accept?(blob) false end
new(blob) 链接
来源:显示 | 在 GitHub 上
# File activestorage/lib/active_storage/previewer.rb, line 18 def initialize(blob) @blob = blob end
实例公共方法
preview(**options) 链接
在具体子类中覆盖此方法。让它产生一个可附加的预览图像(即 ActiveStorage::Attached::One#attach
接受的任何内容)。将附加选项传递给创建的基础 blob。
来源:显示 | 在 GitHub 上
# File activestorage/lib/active_storage/previewer.rb, line 25 def preview(**options) raise NotImplementedError end
实例私有方法
download_blob_to_tempfile(&block) 链接
将 blob 下载到磁盘上的临时文件中。产生临时文件。
来源:显示 | 在 GitHub 上
# File activestorage/lib/active_storage/previewer.rb, line 31 def download_blob_to_tempfile(&block) # :doc: blob.open tmpdir: tmpdir, &block end
draw(*argv) 链接
执行系统命令,将二进制输出捕获到临时文件中。产生临时文件。
使用此方法向系统库(例如 muPDF 或 FFmpeg)发出 shell 命令,以生成预览图像。产生的临时文件可以用作可附加哈希中的 :io
值
def preview
download_blob_to_tempfile do |input|
draw "my-drawing-command", input.path, "--format", "png", "-" do |output|
yield io: output, filename: "#{blob.filename.base}.png", content_type: "image/png"
end
end
end
输出临时文件在 tmpdir
返回的目录中打开。
来源:显示 | 在 GitHub 上
# File activestorage/lib/active_storage/previewer.rb, line 49 def draw(*argv) # :doc: open_tempfile do |file| instrument :preview, key: blob.key do capture(*argv, to: file) end yield file end end
logger() 链接
来源:显示 | 在 GitHub 上
# File activestorage/lib/active_storage/previewer.rb, line 93 def logger # :doc: ActiveStorage.logger end
tmpdir() 链接
来源:显示 | 在 GitHub 上
# File activestorage/lib/active_storage/previewer.rb, line 97 def tmpdir # :doc: Dir.tmpdir end