Skip to content

Improve performance of Message#find_attachment #825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions lib/mail/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2118,23 +2118,32 @@ def init_with_string(string)

# Returns the filename of the attachment (if it exists) or returns nil
def find_attachment
content_type_name = header[:content_type].filename rescue nil
content_disp_name = header[:content_disposition].filename rescue nil
content_loc_name = header[:content_location].location rescue nil
case
when content_type && content_type_name
filename = content_type_name
when content_disposition && content_disp_name
filename = content_disp_name
when content_location && content_loc_name
filename = content_loc_name
else
filename = nil
end
filename = find_attachment_filename
filename = Mail::Encodings.decode_encode(filename, :decode) if filename rescue filename
filename
end

def find_attachment_filename
filename_from_field(header[:content_type]) ||
filename_from_field(header[:content_disposition]) ||
filename_from_field(header[:content_location])
end

def filename_from_field(field)
return if field.nil?

case field.field
when ContentLocationField
field.location
when ContentTypeField, ContentDispositionField
field.filename
when NilClass, UnstructuredField
nil
else
raise ArgumentError, "Don't know how to extract filename from #{field}"
end
end

def do_delivery
begin
if perform_deliveries
Expand Down