Skip to content

Commit

Permalink
fix: returning attachment preview when not annotated (#2192)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
PiTrem authored Oct 2, 2024
1 parent 323b1cb commit 79dd20f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
41 changes: 14 additions & 27 deletions app/api/chemotion/attachment_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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

Expand Down
12 changes: 11 additions & 1 deletion app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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?

Expand All @@ -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
Expand Down

0 comments on commit 79dd20f

Please sign in to comment.