From c1b61fc67b70bb3619413c5280fea4947f86b995 Mon Sep 17 00:00:00 2001 From: as-op Date: Mon, 2 Sep 2024 17:12:23 +0200 Subject: [PATCH 1/2] [#57612] Work package Report export fails if work package has unavailable attachments https://community.openproject.org/work_packages/57612 --- app/models/work_package/pdf_export/markdown.rb | 16 ++++++++++++++-- .../pdf_export/work_package_to_pdf_spec.rb | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/models/work_package/pdf_export/markdown.rb b/app/models/work_package/pdf_export/markdown.rb index bfcf94e04010..b03e89cd38de 100644 --- a/app/models/work_package/pdf_export/markdown.rb +++ b/app/models/work_package/pdf_export/markdown.rb @@ -105,12 +105,24 @@ def write_markdown!(work_package, markdown) private + def attachment_image_local_file(attachment) + return nil if attachment.nil? + + attachment.file.local_file + rescue StandardError => e + Rails.logger.error "Failed to access attachment #{attachment.id} file: #{e}" + nil # return nil as if the id was wrong and the attachment obj has not been found + end + def attachment_image_filepath(work_package, src) # images are embedded into markup with the api-path as img.src attachment = attachment_by_api_content_src(work_package, src) - return nil if attachment.nil? || attachment.file.local_file.nil? || !pdf_embeddable?(attachment.content_type) + return nil if attachment.nil? || !pdf_embeddable?(attachment.content_type) + + local_file = attachment_image_local_file(attachment) + return nil if local_file.nil? - resize_image(attachment.file.local_file.path) + resize_image(local_file.path) end def attachment_by_api_content_src(work_package, src) diff --git a/spec/models/work_packages/pdf_export/work_package_to_pdf_spec.rb b/spec/models/work_packages/pdf_export/work_package_to_pdf_spec.rb index d73b936f1282..f898900a00eb 100644 --- a/spec/models/work_packages/pdf_export/work_package_to_pdf_spec.rb +++ b/spec/models/work_packages/pdf_export/work_package_to_pdf_spec.rb @@ -244,6 +244,20 @@ def get_column_value(column_name) end end + describe "with a faulty image" do + before do + # simulate a null pointer exception + # https://appsignal.com/openproject-gmbh/sites/62a6d833d2a5e482c1ef825d/exceptions/incidents/2326/samples/62a6d833d2a5e482c1ef825d-848752493603098719217252846401 + # where attachment data is in the database but the file is missing, corrupted or not accessible + allow(image_attachment).to receive(:file) + .and_return(nil) + end + + it "still finishes the export" do + expect(pdf[:images].length).to eq(0) + end + end + describe "with embedded work package attributes" do let(:supported_work_package_embeds) do [ From 4213c903a9cf73132f62ee8253454c950dafe727 Mon Sep 17 00:00:00 2001 From: as-op Date: Mon, 2 Sep 2024 17:19:06 +0200 Subject: [PATCH 2/2] remove duplicated check --- app/models/work_package/pdf_export/markdown.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/models/work_package/pdf_export/markdown.rb b/app/models/work_package/pdf_export/markdown.rb index b03e89cd38de..a875f800bf89 100644 --- a/app/models/work_package/pdf_export/markdown.rb +++ b/app/models/work_package/pdf_export/markdown.rb @@ -106,8 +106,6 @@ def write_markdown!(work_package, markdown) private def attachment_image_local_file(attachment) - return nil if attachment.nil? - attachment.file.local_file rescue StandardError => e Rails.logger.error "Failed to access attachment #{attachment.id} file: #{e}"