Skip to content

Commit

Permalink
Fixed a couple possible bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcparker committed Dec 10, 2009
1 parent 9850b94 commit c8e45e0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/mime/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ def multipart_boundary
end
end
def attachment?
headers['content-disposition'] =~ /^attachment(?=;|$)/
headers['content-disposition'] =~ /^attachment(?=;|$)/ || headers['content-disposition'] =~ /^form-data;.* filename=[\"\']?[^\"\']+[\"\']?/
end
def attachment_filename
alias :file? :attachment?
def part_filename
# Content-Disposition: attachment; filename="summary.txt"
if headers['content-disposition'] =~ /^attachment; filename=[\"\']?([^\"\']+)/
if headers['content-disposition'] =~ /; filename=[\"\']?([^\"\']+)/
$1
end
end
Expand All @@ -120,8 +121,8 @@ def find_parts(options)

def save_to_file(path=nil)
filename = path if path && !File.exists?(path) # If path doesn't exist, assume it's a filename
filename ||= path + '/' + attachment_filename if path && attachment? # If path does exist, and we're saving an attachment, use the attachment filename
filename ||= (attachment? ? attachment_filename : path) # If there is no path and we're saving an attachment, use the attachment filename; otherwise use path (whether it is present or not)
filename ||= path + '/' + part_filename if path && attachment? # If path does exist, and we're saving an attachment, use the attachment filename
filename ||= (attachment? ? part_filename : path) # If there is no path and we're saving an attachment, use the attachment filename; otherwise use path (whether it is present or not)
filename ||= '.' # No path supplied, and not saving an attachment. We'll just save it in the current directory.
if File.directory?(filename)
i = 0
Expand Down
9 changes: 9 additions & 0 deletions lib/mime/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ def attachments
find_parts(:content_disposition => 'attachment')
end

def text
part = find_part(:content_type => 'text/plain')
part.content if part
end
def html
part = find_part(:content_type => 'text/html')
part.content if part
end

def save_attachments_to(path=nil)
attachments.each {|a| a.save_to_file(path) }
end
Expand Down

0 comments on commit c8e45e0

Please sign in to comment.