mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-12-26 09:42:46 +08:00
Merge pull request #7986 from scinote-eln/revert-7782-revert-7678-ma_SCI_10809
Revert "Revert "Timeout slow libreoffice preview generation [SCI-10809]""
This commit is contained in:
commit
4859557212
5 changed files with 65 additions and 67 deletions
|
@ -16,39 +16,9 @@ class PdfPreviewJob < ApplicationJob
|
|||
|
||||
def perform(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
|
||||
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
|
||||
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'
|
||||
PdfPreviewService.new(asset.file.blob, asset.file_pdf_preview).generate!
|
||||
ensure
|
||||
asset.update(pdf_preview_processing: false)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,39 +15,7 @@ module Reports
|
|||
|
||||
def perform(report_id)
|
||||
report = Report.find(report_id)
|
||||
blob = report.docx_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
|
||||
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'
|
||||
PdfPreviewService.new(report.docx_file.blob, report.docx_preview_file).generate!
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
52
app/services/pdf_preview_service.rb
Normal file
52
app/services/pdf_preview_service.rb
Normal 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
|
|
@ -328,6 +328,9 @@ class Constants
|
|||
|
||||
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 = [
|
||||
'gif', 'jpeg', 'pjpeg', 'png', 'x-png', 'svg+xml', 'bmp', 'tiff', 'jpg'
|
||||
].freeze
|
||||
|
|
|
@ -22,7 +22,12 @@ module ActiveStorage
|
|||
|
||||
begin
|
||||
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)
|
||||
|
|
Loading…
Reference in a new issue