search_index.rb 2.3 KB

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