2019-07-10 20:37:23 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ActiveStorage
|
|
|
|
class Previewer
|
|
|
|
class LibreofficePreviewer < Previewer
|
|
|
|
class << self
|
|
|
|
include ActiveStorageFileUtil
|
|
|
|
|
|
|
|
def accept?(blob)
|
|
|
|
previewable_document?(blob)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-28 21:43:45 +08:00
|
|
|
def preview(**_options)
|
2019-07-10 20:37:23 +08:00
|
|
|
download_blob_to_tempfile do |input|
|
|
|
|
work_dir = File.dirname(input.path)
|
|
|
|
basename = File.basename(input.path, '.*')
|
|
|
|
preview_file = File.join(work_dir, "#{basename}.png")
|
|
|
|
|
|
|
|
Rails.logger.info "Starting preparing document preview for file #{blob.filename.sanitized}..."
|
|
|
|
|
|
|
|
begin
|
|
|
|
success = system(
|
|
|
|
"#{libreoffice_path} --headless --invisible --convert-to png --outdir #{work_dir} #{input.path}"
|
|
|
|
)
|
|
|
|
|
|
|
|
unless success && File.file?(preview_file)
|
|
|
|
raise StandardError, "There was an error generating document preview, blob id: #{blob.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
yield io: File.open(preview_file), filename: "#{blob.filename.base}.png", content_type: 'image/png'
|
|
|
|
|
|
|
|
Rails.logger.info "Finished preparing document preview for file #{blob.filename.sanitized}."
|
|
|
|
ensure
|
|
|
|
File.delete(preview_file) if File.file?(preview_file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def tempdir
|
|
|
|
Rails.root.join('tmp')
|
|
|
|
end
|
|
|
|
|
|
|
|
def libreoffice_path
|
2019-10-07 22:12:55 +08:00
|
|
|
ENV['LIBREOFFICE_PATH'] || 'soffice'
|
2019-07-10 20:37:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|