class ActiveStorage::Previewer

examples of concrete subclasses.
ActiveStorage::Previewer::MuPDFPreviewer and ActiveStorage::Previewer::VideoPreviewer for
This is an abstract base class for previewers, which generate images from blobs. See

def self.accept?(blob)

the previewer can generate an image.
Implement this method in a concrete subclass. Have it return true when given a blob from which
def self.accept?(blob)
  false
end

def capture(*argv, to:)

def capture(*argv, to:)
  to.binmode
  IO.popen(argv, err: File::NULL) { |out| IO.copy_stream(out, to) }
  to.rewind
end

def draw(*argv) #:doc:

:doc:
The output tempfile is opened in the directory returned by ActiveStorage::Downloading#tempdir.

end
end
end
yield io: output, filename: "#{blob.filename.base}.png", content_type: "image/png"
draw "my-drawing-command", input.path, "--format", "png", "-" do |output|
download_blob_to_tempfile do |input|
def preview

generation. The resulting tempfile can be used as the +:io+ value in an attachable Hash:
Use this method to shell out to a system library (e.g. mupdf or ffmpeg) for preview image

Executes a system command, capturing its binary output in a tempfile. Yields the tempfile.
def draw(*argv) #:doc:
  ActiveSupport::Notifications.instrument("preview.active_storage") do
    open_tempfile_for_drawing do |file|
      capture(*argv, to: file)
      yield file
    end
  end
end

def initialize(blob)

def initialize(blob)
  @blob = blob
end

def logger #:doc:

:doc:
def logger #:doc:
  ActiveStorage.logger
end

def open_tempfile_for_drawing

def open_tempfile_for_drawing
  tempfile = Tempfile.open("ActiveStorage", tempdir)
  begin
    yield tempfile
  ensure
    tempfile.close!
  end
end

def preview

anything accepted by ActiveStorage::Attached::One#attach).
Override this method in a concrete subclass. Have it yield an attachable preview image (i.e.
def preview
  raise NotImplementedError
end