Class: ActiveStorage::Previewer::OfficePreviewer

Inherits:
ActiveStorage::Previewer
  • Object
show all
Defined in:
lib/active_storage/previewer/office_previewer.rb

Constant Summary collapse

ACCEPTABLE_CONTENT_TYPES =
[
  "application/msword", # .doc
  "application/vnd.openxmlformats-officedocument.wordprocessingml.document", # .docx

  "application/vnd.ms-powerpoint", # .ppt
  "application/vnd.openxmlformats-officedocument.presentationml.presentation", # .pptx

  "application/vnd.ms-excel", # .xls
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" # .xlsx
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.accept?(blob) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# 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?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
# 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_pathObject



28
29
30
# File 'lib/active_storage/previewer/office_previewer.rb', line 28

def soffice_path
  ActiveStorage.paths[:soffice] || "soffice"
end

Instance Method Details

#previewObject



33
34
35
36
37
38
39
# File 'lib/active_storage/previewer/office_previewer.rb', line 33

def preview
  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"
    end
  end
end