Revert "Revert "Timeout slow libreoffice preview generation [SCI-10809]""

This commit is contained in:
Martin Artnik 2024-10-04 12:30:18 +02:00 committed by GitHub
parent 9c288c2021
commit 2f0303b2dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 65 additions and 67 deletions

View file

@ -16,39 +16,9 @@ class PdfPreviewJob < ApplicationJob
def perform(asset_id) def perform(asset_id)
asset = Asset.find(asset_id) asset = Asset.find(asset_id)
blob = asset.file.blob
blob.open(tmpdir: tempdir) do |input|
work_dir = File.dirname(input.path)
preview_filename = "#{File.basename(input.path, '.*')}.pdf"
preview_file = File.join(work_dir, preview_filename)
Rails.logger.info "Starting preparing document preview for file #{blob.filename.sanitized}..."
ActiveRecord::Base.transaction do PdfPreviewService.new(asset.file.blob, asset.file_pdf_preview).generate!
success = system( ensure
libreoffice_path, '--headless', '--invisible', '--convert-to', 'pdf', '--outdir', work_dir, input.path asset.update(pdf_preview_processing: false)
)
unless success && File.file?(preview_file)
raise StandardError, "There was an error generating PDF preview, blob id: #{blob.id}"
end
ActiveRecord::Base.no_touching do
asset.file_pdf_preview.attach(io: File.open(preview_file), filename: "#{blob.filename.base}.pdf")
asset.update(pdf_preview_processing: false)
end
Rails.logger.info("Finished preparing PDF preview for file #{blob.filename.sanitized}.")
end
ensure
File.delete(preview_file) if File.file?(preview_file)
end
end
private
def tempdir
Rails.root.join('tmp')
end
def libreoffice_path
ENV['LIBREOFFICE_PATH'] || 'soffice'
end end
end end

View file

@ -15,39 +15,7 @@ module Reports
def perform(report_id) def perform(report_id)
report = Report.find(report_id) report = Report.find(report_id)
blob = report.docx_file.blob PdfPreviewService.new(report.docx_file.blob, report.docx_preview_file).generate!
blob.open(tmpdir: tempdir) do |input|
work_dir = File.dirname(input.path)
preview_filename = "#{File.basename(input.path, '.*')}.pdf"
preview_file = File.join(work_dir, preview_filename)
Rails.logger.info "Starting preparing document preview for file #{blob.filename.sanitized}..."
ActiveRecord::Base.transaction do
success = system(
libreoffice_path, '--headless', '--invisible', '--convert-to', 'pdf', '--outdir', work_dir, input.path
)
unless success && File.file?(preview_file)
raise StandardError, "There was an error generating PDF preview, blob id: #{blob.id}"
end
ActiveRecord::Base.no_touching do
report.docx_preview_file.attach(io: File.open(preview_file), filename: "#{blob.filename.base}.pdf")
end
Rails.logger.info("Finished preparing PDF preview for file #{blob.filename.sanitized}.")
end
ensure
File.delete(preview_file) if File.file?(preview_file)
end
end
private
def tempdir
Rails.root.join('tmp')
end
def libreoffice_path
ENV['LIBREOFFICE_PATH'] || 'soffice'
end end
end end
end end

View file

@ -0,0 +1,52 @@
# frozen_string_literal: true
class PdfPreviewService
def initialize(blob, attached)
@blob = blob
@attached = attached
end
def generate!
preview_file = nil
@blob.open(tmpdir: tempdir) do |input|
work_dir = File.dirname(input.path)
preview_filename = "#{File.basename(input.path, '.*')}.pdf"
preview_file = File.join(work_dir, preview_filename)
Rails.logger.info "Starting preparing document preview for file #{@blob.filename.sanitized}..."
ActiveRecord::Base.transaction do
success = system(
'timeout',
Constants::PREVIEW_TIMEOUT_SECONDS.to_s,
libreoffice_path,
'--headless',
'--invisible',
'--convert-to',
'pdf', '--outdir',
work_dir, input.path
)
unless success && File.file?(preview_file)
raise StandardError, "There was an error generating PDF preview, blob id: #{@blob.id}"
end
ActiveRecord::Base.no_touching do
attached.attach(io: File.open(preview_file), filename: "#{@blob.filename.base}.pdf")
end
Rails.logger.info("Finished preparing PDF preview for file #{@blob.filename.sanitized}.")
end
end
ensure
File.delete(preview_file) if File.file?(preview_file)
end
private
def tempdir
Rails.root.join('tmp')
end
def libreoffice_path
ENV['LIBREOFFICE_PATH'] || 'soffice'
end
end

View file

@ -328,6 +328,9 @@ class Constants
PREVIEWABLE_FILE_TYPES = TEXT_EXTRACT_FILE_TYPES PREVIEWABLE_FILE_TYPES = TEXT_EXTRACT_FILE_TYPES
# default preview timeout to 15 minutes
PREVIEW_TIMEOUT_SECONDS = ENV['PREVIEW_TIMEOUT_SECONDS'] ? ENV['PREVIEW_TIMEOUT_SECONDS'].to_i : 900
WHITELISTED_IMAGE_TYPES = [ WHITELISTED_IMAGE_TYPES = [
'gif', 'jpeg', 'pjpeg', 'png', 'x-png', 'svg+xml', 'bmp', 'tiff', 'jpg' 'gif', 'jpeg', 'pjpeg', 'png', 'x-png', 'svg+xml', 'bmp', 'tiff', 'jpg'
].freeze ].freeze

View file

@ -22,7 +22,12 @@ module ActiveStorage
begin begin
success = system( success = system(
libreoffice_path, '--headless', '--invisible', '--convert-to', 'png', '--outdir', work_dir, input.path 'timeout',
Constants::PREVIEW_TIMEOUT_SECONDS.to_s,
libreoffice_path,
'--headless', '--invisible', '--convert-to', 'png', '--outdir',
work_dir,
input.path
) )
unless success && File.file?(preview_file) unless success && File.file?(preview_file)