From 7052c767ed9e0c85026b8833a271513e3332de74 Mon Sep 17 00:00:00 2001 From: Andrej Date: Mon, 8 Apr 2024 14:57:14 +0200 Subject: [PATCH] Improve handling of tinymce images for pdf reports [SCI-10522] --- app/models/concerns/tiny_mce_images.rb | 21 ++++++++++++++++++++- app/models/tiny_mce_asset.rb | 8 -------- config/initializers/constants.rb | 2 +- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/models/concerns/tiny_mce_images.rb b/app/models/concerns/tiny_mce_images.rb index 275eefb42..3a2761f9e 100644 --- a/app/models/concerns/tiny_mce_images.rb +++ b/app/models/concerns/tiny_mce_images.rb @@ -29,7 +29,18 @@ module TinyMceImages )[0] next unless tm_asset_to_update - tm_asset_to_update.attributes['src'].value = tm_asset.convert_to_base64 + tm_asset = tm_asset.image.representation(resize_to_limit: Constants::LARGE_PIC_FORMAT).processed + + width_attr = tm_asset_to_update.attributes['width'] + height_attr = tm_asset_to_update.attributes['height'] + + if width_attr && height_attr && (width_attr.value.to_i >= Constants::LARGE_PIC_FORMAT[0] || + height_attr.value.to_i >= Constants::LARGE_PIC_FORMAT[1]) + width_attr.value = tm_asset.image.blob.metadata['width'].to_s + height_attr.value = tm_asset.image.blob.metadata['height'].to_s + end + + tm_asset_to_update.attributes['src'].value = convert_to_base64(tm_asset.image) description = html_description.css('body').inner_html.to_s end description @@ -221,5 +232,13 @@ module TinyMceImages end end end + + def convert_to_base64(image) + encoded_data = Base64.strict_encode64(image.download) + "data:#{image.blob.content_type};base64,#{encoded_data}" + rescue StandardError => e + Rails.logger.error e.message + "data:#{image.blob.content_type};base64," + end # rubocop:enable Metrics/BlockLength: end diff --git a/app/models/tiny_mce_asset.rb b/app/models/tiny_mce_asset.rb index 69435f53c..0c123bc51 100644 --- a/app/models/tiny_mce_asset.rb +++ b/app/models/tiny_mce_asset.rb @@ -203,14 +203,6 @@ class TinyMceAsset < ApplicationRecord image&.blob end - def convert_to_base64 - encoded_data = Base64.strict_encode64(image.download) - "data:#{image.blob.content_type};base64,#{encoded_data}" - rescue StandardError => e - Rails.logger.error e.message - "data:#{image.blob.content_type};base64," - end - def duplicate_file(to_asset) return unless image.attached? diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb index 7eebf696b..82be35fb9 100644 --- a/config/initializers/constants.rb +++ b/config/initializers/constants.rb @@ -435,7 +435,7 @@ class Constants ASSET_SYNC_URL = ENV['ASSET_SYNC_URL'].freeze # Grover timeout in ms - GROVER_TIMEOUT_MS = 300000 + GROVER_TIMEOUT_MS = 450000 # SciNote Edit supported versions MIN_SCINOTE_EDIT_VERSION = ENV['MIN_SCINOTE_EDIT_VERSION'].freeze