summaryrefslogtreecommitdiffstats
path: root/lib/vendor/tmail-1.2.7/tmail/attachments.rb
blob: 19ce1aa8a20512c4b6cc847849ad04b7358c03fa (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
=begin rdoc

= Attachment handling file

=end

require 'kconv'
require 'stringio'

module TMail
  class Attachment < StringIO
    attr_accessor :original_filename, :content_type
    alias quoted_filename original_filename
  end

  class Mail
    def has_attachments?
      attachment?(self) || multipart? && parts.any? { |part| attachment?(part) }
    end

    # Returns true if this part's content main type is text, else returns false.
    # By main type is meant "text/plain" is text.  "text/html" is text
    def text_content_type?
      self.header['content-type'] && (self.header['content-type'].main_type == 'text')
    end
  
    def inline_attachment?(part)
      part['content-id'] || (part['content-disposition'] && part['content-disposition'].disposition == 'inline' && !part.text_content_type?)
    end
  
    def attachment?(part)
      part.disposition_is_attachment? || (!part.content_type.nil? && !part.text_content_type?) unless part.multipart?
    end
  
    def attachments
      if multipart?
        parts.collect { |part| attachment(part) }.flatten.compact
      elsif attachment?(self)
        [attachment(self)]
      end
    end
  
    private
  
    def attachment(part)
      if part.multipart?
        part.attachments
      elsif attachment?(part)
        content   = part.body # unquoted automatically by TMail#body
        file_name = (part['content-location'] && part['content-location'].body) ||
                    part.sub_header('content-type', 'name') ||
                    part.sub_header('content-disposition', 'filename') ||
                    'noname'

        return if content.blank?

        attachment = TMail::Attachment.new(content)
        attachment.original_filename = file_name.strip unless file_name.blank?
        attachment.content_type = part.content_type
        attachment
      end
    end

  end
end