diff --git a/app/jobs/pdf_preview_job.rb b/app/jobs/pdf_preview_job.rb index 8ed34ecc9..6c9bcd19b 100644 --- a/app/jobs/pdf_preview_job.rb +++ b/app/jobs/pdf_preview_job.rb @@ -25,7 +25,7 @@ class PdfPreviewJob < ApplicationJob ActiveRecord::Base.transaction do success = system( - "#{libreoffice_path} --headless --invisible --convert-to pdf --outdir #{work_dir} #{input.path}" + 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}" diff --git a/app/jobs/reports/docx_job.rb b/app/jobs/reports/docx_job.rb index 3be28c252..8a092a248 100644 --- a/app/jobs/reports/docx_job.rb +++ b/app/jobs/reports/docx_job.rb @@ -6,6 +6,14 @@ module Reports queue_as :reports + discard_on StandardError do |job, error| + report = Report.find_by(id: job.arguments.first) + ActiveRecord::Base.no_touching do + report&.update(docx_file_processing: false) + end + Rails.logger.error("Couldn't generate DOCX for Report with id: #{job.arguments.first}. Error:\n #{error}") + end + def perform(report, user, team, root_url) file = Tempfile.new(['report', '.docx']) begin diff --git a/app/jobs/reports/pdf_job.rb b/app/jobs/reports/pdf_job.rb index 353f980b4..0a6b5be38 100644 --- a/app/jobs/reports/pdf_job.rb +++ b/app/jobs/reports/pdf_job.rb @@ -7,6 +7,16 @@ module Reports queue_as :reports + discard_on StandardError do |job, error| + report = Report.find_by(id: job.arguments.first) + ActiveRecord::Base.no_touching do + report&.update(pdf_file_processing: false) + end + Rails.logger.error("Couldn't generate PDF for Report with id: #{job.arguments.first}. Error:\n #{error}") + end + + PREVIEW_EXTENSIONS = %w(docx pdf).freeze + def perform(report, template, user) file = Tempfile.new(['report', '.pdf'], binmode: true) begin @@ -36,6 +46,7 @@ module Reports ) file.rewind + file = append_result_asset_previews(report, file) if report.settings.dig(:task, :file_results_previews) report.pdf_file.attach(io: file, filename: 'report.pdf') report.update!(pdf_file_processing: false) @@ -53,5 +64,42 @@ module Reports file.unlink end end + + private + + def append_result_asset_previews(report, report_file) + Dir.mktmpdir do |tmp_dir| + report.report_elements.my_module.each do |my_module_element| + my_module_element.my_module.results.each do |result| + next unless result.is_asset && PREVIEW_EXTENSIONS.include?(result.asset.file.blob.filename.extension) + + asset = result.asset + unless asset.file_pdf_preview.attached? + PdfPreviewJob.perform_now(asset.id) + asset.reload + end + asset.file_pdf_preview.open(tmpdir: tmp_dir) do |file| + report_file = merge_pdf_files(file, report_file) + end + end + end + end + report_file + end + + def merge_pdf_files(file, report_file) + merged_file = Tempfile.new(['report', '.pdf'], binmode: true) + success = system( + 'pdfunite', report_file.path, file.path, merged_file.path + ) + + unless success && File.file?(merged_file) + raise StandardError, 'There was an error merging report and PDF file preview' + end + + report_file.close + report_file.unlink + merged_file + end end end diff --git a/app/services/report_actions/report_content.rb b/app/services/report_actions/report_content.rb index 7aa215315..9a97ed355 100644 --- a/app/services/report_actions/report_content.rb +++ b/app/services/report_actions/report_content.rb @@ -29,9 +29,11 @@ module ReportActions # Delete existing template values @report.report_template_values.destroy_all - formatted_template_values = @template_values.as_json.map { |k, v| v['name'] = k; v } - # Save new template values - @report.report_template_values.create!(formatted_template_values) + if @template_values.present? + formatted_template_values = @template_values.as_json.map { |k, v| v['name'] = k; v } + # Save new template values + @report.report_template_values.create!(formatted_template_values) + end end @report diff --git a/app/views/reports/elements/_my_module_result_asset_element.html.erb b/app/views/reports/elements/_my_module_result_asset_element.html.erb index 3359e4e5c..5ca2ff75b 100644 --- a/app/views/reports/elements/_my_module_result_asset_element.html.erb +++ b/app/views/reports/elements/_my_module_result_asset_element.html.erb @@ -37,6 +37,9 @@
<%= t("projects.reports.elements.result_asset.user_time", user: result.user.full_name, timestamp: l(timestamp, format: :full)) %> + <% if report.settings.dig(:task, :file_results_previews) %> + <%= t('projects.reports.elements.result_asset.full_preview_attached') %> + <% end %>
<%= render partial: "reports/elements/element_controls.html.erb" %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 5f0fba8fe..abe7cd1a5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -728,6 +728,7 @@ en: result_asset: file_name: "[ %{file} ]" user_time: "Uploaded by %{user} on %{timestamp}." + full_preview_attached: "[full document attached to report]" result_table: table_name: "[ %{name} ]" user_time: "Created by %{user} on %{timestamp}." diff --git a/spec/controllers/reports_controller_spec.rb b/spec/controllers/reports_controller_spec.rb index 2e8a9ce27..282871c7f 100644 --- a/spec/controllers/reports_controller_spec.rb +++ b/spec/controllers/reports_controller_spec.rb @@ -9,9 +9,10 @@ describe ReportsController, type: :controller do let!(:team) { create :team, created_by: user } let!(:user_team) { create :user_team, team: team, user: user } let(:user_project) { create :user_project, :owner, user: user } - let(:project) do - create :project, team: team, user_projects: [user_project] - end + let(:project) { create :project, team: team, user_projects: [user_project] } + let(:experiment) { create :experiment, project: project } + let(:my_module1) { create :my_module, experiment: experiment } + let(:my_module2) { create :my_module, experiment: experiment } let(:report) do create :report, user: user, project: project, team: team, name: 'test repot A1', description: 'test description A1' @@ -23,9 +24,10 @@ describe ReportsController, type: :controller do let(:params) do { project_id: project.id, report: { name: 'test report created', - description: 'test description created' }, - report_contents: '[{"type_of":"project_header","id":{"project_id":' + - project.id.to_s + '},"sort_order":null,"children":[]}]' } + description: 'test description created', + settings: Report::DEFAULT_SETTINGS }, + project_content: { experiments: { experiment.id => [my_module1.id] } }, + template_values: [] } end it 'calls create activity service' do @@ -49,8 +51,8 @@ describe ReportsController, type: :controller do id: report.id, report: { name: 'test report update', description: 'test description update' }, - report_contents: '[{"type_of":"project_header","id":{"project_id":' + - project.id.to_s + '},"sort_order":null,"children":[]}]' } + project_content: { experiments: { experiment.id => [my_module2.id] } }, + template_values: [] } end it 'calls create activity service' do expect(Activities::CreateActivityService).to receive(:call)