diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-04-25 17:17:49 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-04-25 17:17:49 +0000 |
commit | 5e57a1a9d9478162ac4f27ae96b2ccaf55a1aba7 (patch) | |
tree | 93e57765139714bd82dede475725516c448c0d55 /lib/vendor/tmail-1.2.7/tmail/attachments.rb | |
parent | 34e20c4373b7f5a20ab3a132feae3f70f21ec477 (diff) | |
download | redmine-5e57a1a9d9478162ac4f27ae96b2ccaf55a1aba7.tar.gz redmine-5e57a1a9d9478162ac4f27ae96b2ccaf55a1aba7.zip |
Merged rails-3.2 branch.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9528 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib/vendor/tmail-1.2.7/tmail/attachments.rb')
-rw-r--r-- | lib/vendor/tmail-1.2.7/tmail/attachments.rb | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/lib/vendor/tmail-1.2.7/tmail/attachments.rb b/lib/vendor/tmail-1.2.7/tmail/attachments.rb new file mode 100644 index 000000000..19ce1aa8a --- /dev/null +++ b/lib/vendor/tmail-1.2.7/tmail/attachments.rb @@ -0,0 +1,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 |