mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-27 17:30:32 +08:00
Fix report image issues [SCI-3772] (#1983)
* Fix report image issues * Small fixes
This commit is contained in:
parent
bc86d5bc40
commit
a3b2c7d5fe
7 changed files with 41 additions and 26 deletions
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module ReportsHelper
|
||||
include StringUtility
|
||||
|
||||
|
@ -85,21 +87,19 @@ module ReportsHelper
|
|||
# ReportExtends is located in config/initializers/extends/report_extends.rb
|
||||
ReportElement.type_ofs.keys.each do |type|
|
||||
next unless element.public_send("#{type}?")
|
||||
|
||||
element.element_references.each do |el_ref|
|
||||
locals[el_ref.class.name.underscore.to_sym] = el_ref
|
||||
end
|
||||
if type.in? ReportExtends::SORTED_ELEMENTS
|
||||
locals[:order] = element.sort_order
|
||||
end
|
||||
locals[:order] = element.sort_order if type.in? ReportExtends::SORTED_ELEMENTS
|
||||
end
|
||||
|
||||
(render partial: view, locals: locals).html_safe
|
||||
end
|
||||
|
||||
# "Hack" to omit file preview URL because of WKHTML issues
|
||||
def report_image_asset_url(asset, type = :asset, klass = nil)
|
||||
url = type == :tiny_mce_asset ? asset.preview : asset.large_preview
|
||||
image_tag(url, class: klass)
|
||||
def report_image_asset_url(asset, _type = :asset, klass = nil)
|
||||
image_tag(asset.generate_base64(:medium), class: klass)
|
||||
end
|
||||
|
||||
# "Hack" to load Glyphicons css directly from the CDN
|
||||
|
@ -107,6 +107,7 @@ module ReportsHelper
|
|||
def bootstrap_cdn_link_tag
|
||||
specs = Gem.loaded_specs['bootstrap-sass']
|
||||
return '' unless specs.present?
|
||||
|
||||
stylesheet_link_tag("http://netdna.bootstrapcdn.com/bootstrap/" \
|
||||
"#{specs.version.version}/css/bootstrap.min.css",
|
||||
media: 'all')
|
||||
|
|
|
@ -7,6 +7,7 @@ class Asset < ApplicationRecord
|
|||
include Encryptor
|
||||
include WopiUtil
|
||||
include ActiveStorageFileUtil
|
||||
include ImageVariantProcessing
|
||||
|
||||
require 'tempfile'
|
||||
# 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
|
||||
end
|
||||
|
||||
def generate_base64(style)
|
||||
return convert_variant_to_base64(medium_preview) if style == :medium
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def tempdir
|
||||
|
|
15
app/models/concerns/image_variant_processing.rb
Normal file
15
app/models/concerns/image_variant_processing.rb
Normal 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
|
|
@ -9,6 +9,7 @@ class User < ApplicationRecord
|
|||
include TeamBySubjectModel
|
||||
include InputSanitizeHelper
|
||||
include ActiveStorage::Downloading
|
||||
include ImageVariantProcessing
|
||||
|
||||
acts_as_token_authenticatable
|
||||
devise :invitable, :confirmable, :database_authenticatable, :registerable,
|
||||
|
@ -573,19 +574,12 @@ class User < ApplicationRecord
|
|||
end
|
||||
|
||||
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
|
||||
return "data:image/png;base64,#{Base64.strict_encode64(missing_link)}"
|
||||
end
|
||||
|
||||
avatar_uri = if avatar.options[:storage].to_sym == :s3
|
||||
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}"
|
||||
convert_variant_to_base64(avatar_variant(style))
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
|
@ -6,7 +6,6 @@ module DrawResultAsset
|
|||
return unless result
|
||||
|
||||
asset = result.asset
|
||||
is_image = result.asset.is_image?
|
||||
timestamp = asset.created_at
|
||||
color = @color
|
||||
@docx.p
|
||||
|
@ -15,10 +14,10 @@ module DrawResultAsset
|
|||
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.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
|
||||
|
||||
asset_image_preparing(asset) if is_image
|
||||
asset_image_preparing(asset) if asset.image?
|
||||
|
||||
subject['children'].each do |child|
|
||||
public_send("draw_#{child['type_of']}", child)
|
||||
|
|
|
@ -5,7 +5,6 @@ module DrawStepAsset
|
|||
asset = Asset.find_by_id(subject['id']['asset_id'])
|
||||
return unless asset
|
||||
|
||||
is_image = asset.is_image?
|
||||
timestamp = asset.created_at
|
||||
color = @color
|
||||
@docx.p
|
||||
|
@ -16,6 +15,6 @@ module DrawStepAsset
|
|||
timestamp: I18n.l(timestamp, format: :full)), color: color[:gray]
|
||||
end
|
||||
|
||||
asset_image_preparing(asset) if is_image
|
||||
asset_image_preparing(asset) if asset.image?
|
||||
end
|
||||
end
|
||||
|
|
|
@ -284,12 +284,14 @@ module PrivateMethods
|
|||
}
|
||||
end
|
||||
|
||||
def image_path(image)
|
||||
if image.is_stored_on_s3?
|
||||
image.url
|
||||
else
|
||||
image.open.path
|
||||
end
|
||||
def image_path(asset)
|
||||
image = if asset.class == Asset
|
||||
asset.file
|
||||
else
|
||||
asset.image
|
||||
end
|
||||
|
||||
image.service_url
|
||||
end
|
||||
|
||||
def calculate_color_hsp(color)
|
||||
|
|
Loading…
Reference in a new issue