2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
module Ticket::SearchIndex
|
2018-04-26 08:55:53 +00:00
|
|
|
extend ActiveSupport::Concern
|
2014-01-27 22:59:41 +00:00
|
|
|
|
2021-01-27 09:58:35 +00:00
|
|
|
def search_index_attribute_lookup(include_references: true)
|
2016-07-06 06:13:44 +00:00
|
|
|
attributes = super
|
|
|
|
return if !attributes
|
2015-04-27 23:19:26 +00:00
|
|
|
|
2016-07-06 06:13:44 +00:00
|
|
|
# collect article data
|
2015-04-27 23:19:26 +00:00
|
|
|
# add tags
|
2021-03-16 08:59:32 +00:00
|
|
|
attributes['tags'] = tag_list
|
|
|
|
|
|
|
|
# mentions
|
|
|
|
attributes['mention_user_ids'] = mentions.pluck(:user_id)
|
2015-04-05 23:28:52 +00:00
|
|
|
|
2021-03-01 08:50:07 +00:00
|
|
|
# current payload size
|
|
|
|
total_size_current = 0
|
2015-04-05 23:28:52 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# collect article data
|
2016-09-09 21:10:27 +00:00
|
|
|
attributes['article'] = []
|
2021-03-24 09:32:21 +00:00
|
|
|
Ticket::Article.where(ticket_id: id).limit(1000).find_each(batch_size: 50).each do |article|
|
2017-10-15 22:16:37 +00:00
|
|
|
|
|
|
|
# lookup attributes of ref. objects (normally name and note)
|
2021-01-27 09:58:35 +00:00
|
|
|
article_attributes = article.search_index_attribute_lookup(include_references: false)
|
2015-04-05 23:28:52 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# remove note needed attributes
|
2017-12-04 00:24:58 +00:00
|
|
|
ignore = %w[message_id_md5 ticket]
|
2017-10-01 12:25:52 +00:00
|
|
|
ignore.each do |attribute|
|
2016-07-06 06:13:44 +00:00
|
|
|
article_attributes.delete(attribute)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2014-02-02 18:58:59 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# index raw text body
|
|
|
|
if article_attributes['content_type'] && article_attributes['content_type'] == 'text/html' && article_attributes['body']
|
|
|
|
article_attributes['body'] = article_attributes['body'].html2text
|
|
|
|
end
|
2015-04-27 21:27:51 +00:00
|
|
|
|
2021-03-01 08:50:07 +00:00
|
|
|
article_attributes_payload_size = article_attributes.to_json.bytes.size
|
|
|
|
|
|
|
|
next if search_index_attribute_lookup_oversized?(total_size_current + article_attributes_payload_size)
|
|
|
|
|
|
|
|
# add body size to totel payload size
|
|
|
|
total_size_current += article_attributes_payload_size
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# lookup attachments
|
2017-12-04 00:24:58 +00:00
|
|
|
article_attributes['attachment'] = []
|
2015-04-05 23:28:52 +00:00
|
|
|
|
2021-03-01 08:50:07 +00:00
|
|
|
article.attachments.each do |attachment|
|
|
|
|
|
|
|
|
next if search_index_attribute_lookup_file_ignored?(attachment)
|
2015-05-07 09:04:40 +00:00
|
|
|
|
2021-03-01 08:50:07 +00:00
|
|
|
next if search_index_attribute_lookup_file_oversized?(attachment, total_size_current)
|
2015-05-07 09:04:40 +00:00
|
|
|
|
2021-03-01 08:50:07 +00:00
|
|
|
next if search_index_attribute_lookup_oversized?(total_size_current + attachment.content.bytes.size)
|
2015-05-07 09:04:40 +00:00
|
|
|
|
2021-03-01 08:50:07 +00:00
|
|
|
# add attachment size to totel payload size
|
|
|
|
total_size_current += attachment.content.bytes.size
|
2015-05-07 09:04:40 +00:00
|
|
|
|
2021-03-01 08:50:07 +00:00
|
|
|
data = {
|
|
|
|
'size' => attachment.size,
|
|
|
|
'_name' => attachment.filename,
|
|
|
|
'_content' => Base64.encode64(attachment.content).delete("\n"),
|
|
|
|
}
|
2018-11-15 09:52:42 +00:00
|
|
|
|
2021-03-01 08:50:07 +00:00
|
|
|
article_attributes['attachment'].push data
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2021-03-01 08:50:07 +00:00
|
|
|
|
2016-09-09 21:10:27 +00:00
|
|
|
attributes['article'].push article_attributes
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2014-01-27 22:59:41 +00:00
|
|
|
|
2016-07-06 06:13:44 +00:00
|
|
|
attributes
|
2014-01-27 22:59:41 +00:00
|
|
|
end
|
2016-07-06 06:13:44 +00:00
|
|
|
|
2021-03-01 08:50:07 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def search_index_attribute_lookup_oversized?(total_size_current)
|
|
|
|
|
|
|
|
# if complete payload is to high
|
|
|
|
total_max_size_in_kb = (Setting.get('es_total_max_size_in_mb') || 300).megabyte
|
|
|
|
return true if total_size_current >= total_max_size_in_kb
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_index_attribute_lookup_file_oversized?(attachment, total_size_current)
|
|
|
|
return true if attachment.content.blank?
|
|
|
|
|
|
|
|
# if attachment size is bigger as configured
|
|
|
|
attachment_max_size = (Setting.get('es_attachment_max_size_in_mb') || 10).megabyte
|
|
|
|
return true if attachment.content.bytes.size > attachment_max_size
|
|
|
|
|
|
|
|
# if complete size is bigger as configured
|
|
|
|
return true if search_index_attribute_lookup_oversized?(total_size_current + attachment.content.bytes.size)
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_index_attribute_lookup_file_ignored?(attachment)
|
|
|
|
return true if attachment.filename.blank?
|
|
|
|
|
|
|
|
filename_extention = attachment.filename.downcase
|
2021-05-12 11:37:44 +00:00
|
|
|
filename_extention.gsub!(%r{^.*(\..+?)$}, '\\1')
|
2021-03-01 08:50:07 +00:00
|
|
|
|
|
|
|
# list ignored file extensions
|
|
|
|
attachments_ignore = Setting.get('es_attachment_ignore') || [ '.png', '.jpg', '.jpeg', '.mpeg', '.mpg', '.mov', '.bin', '.exe' ]
|
|
|
|
|
|
|
|
return true if attachments_ignore.include?(filename_extention.downcase)
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|