mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-12-27 02:04:33 +08:00
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:
commit
48202c88d1
5 changed files with 59 additions and 0 deletions
|
@ -52,6 +52,8 @@ module Reports
|
||||||
report_link: "<a href='#{report_path}'>#{sanitize_input(report.name)}</a>",
|
report_link: "<a href='#{report_path}'>#{sanitize_input(report.name)}</a>",
|
||||||
team_name: sanitize_input(report.team.name))
|
team_name: sanitize_input(report.team.name))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Reports::DocxPreviewJob.perform_now(report.id)
|
||||||
notification.create_user_notification(user)
|
notification.create_user_notification(user)
|
||||||
ensure
|
ensure
|
||||||
I18n.backend.date_format = nil
|
I18n.backend.date_format = nil
|
||||||
|
|
53
app/jobs/reports/docx_preview_job.rb
Normal file
53
app/jobs/reports/docx_preview_job.rb
Normal 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
|
|
@ -11,6 +11,7 @@ class Report < ApplicationRecord
|
||||||
# ActiveStorage configuration
|
# ActiveStorage configuration
|
||||||
has_one_attached :pdf_file
|
has_one_attached :pdf_file
|
||||||
has_one_attached :docx_file
|
has_one_attached :docx_file
|
||||||
|
has_one_attached :docx_preview_file
|
||||||
|
|
||||||
auto_strip_attributes :name, :description, nullify: false
|
auto_strip_attributes :name, :description, nullify: false
|
||||||
validates :name,
|
validates :name,
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
<div class="file-preview-container">
|
<div class="file-preview-container">
|
||||||
<% if report_type == 'pdf' %>
|
<% if report_type == 'pdf' %>
|
||||||
<%= render partial: 'shared/pdf_viewer.html.erb', locals: { asset: report.pdf_file, report_document: true } %>
|
<%= 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 %>
|
<% else %>
|
||||||
<div>
|
<div>
|
||||||
<i class="fas fa-10x fa-file-word"></i>
|
<i class="fas fa-10x fa-file-word"></i>
|
||||||
|
|
|
@ -7234,6 +7234,7 @@ INSERT INTO "schema_migrations" (version) VALUES
|
||||||
('20210312185911'),
|
('20210312185911'),
|
||||||
('20210325152257'),
|
('20210325152257'),
|
||||||
('20210407143303'),
|
('20210407143303'),
|
||||||
|
('20210410100006'),
|
||||||
('20210506125657');
|
('20210506125657');
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue