Class: ActiveStorage::Preview
- Inherits:
-
Object
- Object
- ActiveStorage::Preview
- Includes:
- Blob::Servable
- Defined in:
- app/models/active_storage/preview.rb
Overview
Active Storage Preview
Some non-image blobs can be previewed: that is, they can be presented as images. A video blob can be previewed by extracting its first frame, and a PDF blob can be previewed by extracting its first page.
A previewer extracts a preview image from a blob. Active Storage provides previewers for videos and PDFs. ActiveStorage::Previewer::VideoPreviewer is used for videos whereas ActiveStorage::Previewer::PopplerPDFPreviewer and ActiveStorage::Previewer::MuPDFPreviewer are used for PDFs. Build custom previewers by subclassing ActiveStorage::Previewer and implementing the requisite methods. Consult the ActiveStorage::Previewer documentation for more details on what’s required of previewers.
To choose the previewer for a blob, Active Storage calls accept?
on each registered previewer in order. It uses the first previewer for which accept?
returns true when given the blob. In a Rails application, add or remove previewers by manipulating Rails.application.config.active_storage.previewers
in an initializer:
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 ]
Outside of a Rails application, modify ActiveStorage.previewers
instead.
The built-in previewers rely on third-party system libraries. Specifically, the built-in video previewer requires FFmpeg. Two PDF previewers are provided: one requires Poppler, and the other requires muPDF (version 1.8 or newer). To preview PDFs, install either Poppler or muPDF.
These libraries are not provided by Rails. You must install them yourself to use the built-in previewers. Before you install and use third-party software, make sure you understand the licensing implications of doing so.
Defined Under Namespace
Classes: UnprocessedError
Instance Attribute Summary collapse
-
#blob ⇒ Object
readonly
Returns the value of attribute blob.
-
#variation ⇒ Object
readonly
Returns the value of attribute variation.
Instance Method Summary collapse
-
#download(&block) ⇒ Object
Downloads the file associated with this preview’s variant.
-
#image ⇒ Object
Returns the blob’s attached preview image.
-
#initialize(blob, variation_or_variation_key) ⇒ Preview
constructor
A new instance of Preview.
-
#key ⇒ Object
Returns a combination key of the blob and the variation that together identifies a specific variant.
-
#processed ⇒ Object
Processes the preview if it has not been processed yet.
-
#url(**options) ⇒ Object
Returns the URL of the preview’s variant on the service.
Methods included from Blob::Servable
#content_type_for_serving, #forced_disposition_for_serving
Constructor Details
#initialize(blob, variation_or_variation_key) ⇒ Preview
Returns a new instance of Preview.
42 43 44 |
# File '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 |
Instance Attribute Details
#blob ⇒ Object (readonly)
Returns the value of attribute blob.
40 41 42 |
# File 'app/models/active_storage/preview.rb', line 40 def blob @blob end |
#variation ⇒ Object (readonly)
Returns the value of attribute variation.
40 41 42 |
# File 'app/models/active_storage/preview.rb', line 40 def variation @variation end |
Instance Method Details
#download(&block) ⇒ Object
Downloads the file associated with this preview’s variant. If no block is given, the entire file is read into memory and returned. That’ll use a lot of RAM for very large files. If a block is given, then the download is streamed and yielded in chunks. Raises ActiveStorage::Preview::UnprocessedError if the preview has not been processed yet.
90 91 92 93 94 95 96 |
# File 'app/models/active_storage/preview.rb', line 90 def download(&block) if processed? presentation.download(&block) else raise UnprocessedError end end |
#image ⇒ Object
Returns the blob’s attached preview image.
59 60 61 |
# File 'app/models/active_storage/preview.rb', line 59 def image blob.preview_image end |
#key ⇒ Object
Returns a combination key of the blob and the variation that together identifies a specific variant.
77 78 79 80 81 82 83 |
# File 'app/models/active_storage/preview.rb', line 77 def key if processed? presentation.key else raise UnprocessedError end end |
#processed ⇒ Object
Processes the preview if it has not been processed yet. Returns the receiving ActiveStorage::Preview
instance for convenience:
blob.preview(resize_to_limit: [100, 100]).processed.url
Processing a preview generates an image from its blob and attaches the preview image to the blob. Because the preview image is stored with the blob, it is only generated once.
52 53 54 55 56 |
# File 'app/models/active_storage/preview.rb', line 52 def processed process unless processed? variant.processed if variant? self end |
#url(**options) ⇒ Object
Returns the URL of the preview’s variant on the service. Raises ActiveStorage::Preview::UnprocessedError if the preview has not been processed yet.
This method synchronously processes a variant of the preview image, so do not call it in views. Instead, generate a stable URL that redirects to the URL returned by this method.
68 69 70 71 72 73 74 |
# File 'app/models/active_storage/preview.rb', line 68 def url(**) if processed? presentation.url(**) else raise UnprocessedError end end |