class ActiveStorage::Previewer::OfficePreviewer

Constants

ACCEPTABLE_CONTENT_TYPES

Public Class Methods

accept?(blob) click to toggle source
# File lib/active_storage/previewer/office_previewer.rb, line 16
def accept?(blob)
  blob.content_type.in?(ACCEPTABLE_CONTENT_TYPES) && soffice_exists?
end
soffice_exists?() click to toggle source
# File lib/active_storage/previewer/office_previewer.rb, line 20
def soffice_exists?
  if @soffice_exists.nil?
    @soffice_exists = system(soffice_path, "--version", out: File::NULL, err: File::NULL)
  else
    @soffice_exists
  end
end
soffice_path() click to toggle source
# File lib/active_storage/previewer/office_previewer.rb, line 28
def soffice_path
  ActiveStorage.paths[:soffice] || "soffice"
end

Public Instance Methods

preview(**options) { |io: output, filename: "#{filename.base}.png", content_type: "image/png", **options| ... } click to toggle source
# File lib/active_storage/previewer/office_previewer.rb, line 33
def preview(**options)
  download_blob_to_tempfile do |input|
    draw_poster_image_from input do |output|
      yield io: output, filename: "#{blob.filename.base}.png", content_type: "image/png", **options
    end
  end
end

Private Instance Methods

consume(path, &block) click to toggle source
# File lib/active_storage/previewer/office_previewer.rb, line 48
def consume(path, &block)
  File.open(path, "rb", &block)
ensure
  File.delete(path)
end
draw_poster_image_from(file, &block) click to toggle source
# File lib/active_storage/previewer/office_previewer.rb, line 42
def draw_poster_image_from(file, &block)
  system soffice_path, "--headless", "--invisible", "--convert-to", "png",
    "--outdir", tmpdir, file.path, out: File::NULL, err: File::NULL
  consume "#{tmpdir}/#{File.basename(file.path, ".*")}.png", &block
end
soffice_path() click to toggle source
# File lib/active_storage/previewer/office_previewer.rb, line 54
def soffice_path
  self.class.soffice_path
end