search_index.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. module Ticket::SearchIndex
  3. extend ActiveSupport::Concern
  4. =begin
  5. lookup name of ref. objects
  6. ticket = Ticket.find(123)
  7. result = ticket.search_index_attribute_lookup
  8. returns
  9. attributes # object with lookup data
  10. =end
  11. def search_index_attribute_lookup
  12. attributes = super
  13. return if !attributes
  14. # collect article data
  15. # add tags
  16. tags = tag_list
  17. if tags.present?
  18. attributes[:tag] = tags
  19. end
  20. # list ignored file extentions
  21. attachments_ignore = Setting.get('es_attachment_ignore') || [ '.png', '.jpg', '.jpeg', '.mpeg', '.mpg', '.mov', '.bin', '.exe' ]
  22. # max attachment size
  23. attachment_max_size_in_mb = Setting.get('es_attachment_max_size_in_mb') || 40
  24. # collect article data
  25. articles = Ticket::Article.where(ticket_id: id)
  26. attributes['article'] = []
  27. articles.each do |article|
  28. # lookup attributes of ref. objects (normally name and note)
  29. article_attributes = article.search_index_attribute_lookup
  30. # remove note needed attributes
  31. ignore = %w[message_id_md5 ticket]
  32. ignore.each do |attribute|
  33. article_attributes.delete(attribute)
  34. end
  35. # index raw text body
  36. if article_attributes['content_type'] && article_attributes['content_type'] == 'text/html' && article_attributes['body']
  37. article_attributes['body'] = article_attributes['body'].html2text
  38. end
  39. # lookup attachments
  40. article_attributes['attachment'] = []
  41. article.attachments.each do |attachment|
  42. # check file size
  43. next if !attachment.content
  44. next if attachment.content.size / 1024 > attachment_max_size_in_mb * 1024
  45. # check ignored files
  46. next if !attachment.filename
  47. filename_extention = attachment.filename.downcase
  48. filename_extention.gsub!(/^.*(\..+?)$/, '\\1')
  49. next if attachments_ignore.include?(filename_extention.downcase)
  50. data = {
  51. '_name' => attachment.filename,
  52. '_content' => Base64.encode64(attachment.content).delete("\n")
  53. }
  54. article_attributes['attachment'].push data
  55. end
  56. attributes['article'].push article_attributes
  57. end
  58. attributes
  59. end
  60. end