2019-03-11 20:43:50 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module TinyMceImages
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
has_many :tiny_mce_assets,
|
|
|
|
as: :object,
|
|
|
|
class_name: :TinyMceAsset,
|
|
|
|
dependent: :destroy
|
|
|
|
|
2019-04-23 21:16:40 +08:00
|
|
|
before_save :clean_tiny_mce_image_urls
|
|
|
|
|
2019-03-11 20:43:50 +08:00
|
|
|
def prepare_for_report(field)
|
|
|
|
description = self[field]
|
2019-05-08 20:29:25 +08:00
|
|
|
|
|
|
|
# Check tinymce for old format
|
2019-05-14 21:01:57 +08:00
|
|
|
description = TinyMceAsset.update_old_tinymce(description, self)
|
2019-05-08 20:29:25 +08:00
|
|
|
|
2019-03-20 21:40:18 +08:00
|
|
|
tiny_mce_assets.each do |tm_asset|
|
|
|
|
tmp_f = Tempfile.open(tm_asset.image_file_name, Rails.root.join('tmp'))
|
2019-03-11 20:43:50 +08:00
|
|
|
begin
|
2019-03-20 21:40:18 +08:00
|
|
|
tm_asset.image.copy_to_local_file(:large, tmp_f.path)
|
|
|
|
encoded_tm_asset = Base64.strict_encode64(tmp_f.read)
|
2019-04-19 20:44:40 +08:00
|
|
|
new_tm_asset_src = "data:image/jpg;base64,#{encoded_tm_asset}"
|
2019-03-11 20:43:50 +08:00
|
|
|
html_description = Nokogiri::HTML(description)
|
2019-03-20 22:20:31 +08:00
|
|
|
tm_asset_to_update = html_description.css(
|
2019-04-02 19:41:32 +08:00
|
|
|
"img[data-mce-token=\"#{Base62.encode(tm_asset.id)}\"]"
|
2019-03-20 22:20:31 +08:00
|
|
|
)[0]
|
2019-05-13 16:55:08 +08:00
|
|
|
next unless tm_asset_to_update
|
|
|
|
|
2019-04-19 20:44:40 +08:00
|
|
|
tm_asset_to_update.attributes['src'].value = new_tm_asset_src
|
2019-03-11 20:43:50 +08:00
|
|
|
description = html_description.css('body').inner_html.to_s
|
|
|
|
ensure
|
|
|
|
tmp_f.close
|
|
|
|
tmp_f.unlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
description
|
|
|
|
end
|
2019-03-22 17:52:26 +08:00
|
|
|
|
|
|
|
def tinymce_render(field)
|
2019-05-14 21:01:57 +08:00
|
|
|
TinyMceAsset.generate_url(self[field], self)
|
2019-03-22 17:52:26 +08:00
|
|
|
end
|
2019-04-23 21:16:40 +08:00
|
|
|
|
|
|
|
# Takes array of old/new TinyMCE asset ID pairs
|
|
|
|
# and updates references in assosiated object's description
|
|
|
|
def reassign_tiny_mce_image_references(images = [])
|
|
|
|
object_field = Extends::RICH_TEXT_FIELD_MAPPINGS[self.class.name]
|
2019-05-08 20:29:25 +08:00
|
|
|
description = read_attribute(object_field)
|
2019-05-08 20:35:28 +08:00
|
|
|
|
2019-05-08 20:29:25 +08:00
|
|
|
# Check tinymce for old format
|
2019-05-14 21:01:57 +08:00
|
|
|
description = TinyMceAsset.update_old_tinymce(description, self)
|
2019-05-08 20:29:25 +08:00
|
|
|
|
|
|
|
parsed_description = Nokogiri::HTML(description)
|
2019-04-23 21:16:40 +08:00
|
|
|
images.each do |image|
|
|
|
|
old_id = image[0]
|
|
|
|
new_id = image[1]
|
|
|
|
image = parsed_description.at_css("img[data-mce-token=\"#{Base62.encode(old_id)}\"]")
|
2019-05-22 22:13:40 +08:00
|
|
|
|
|
|
|
unless image
|
|
|
|
Rails.logger.error "TinyMCE Asset with id #{old_id} not in text"
|
|
|
|
next
|
|
|
|
end
|
2019-05-27 22:27:27 +08:00
|
|
|
|
2019-04-23 21:16:40 +08:00
|
|
|
image['data-mce-token'] = Base62.encode(new_id)
|
|
|
|
end
|
2019-05-15 20:13:13 +08:00
|
|
|
update(object_field => parsed_description.css('body').inner_html.to_s)
|
2019-04-23 21:16:40 +08:00
|
|
|
end
|
|
|
|
|
2019-04-24 19:34:56 +08:00
|
|
|
def clone_tinymce_assets(target, team)
|
|
|
|
cloned_img_ids = []
|
|
|
|
tiny_mce_assets.each do |tiny_img|
|
|
|
|
tiny_img_clone = TinyMceAsset.new(
|
|
|
|
image: tiny_img.image,
|
|
|
|
estimated_size: tiny_img.estimated_size,
|
|
|
|
object: target,
|
|
|
|
team: team
|
|
|
|
)
|
|
|
|
tiny_img_clone.save!
|
|
|
|
|
|
|
|
target.tiny_mce_assets << tiny_img_clone
|
|
|
|
cloned_img_ids << [tiny_img.id, tiny_img_clone.id]
|
|
|
|
end
|
|
|
|
target.reassign_tiny_mce_image_references(cloned_img_ids)
|
|
|
|
end
|
|
|
|
|
2019-05-20 18:44:16 +08:00
|
|
|
def copy_unknown_tiny_mce_images
|
|
|
|
asset_team_id = Team.find_by_object(self)
|
|
|
|
return unless asset_team_id
|
|
|
|
|
|
|
|
object_field = Extends::RICH_TEXT_FIELD_MAPPINGS[self.class.name]
|
|
|
|
|
|
|
|
image_changed = false
|
|
|
|
parsed_description = Nokogiri::HTML(read_attribute(object_field))
|
|
|
|
parsed_description.css('img').each do |image|
|
|
|
|
if image['data-mce-token']
|
|
|
|
asset = TinyMceAsset.find_by_id(Base62.decode(image['data-mce-token']))
|
|
|
|
|
2019-07-04 21:59:11 +08:00
|
|
|
next if asset && (asset.object == self || asset_team_id != asset.team_id)
|
2019-05-20 18:44:16 +08:00
|
|
|
|
|
|
|
new_image = asset.image
|
|
|
|
else
|
|
|
|
new_image = URI.parse(image['src'])
|
|
|
|
end
|
|
|
|
|
|
|
|
new_asset = TinyMceAsset.create(
|
|
|
|
image: new_image,
|
|
|
|
object: self,
|
|
|
|
team_id: asset_team_id
|
|
|
|
)
|
|
|
|
|
|
|
|
image['src'] = ''
|
|
|
|
image['class'] = 'img-responsive'
|
|
|
|
image['data-mce-token'] = Base62.encode(new_asset.id)
|
|
|
|
image_changed = true
|
|
|
|
end
|
|
|
|
update(object_field => parsed_description.css('body').inner_html.to_s) if image_changed
|
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error "Object: #{self.class.name}, id: #{id}, error: #{e.message}"
|
|
|
|
end
|
|
|
|
|
2019-04-23 21:16:40 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def clean_tiny_mce_image_urls
|
|
|
|
object_field = Extends::RICH_TEXT_FIELD_MAPPINGS[self.class.name]
|
|
|
|
return unless changed.include?(object_field.to_s)
|
2019-04-24 19:34:56 +08:00
|
|
|
|
2019-04-23 21:16:40 +08:00
|
|
|
image_changed = false
|
|
|
|
parsed_description = Nokogiri::HTML(read_attribute(object_field))
|
|
|
|
parsed_description.css('img[data-mce-token]').each do |image|
|
|
|
|
image['src'] = ''
|
|
|
|
image['class'] = 'img-responsive'
|
|
|
|
image_changed = true
|
|
|
|
end
|
|
|
|
self[object_field] = parsed_description.to_html if image_changed
|
|
|
|
end
|
2019-03-11 20:43:50 +08:00
|
|
|
end
|
|
|
|
end
|