2019-03-11 20:43:50 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-23 21:19:08 +08:00
|
|
|
class TinyMceAsset < ApplicationRecord
|
2019-04-19 20:44:40 +08:00
|
|
|
extend ProtocolsExporter
|
2017-04-21 22:09:04 +08:00
|
|
|
attr_accessor :reference
|
2017-06-28 21:21:32 +08:00
|
|
|
before_create :set_reference, optional: true
|
2019-03-20 21:40:18 +08:00
|
|
|
after_create :update_estimated_size, :self_destruct
|
2017-04-25 19:44:31 +08:00
|
|
|
after_destroy :release_team_space
|
2017-04-21 22:09:04 +08:00
|
|
|
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :team, inverse_of: :tiny_mce_assets, optional: true
|
2018-12-13 18:18:09 +08:00
|
|
|
belongs_to :step, inverse_of: :tiny_mce_assets, touch: true, optional: true
|
2018-11-14 18:57:53 +08:00
|
|
|
belongs_to :result_text,
|
|
|
|
inverse_of: :tiny_mce_assets,
|
2018-12-13 18:18:09 +08:00
|
|
|
touch: true,
|
2018-11-14 18:57:53 +08:00
|
|
|
optional: true
|
2019-03-11 20:43:50 +08:00
|
|
|
|
2019-03-20 21:40:18 +08:00
|
|
|
belongs_to :object, polymorphic: true,
|
|
|
|
optional: true,
|
|
|
|
inverse_of: :tiny_mce_assets
|
2017-04-21 22:09:04 +08:00
|
|
|
has_attached_file :image,
|
2017-04-24 22:22:25 +08:00
|
|
|
styles: { large: [Constants::LARGE_PIC_FORMAT, :jpg] },
|
|
|
|
convert_options: { large: '-quality 100 -strip' }
|
2017-04-21 22:09:04 +08:00
|
|
|
|
|
|
|
validates_attachment_content_type :image,
|
|
|
|
content_type: %r{^image/#{ Regexp.union(
|
|
|
|
Constants::WHITELISTED_IMAGE_TYPES
|
|
|
|
) }}
|
2017-04-24 22:22:25 +08:00
|
|
|
validates_attachment :image,
|
|
|
|
presence: true,
|
|
|
|
size: {
|
2019-03-20 21:40:18 +08:00
|
|
|
less_than: Rails.configuration.x\
|
|
|
|
.file_max_size_mb.megabytes
|
2017-04-24 22:22:25 +08:00
|
|
|
}
|
2017-04-24 18:28:27 +08:00
|
|
|
validates :estimated_size, presence: true
|
|
|
|
|
2019-03-11 20:43:50 +08:00
|
|
|
def self.update_images(object, images)
|
|
|
|
images = JSON.parse(images)
|
|
|
|
current_images = object.tiny_mce_assets.pluck(:id)
|
2019-03-20 21:40:18 +08:00
|
|
|
images_to_delete = current_images.reject do |x|
|
|
|
|
(images.include? Base62.encode(x))
|
|
|
|
end
|
2019-03-11 20:43:50 +08:00
|
|
|
images.each do |image|
|
|
|
|
image_to_update = find_by_id(Base62.decode(image))
|
2019-07-04 21:59:11 +08:00
|
|
|
next if image_to_update.object || image_to_update.team_id != Team.find_by_object(object)
|
|
|
|
|
|
|
|
image_to_update&.update(object: object, saved: true)
|
2019-03-11 20:43:50 +08:00
|
|
|
end
|
|
|
|
where(id: images_to_delete).destroy_all
|
2019-05-20 18:44:16 +08:00
|
|
|
|
|
|
|
object.delay(queue: :assets).copy_unknown_tiny_mce_images
|
2019-03-20 21:40:18 +08:00
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error e.message
|
2019-03-11 20:43:50 +08:00
|
|
|
end
|
|
|
|
|
2019-05-14 21:01:57 +08:00
|
|
|
def self.generate_url(description, obj = nil)
|
2019-05-08 20:23:26 +08:00
|
|
|
# Check tinymce for old format
|
2019-05-14 21:01:57 +08:00
|
|
|
description = update_old_tinymce(description, obj)
|
2019-05-08 20:23:26 +08:00
|
|
|
|
2019-03-22 17:52:26 +08:00
|
|
|
description = Nokogiri::HTML(description)
|
2019-05-16 19:33:04 +08:00
|
|
|
tm_assets = description.css('img[data-mce-token]')
|
2019-03-22 17:52:26 +08:00
|
|
|
tm_assets.each do |tm_asset|
|
2019-04-02 19:41:32 +08:00
|
|
|
asset_id = tm_asset.attr('data-mce-token')
|
2019-07-04 21:59:11 +08:00
|
|
|
new_asset_url = obj.tiny_mce_assets.find_by_id(Base62.decode(asset_id))
|
2019-03-22 17:52:26 +08:00
|
|
|
if new_asset_url
|
|
|
|
tm_asset.attributes['src'].value = new_asset_url.url
|
|
|
|
tm_asset['class'] = 'img-responsive'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
description.css('body').inner_html.to_s
|
|
|
|
end
|
|
|
|
|
2017-04-24 22:22:25 +08:00
|
|
|
def presigned_url(style = :large,
|
2017-04-21 22:09:04 +08:00
|
|
|
download: false,
|
2017-05-09 19:59:59 +08:00
|
|
|
timeout: Constants::URL_LONG_EXPIRE_TIME)
|
2019-07-01 16:14:16 +08:00
|
|
|
if is_stored_on_s3?
|
2019-04-24 19:34:56 +08:00
|
|
|
download_arg = ('attachment; filename=' + CGI.escape(image_file_name) if download)
|
2017-04-21 22:09:04 +08:00
|
|
|
|
|
|
|
signer = Aws::S3::Presigner.new(client: S3_BUCKET.client)
|
|
|
|
signer.presigned_url(:get_object,
|
|
|
|
bucket: S3_BUCKET.name,
|
|
|
|
key: image.path(style)[1..-1],
|
|
|
|
expires_in: timeout,
|
|
|
|
response_content_disposition: download_arg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-01 16:14:16 +08:00
|
|
|
def is_stored_on_s3?
|
2017-04-21 22:09:04 +08:00
|
|
|
image.options[:storage].to_sym == :s3
|
|
|
|
end
|
|
|
|
|
2017-05-09 19:59:59 +08:00
|
|
|
def url(style = :large, timeout: Constants::URL_LONG_EXPIRE_TIME)
|
2017-04-21 22:09:04 +08:00
|
|
|
if image.is_stored_on_s3?
|
|
|
|
presigned_url(style, timeout: timeout)
|
|
|
|
else
|
|
|
|
image.url(style)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-21 23:30:48 +08:00
|
|
|
def open
|
|
|
|
if image.is_stored_on_s3?
|
|
|
|
Kernel.open(presigned_url, 'rb')
|
|
|
|
else
|
2017-11-24 22:19:58 +08:00
|
|
|
File.open(image.path, 'rb')
|
2017-11-21 23:30:48 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-20 21:40:18 +08:00
|
|
|
def self.delete_unsaved_image(id)
|
|
|
|
asset = find_by_id(id)
|
|
|
|
asset.destroy if asset && !asset.saved
|
|
|
|
end
|
|
|
|
|
2019-06-14 22:20:27 +08:00
|
|
|
def self.update_old_tinymce(description, obj = nil, import = false)
|
2019-05-08 21:22:06 +08:00
|
|
|
return description unless description
|
|
|
|
|
2019-04-19 20:44:40 +08:00
|
|
|
description.scan(/\[~tiny_mce_id:(\w+)\]/).flatten.each do |token|
|
|
|
|
old_format = /\[~tiny_mce_id:#{token}\]/
|
2019-05-08 20:23:26 +08:00
|
|
|
new_format = "<img src=\"\" class=\"img-responsive\" data-mce-token=\"#{Base62.encode(token.to_i)}\"/>"
|
2019-05-14 21:01:57 +08:00
|
|
|
|
|
|
|
asset = find_by_id(token)
|
2019-06-14 22:20:27 +08:00
|
|
|
# impor flag only for import from file cases, because we don't have image in DB
|
|
|
|
unless asset || import
|
2019-05-15 22:11:48 +08:00
|
|
|
# remove tag if asset deleted
|
|
|
|
description.sub!(old_format, '')
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2019-05-14 21:01:57 +08:00
|
|
|
# If object (step or result text) don't have direct assciation to tinyMCE image, we need copy it.
|
|
|
|
asset.clone_tinymce_asset(obj) if obj && obj != asset.object
|
|
|
|
|
2019-04-19 20:44:40 +08:00
|
|
|
description.sub!(old_format, new_format)
|
|
|
|
end
|
|
|
|
description
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.save_to_eln(ostream, dir)
|
|
|
|
if exists?
|
|
|
|
order(:id).each do |tiny_mce_asset|
|
|
|
|
asset_guid = get_guid(tiny_mce_asset.id)
|
|
|
|
asset_file_name =
|
|
|
|
"rte-#{asset_guid.to_s +
|
|
|
|
File.extname(tiny_mce_asset.image_file_name).to_s}"
|
|
|
|
ostream.put_next_entry("#{dir}/#{asset_file_name}")
|
|
|
|
input_file = tiny_mce_asset.open
|
|
|
|
ostream.print(input_file.read)
|
|
|
|
input_file.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ostream
|
|
|
|
end
|
|
|
|
|
2019-05-14 21:01:57 +08:00
|
|
|
def clone_tinymce_asset(obj)
|
2019-05-15 22:11:48 +08:00
|
|
|
begin
|
|
|
|
# It will trigger only for Step or ResultText
|
|
|
|
team_id = if obj.class.name == 'Step'
|
|
|
|
obj.protocol.team_id
|
|
|
|
else
|
|
|
|
obj.result.my_module.protocol.team_id
|
|
|
|
end
|
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
team_id = nil
|
|
|
|
end
|
2019-05-14 21:01:57 +08:00
|
|
|
|
|
|
|
return false unless team_id
|
|
|
|
|
|
|
|
tiny_img_clone = TinyMceAsset.new(
|
|
|
|
image: image,
|
|
|
|
estimated_size: estimated_size,
|
|
|
|
object: obj,
|
|
|
|
team_id: team_id
|
|
|
|
)
|
|
|
|
tiny_img_clone.save!
|
|
|
|
|
|
|
|
obj.tiny_mce_assets << tiny_img_clone
|
|
|
|
# Prepare array of image to update
|
|
|
|
cloned_img_ids = [[id, tiny_img_clone.id]]
|
|
|
|
|
|
|
|
obj_field = Extends::RICH_TEXT_FIELD_MAPPINGS[obj.class.name]
|
|
|
|
|
|
|
|
# Update description with new format
|
|
|
|
obj.update(obj_field => TinyMceAsset.update_old_tinymce(obj[obj_field]))
|
|
|
|
|
|
|
|
# reassign images
|
|
|
|
obj.reassign_tiny_mce_image_references(cloned_img_ids)
|
|
|
|
end
|
|
|
|
|
2017-04-24 18:28:27 +08:00
|
|
|
private
|
|
|
|
|
2019-03-20 21:40:18 +08:00
|
|
|
def self_destruct
|
2019-03-20 22:20:31 +08:00
|
|
|
TinyMceAsset.delay(queue: :assets, run_at: 1.days.from_now).delete_unsaved_image(id)
|
2019-03-11 20:43:50 +08:00
|
|
|
end
|
|
|
|
|
2017-04-24 18:28:27 +08:00
|
|
|
def update_estimated_size
|
|
|
|
return if image_file_size.blank?
|
2019-03-11 20:43:50 +08:00
|
|
|
|
2017-04-24 18:28:27 +08:00
|
|
|
es = image_file_size * Constants::ASSET_ESTIMATED_SIZE_FACTOR
|
|
|
|
update(estimated_size: es)
|
|
|
|
Rails.logger.info "Asset #{id}: Estimated size successfully calculated"
|
2017-04-25 19:44:31 +08:00
|
|
|
# update team space taken
|
2019-03-11 20:43:50 +08:00
|
|
|
team.take_space(es)
|
|
|
|
team.save
|
2017-04-25 19:44:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def release_team_space
|
2019-03-11 20:43:50 +08:00
|
|
|
team.release_space(estimated_size)
|
|
|
|
team.save
|
2017-04-24 18:28:27 +08:00
|
|
|
end
|
|
|
|
|
2017-04-21 22:09:04 +08:00
|
|
|
def set_reference
|
|
|
|
obj_type = "#{@reference.class.to_s.underscore}=".to_sym
|
2017-04-24 22:22:25 +08:00
|
|
|
public_send(obj_type, @reference) if @reference
|
2017-04-21 22:09:04 +08:00
|
|
|
end
|
|
|
|
end
|