attachment_factory.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module Import
  3. module OTRS
  4. class Article
  5. module AttachmentFactory
  6. extend Import::Helper
  7. extend self
  8. def import(args)
  9. attachments = args[:attachments] || []
  10. local_article = args[:local_article]
  11. return if skip_import?(attachments, local_article)
  12. perform_import(attachments, local_article)
  13. end
  14. private
  15. def perform_import(attachments, local_article)
  16. attachments.each { |attachment| import_single(local_article, attachment) }
  17. end
  18. def import_single(local_article, attachment)
  19. decoded_filename = Base64.decode64(attachment['Filename'])
  20. decoded_content = Base64.decode64(attachment['Content'])
  21. # rubocop:disable Style/ClassVars
  22. @@mutex ||= Mutex.new
  23. @@mutex.synchronize do
  24. # rubocop:enable Style/ClassVars
  25. Store.create!(
  26. object: 'Ticket::Article',
  27. o_id: local_article.id,
  28. filename: decoded_filename.force_encoding('utf-8'),
  29. data: decoded_content,
  30. preferences: {
  31. 'Mime-Type' => attachment['ContentType'],
  32. 'Content-ID' => attachment['ContentID'],
  33. 'content-alternative' => attachment['ContentAlternative'],
  34. },
  35. created_by_id: 1,
  36. )
  37. end
  38. end
  39. def skip_import?(attachments, local_article)
  40. local_attachments = local_article.attachments
  41. return true if local_attachments.count == attachments.count
  42. # get a common ground
  43. local_attachments.each(&:delete)
  44. return true if attachments.blank?
  45. false
  46. end
  47. end
  48. end
  49. end
  50. end