From 79dd20f1071551f672ff7e3307229675affd18a6 Mon Sep 17 00:00:00 2001 From: PiTrem Date: Wed, 2 Oct 2024 16:25:28 +0200 Subject: [PATCH] fix: returning attachment preview when not annotated (#2192) when the attachment does not have annotation derivative calling /api/v1/attachments/{attachment id}/annotated_image raises TODO: check why the calls are made for non annotated attachment --- app/api/chemotion/attachment_api.rb | 41 ++++++++++------------------- app/models/attachment.rb | 12 ++++++++- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/app/api/chemotion/attachment_api.rb b/app/api/chemotion/attachment_api.rb index c95b1dc3b6..c7c1c4d16a 100644 --- a/app/api/chemotion/attachment_api.rb +++ b/app/api/chemotion/attachment_api.rb @@ -219,31 +219,19 @@ def remove_duplicated(att) desc 'get_annotated_image_of_attachment' get ':attachment_id/annotated_image' do content_type 'application/octet-stream' - env['api.format'] = :binary - store = @attachment.attachment.storage.directory - file_location = store.join( - @attachment.attachment_data['derivatives']['annotation']['annotated_file_location'] || 'not available', - ) - - uploaded_file = if file_location.present? && File.file?(file_location) - extension_of_annotation = File.extname(@attachment.filename) - extension_of_annotation = '.png' if @attachment.attachment.mime_type == 'image/tiff' - filename_of_annotated_image = @attachment.filename.gsub( - File.extname(@attachment.filename), - "_annotated#{extension_of_annotation}", - ) - header['Content-Disposition'] = "attachment; filename=\"#{filename_of_annotated_image}\"" - File.open(file_location) - else - header['Content-Disposition'] = "attachment; filename=\"#{@attachment.filename}\"" - @attachment.attachment_attacher.file - end - data = uploaded_file.read - uploaded_file.close - - data + annotation = @attachment.annotated_file_location.presence + if annotation.present? && File.file?(annotation) + header['Content-Disposition'] = "attachment; filename=\"#{@attachment.annotated_filename}\"" + file = File.open(annotation) + else + header['Content-Disposition'] = "attachment; filename=\"#{@attachment.filename}\"" + file = @attachment.attachment_attacher.file + end + file.read + ensure + file&.close end desc 'update_annotation_of_attachment' @@ -314,13 +302,12 @@ def remove_duplicated(att) next unless att.annotated? begin - annotated_file_name = "#{File.basename(att.filename, '.*')}_annotated#{File.extname(att.filename)}" - zip.put_next_entry annotated_file_name + zip.put_next_entry att.annotated_filename file = File.open(att.annotated_file_location) zip.write file.read - file_text += "#{annotated_file_name} #{file.size}\n" + file_text += "#{att.annotated_filename} #{file.size}\n" ensure - file.close + file&.close end end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 19149d22e6..3cf9df1be3 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -237,7 +237,7 @@ def type_image_tiff? def annotated? # attachment['derivatives'].present? && attachment['derivatives']['annotation'].present? - attachment_data&.dig('derivatives', 'annotation', 'annotated_file_location')&.present? || false + attachment_data&.dig('derivatives', 'annotation', 'annotated_file_location').present? || false end def annotated_image? @@ -249,6 +249,7 @@ def type_pdf? attachment['mime_type'].to_s == 'application/pdf' end + # @return [String] the path to the combined image file on disk def annotated_file_location return '' unless annotated? @@ -258,6 +259,15 @@ def annotated_file_location ) end + # @return [String] build annotation file name based on the original file name + def annotated_filename + return '' unless annotated? + + # NB: original tiff file are converted to png for the annotation background layer + extension_of_annotation = content_type == 'image/tiff' ? '.png' : File.extname(filename) + "#{File.basename(filename, '.*')}_annotated#{extension_of_annotation}" + end + def preview "data:image/png;base64,#{Base64.encode64(read_thumbnail)}" if thumb end