summaryrefslogtreecommitdiff
path: root/lib/gitlab/email/attachment_uploader.rb
blob: 83440ae227dad9f9b7fadd1a10debc1b8a53a9aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Gitlab
  module Email
    class AttachmentUploader
      attr_accessor :message

      def initialize(message)
        @message = message
      end

      def execute(project)
        attachments = []

        message.attachments.each do |attachment|
          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
            }

            link = UploadService.new(project, file).execute
            attachments << link if link
          ensure
            tmp.close!
          end
        end

        attachments
      end
    end
  end
end