mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-12 01:46:55 +08:00
52 lines
1.4 KiB
Ruby
52 lines
1.4 KiB
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
module ActiveStorage
|
||
|
class Previewer
|
||
|
class LibreofficePreviewer < Previewer
|
||
|
class << self
|
||
|
include ActiveStorageFileUtil
|
||
|
|
||
|
def accept?(blob)
|
||
|
previewable_document?(blob)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def preview
|
||
|
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
|
||
|
ENV['LIBREOFFICE_PATH'] || 'libreoffice'
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|