Fix report image issues [SCI-3772] (#1983)

* Fix report image issues

* Small fixes
This commit is contained in:
aignatov-bio 2019-09-12 09:02:45 +02:00 committed by GitHub
parent bc86d5bc40
commit a3b2c7d5fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 26 deletions

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ReportsHelper module ReportsHelper
include StringUtility include StringUtility
@ -85,21 +87,19 @@ module ReportsHelper
# ReportExtends is located in config/initializers/extends/report_extends.rb # ReportExtends is located in config/initializers/extends/report_extends.rb
ReportElement.type_ofs.keys.each do |type| ReportElement.type_ofs.keys.each do |type|
next unless element.public_send("#{type}?") next unless element.public_send("#{type}?")
element.element_references.each do |el_ref| element.element_references.each do |el_ref|
locals[el_ref.class.name.underscore.to_sym] = el_ref locals[el_ref.class.name.underscore.to_sym] = el_ref
end end
if type.in? ReportExtends::SORTED_ELEMENTS locals[:order] = element.sort_order if type.in? ReportExtends::SORTED_ELEMENTS
locals[:order] = element.sort_order
end
end end
(render partial: view, locals: locals).html_safe (render partial: view, locals: locals).html_safe
end end
# "Hack" to omit file preview URL because of WKHTML issues # "Hack" to omit file preview URL because of WKHTML issues
def report_image_asset_url(asset, type = :asset, klass = nil) def report_image_asset_url(asset, _type = :asset, klass = nil)
url = type == :tiny_mce_asset ? asset.preview : asset.large_preview image_tag(asset.generate_base64(:medium), class: klass)
image_tag(url, class: klass)
end end
# "Hack" to load Glyphicons css directly from the CDN # "Hack" to load Glyphicons css directly from the CDN
@ -107,6 +107,7 @@ module ReportsHelper
def bootstrap_cdn_link_tag def bootstrap_cdn_link_tag
specs = Gem.loaded_specs['bootstrap-sass'] specs = Gem.loaded_specs['bootstrap-sass']
return '' unless specs.present? return '' unless specs.present?
stylesheet_link_tag("http://netdna.bootstrapcdn.com/bootstrap/" \ stylesheet_link_tag("http://netdna.bootstrapcdn.com/bootstrap/" \
"#{specs.version.version}/css/bootstrap.min.css", "#{specs.version.version}/css/bootstrap.min.css",
media: 'all') media: 'all')

View file

@ -7,6 +7,7 @@ class Asset < ApplicationRecord
include Encryptor include Encryptor
include WopiUtil include WopiUtil
include ActiveStorageFileUtil include ActiveStorageFileUtil
include ImageVariantProcessing
require 'tempfile' require 'tempfile'
# Lock duration set to 30 minutes # Lock duration set to 30 minutes
@ -446,6 +447,10 @@ class Asset < ApplicationRecord
!locked? && %r{^image/#{Regexp.union(Constants::WHITELISTED_IMAGE_TYPES_EDITABLE)}} =~ file.content_type !locked? && %r{^image/#{Regexp.union(Constants::WHITELISTED_IMAGE_TYPES_EDITABLE)}} =~ file.content_type
end end
def generate_base64(style)
return convert_variant_to_base64(medium_preview) if style == :medium
end
private private
def tempdir def tempdir

View file

@ -0,0 +1,15 @@
# frozen_string_literal: true
module ImageVariantProcessing
extend ActiveSupport::Concern
def convert_variant_to_base64(variant)
image_link = if ENV['ACTIVESTORAGE_SERVICE'] == 'amazon'
URI.parse(variant.processed.service_url).open.to_a.join
else
File.open(variant.processed.service_url).to_a.join
end
encoded_data = Base64.strict_encode64(image_link)
"data:#{variant.image.blob.content_type};base64,#{encoded_data}"
end
end

View file

@ -9,6 +9,7 @@ class User < ApplicationRecord
include TeamBySubjectModel include TeamBySubjectModel
include InputSanitizeHelper include InputSanitizeHelper
include ActiveStorage::Downloading include ActiveStorage::Downloading
include ImageVariantProcessing
acts_as_token_authenticatable acts_as_token_authenticatable
devise :invitable, :confirmable, :database_authenticatable, :registerable, devise :invitable, :confirmable, :database_authenticatable, :registerable,
@ -573,19 +574,12 @@ class User < ApplicationRecord
end end
def avatar_base64(style) def avatar_base64(style)
unless avatar.present? unless avatar.attached?
missing_link = File.open("#{Rails.root}/app/assets/images/#{style}/missing.png").to_a.join missing_link = File.open("#{Rails.root}/app/assets/images/#{style}/missing.png").to_a.join
return "data:image/png;base64,#{Base64.strict_encode64(missing_link)}" return "data:image/png;base64,#{Base64.strict_encode64(missing_link)}"
end end
avatar_uri = if avatar.options[:storage].to_sym == :s3 convert_variant_to_base64(avatar_variant(style))
URI.parse(avatar.url(style)).open.to_a.join
else
File.open(avatar.path(style)).to_a.join
end
encoded_data = Base64.strict_encode64(avatar_uri)
"data:#{avatar_content_type};base64,#{encoded_data}"
end end
protected protected

View file

@ -6,7 +6,6 @@ module DrawResultAsset
return unless result return unless result
asset = result.asset asset = result.asset
is_image = result.asset.is_image?
timestamp = asset.created_at timestamp = asset.created_at
color = @color color = @color
@docx.p @docx.p
@ -15,10 +14,10 @@ module DrawResultAsset
text ' ' + I18n.t('search.index.archived'), color: color[:gray] if result.archived? text ' ' + I18n.t('search.index.archived'), color: color[:gray] if result.archived?
text ' ' + I18n.t('projects.reports.elements.result_asset.file_name', file: asset.file_name) text ' ' + I18n.t('projects.reports.elements.result_asset.file_name', file: asset.file_name)
text ' ' + I18n.t('projects.reports.elements.result_asset.user_time', text ' ' + I18n.t('projects.reports.elements.result_asset.user_time',
user: result.user.full_name, timestamp: I18n.l(timestamp, format: :full)), color: color[:gray] user: result.user.full_name, timestamp: I18n.l(timestamp, format: :full)), color: color[:gray]
end end
asset_image_preparing(asset) if is_image asset_image_preparing(asset) if asset.image?
subject['children'].each do |child| subject['children'].each do |child|
public_send("draw_#{child['type_of']}", child) public_send("draw_#{child['type_of']}", child)

View file

@ -5,7 +5,6 @@ module DrawStepAsset
asset = Asset.find_by_id(subject['id']['asset_id']) asset = Asset.find_by_id(subject['id']['asset_id'])
return unless asset return unless asset
is_image = asset.is_image?
timestamp = asset.created_at timestamp = asset.created_at
color = @color color = @color
@docx.p @docx.p
@ -16,6 +15,6 @@ module DrawStepAsset
timestamp: I18n.l(timestamp, format: :full)), color: color[:gray] timestamp: I18n.l(timestamp, format: :full)), color: color[:gray]
end end
asset_image_preparing(asset) if is_image asset_image_preparing(asset) if asset.image?
end end
end end

View file

@ -284,12 +284,14 @@ module PrivateMethods
} }
end end
def image_path(image) def image_path(asset)
if image.is_stored_on_s3? image = if asset.class == Asset
image.url asset.file
else else
image.open.path asset.image
end end
image.service_url
end end
def calculate_color_hsp(color) def calculate_color_hsp(color)