2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
module Import
|
|
|
|
module OTRS
|
|
|
|
class Article
|
|
|
|
module AttachmentFactory
|
|
|
|
extend Import::Helper
|
|
|
|
extend self
|
|
|
|
|
|
|
|
def import(args)
|
|
|
|
attachments = args[:attachments] || []
|
|
|
|
local_article = args[:local_article]
|
|
|
|
|
|
|
|
return if skip_import?(attachments, local_article)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
perform_import(attachments, local_article)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def perform_import(attachments, local_article)
|
|
|
|
attachments.each { |attachment| import_single(local_article, attachment) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_single(local_article, attachment)
|
|
|
|
|
|
|
|
decoded_filename = Base64.decode64(attachment['Filename'])
|
|
|
|
decoded_content = Base64.decode64(attachment['Content'])
|
|
|
|
|
2021-09-20 10:47:05 +00:00
|
|
|
# rubocop:disable Style/ClassVars
|
|
|
|
@@mutex ||= Mutex.new
|
|
|
|
@@mutex.synchronize do
|
|
|
|
# rubocop:enable Style/ClassVars
|
|
|
|
|
|
|
|
Store.add(
|
|
|
|
object: 'Ticket::Article',
|
|
|
|
o_id: local_article.id,
|
|
|
|
filename: decoded_filename.force_encoding('utf-8'),
|
|
|
|
data: decoded_content,
|
|
|
|
preferences: {
|
|
|
|
'Mime-Type' => attachment['ContentType'],
|
|
|
|
'Content-ID' => attachment['ContentID'],
|
|
|
|
'content-alternative' => attachment['ContentAlternative'],
|
|
|
|
},
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
2016-11-25 16:10:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def skip_import?(attachments, local_article)
|
|
|
|
local_attachments = local_article.attachments
|
|
|
|
return true if local_attachments.count == attachments.count
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
# get a common ground
|
|
|
|
local_attachments.each(&:delete)
|
2017-11-23 08:09:44 +00:00
|
|
|
return true if attachments.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|