Merge pull request #3350 from aignatov-bio/ai-sci-5601-add-pdf-preview-for-docx-report

Add pdf preview for docx reports [SCI-5601]
This commit is contained in:
Miha Mencin 2021-06-09 11:27:45 +02:00 committed by GitHub
commit 48202c88d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 0 deletions

View file

@ -52,6 +52,8 @@ module Reports
report_link: "<a href='#{report_path}'>#{sanitize_input(report.name)}</a>",
team_name: sanitize_input(report.team.name))
)
Reports::DocxPreviewJob.perform_now(report.id)
notification.create_user_notification(user)
ensure
I18n.backend.date_format = nil

View file

@ -0,0 +1,53 @@
# frozen_string_literal: true
# Provides asynchronous generation of image previews for ActiveStorage::Blob records.
module Reports
class DocxPreviewJob < ApplicationJob
queue_as :reports
discard_on StandardError do |job, error|
Rails.logger.error(
"Couldn't generate PDF preview for DOCX Report with id: #{job.arguments.first}. Error:\n #{error}"
)
end
discard_on ActiveRecord::RecordNotFound
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'
end
end
end

View file

@ -11,6 +11,7 @@ class Report < ApplicationRecord
# ActiveStorage configuration
has_one_attached :pdf_file
has_one_attached :docx_file
has_one_attached :docx_preview_file
auto_strip_attributes :name, :description, nullify: false
validates :name,

View file

@ -11,6 +11,8 @@
<div class="file-preview-container">
<% if report_type == 'pdf' %>
<%= render partial: 'shared/pdf_viewer.html.erb', locals: { asset: report.pdf_file, report_document: true } %>
<% elsif report_type == 'docx' %>
<%= render partial: 'shared/pdf_viewer.html.erb', locals: { asset: report.docx_preview_file, report_document: true } %>
<% else %>
<div>
<i class="fas fa-10x fa-file-word"></i>

View file

@ -7234,6 +7234,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20210312185911'),
('20210325152257'),
('20210407143303'),
('20210410100006'),
('20210506125657');