debian-mirror-gitlab/lib/gitlab/email/attachment_uploader.rb

54 lines
1.4 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2015-09-25 12:07:36 +05:30
module Gitlab
module Email
class AttachmentUploader
attr_accessor :message
def initialize(message)
@message = message
end
2020-03-13 15:44:24 +05:30
def execute(upload_parent:, uploader_class:)
2015-09-25 12:07:36 +05:30
attachments = []
2020-03-13 15:44:24 +05:30
filter_signature_attachments(message).each do |attachment|
2015-09-25 12:07:36 +05:30
tmp = Tempfile.new("gitlab-email-attachment")
begin
File.open(tmp.path, "w+b") { |f| f.write attachment.body.decoded }
file = {
tempfile: tmp,
filename: attachment.filename,
content_type: attachment.content_type
}
2020-03-13 15:44:24 +05:30
uploader = UploadService.new(upload_parent, file, uploader_class).execute
2019-02-15 15:39:39 +05:30
attachments << uploader.to_h if uploader
2015-09-25 12:07:36 +05:30
ensure
tmp.close!
end
end
attachments
end
2020-03-13 15:44:24 +05:30
private
# If this is a signed message (e.g. S/MIME or PGP), remove the signature
# from the uploaded attachments
def filter_signature_attachments(message)
attachments = message.attachments
if message.content_type&.starts_with?('multipart/signed')
signature_protocol = message.content_type_parameters[:protocol]
attachments.delete_if { |attachment| attachment.content_type.starts_with?(signature_protocol) } if signature_protocol.present?
end
attachments
end
2015-09-25 12:07:36 +05:30
end
end
end