Skip to content

Commit cf3a2be

Browse files
committed
improve performance of Message#find_attachment
1 parent 800022a commit cf3a2be

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

lib/mail/message.rb

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,23 +2122,32 @@ def init_with_string(string)
21222122

21232123
# Returns the filename of the attachment (if it exists) or returns nil
21242124
def find_attachment
2125-
content_type_name = header[:content_type].filename rescue nil
2126-
content_disp_name = header[:content_disposition].filename rescue nil
2127-
content_loc_name = header[:content_location].location rescue nil
2128-
case
2129-
when content_disposition && content_disp_name
2130-
filename = content_disp_name
2131-
when content_type && content_type_name
2132-
filename = content_type_name
2133-
when content_location && content_loc_name
2134-
filename = content_loc_name
2135-
else
2136-
filename = nil
2137-
end
2125+
filename = find_attachment_filename
21382126
filename = Mail::Encodings.decode_encode(filename, :decode) if filename rescue filename
21392127
filename
21402128
end
21412129

2130+
def find_attachment_filename
2131+
filename_from_field(header[:content_location]) ||
2132+
filename_from_field(header[:content_disposition]) ||
2133+
filename_from_field(header[:content_type])
2134+
end
2135+
2136+
def filename_from_field(field)
2137+
return if field.nil?
2138+
2139+
case field.field
2140+
when ContentLocationField
2141+
field.location
2142+
when ContentTypeField, ContentDispositionField
2143+
field.filename
2144+
when NilClass, UnstructuredField
2145+
nil
2146+
else
2147+
raise ArgumentError, "Don't know how to extract filename from #{field}"
2148+
end
2149+
end
2150+
21422151
def do_delivery
21432152
begin
21442153
if perform_deliveries

0 commit comments

Comments
 (0)