@@ -13,63 +13,69 @@ def __init__(self, config, request):
1313 self ._payload = request .form
1414 self ._raw_payload = request .data
1515
16- """Return a dictionary of key/values in the payload received from
17- the webhook"""
1816 def key_values (self ):
17+ """Return a dictionary of key/values in the payload received from
18+ the webhook"""
1919 key_values = {}
2020 for key in self .keys :
2121 if key in self .payload :
2222 key_values [key ] = self .payload [key ]
2323 return key_values
2424
25- """This only applies to raw payloads:
26- https://sendgrid.com/docs/Classroom/Basics/Inbound_Parse_Webhook/setting_up_the_inbound_parse_webhook.html#-Raw-Parameters"""
2725 def get_raw_email (self ):
28- if 'email' in self .payload :
26+ """This only applies to raw payloads:
27+ https://sendgrid.com/docs/Classroom/Basics/Inbound_Parse_Webhook/setting_up_the_inbound_parse_webhook.html#-Raw-Parameters"""
28+ if 'email' in self .payload :
2929 raw_email = email .message_from_string (self .payload ['email' ])
3030 return raw_email
31- else :
31+ else :
3232 return None
3333
34- """Returns an object with:
35- type = file content type
36- file_name = the name of the file
37- contents = base64 encoded file contents"""
3834 def attachments (self ):
39- attachments = []
35+ """Returns an object with:
36+ type = file content type
37+ file_name = the name of the file
38+ contents = base64 encoded file contents"""
39+ attachments = None
4040 if 'attachment-info' in self .payload :
41- for _ , filestorage in self .request .files .iteritems ():
42- attachment = {}
43- if filestorage .filename not in (None , 'fdopen' , '<fdopen>' ):
44- filename = secure_filename (filestorage .filename )
45- attachment ['type' ] = filestorage .content_type
46- attachment ['file_name' ] = filename
47- attachment ['contents' ] = base64 .b64encode (filestorage .getvalue ())
48- attachments .append (attachment )
49- return attachments
50-
41+ attachments = self ._get_attachments (self .request )
5142 # Check if we have a raw message
52- attachments = []
5343 raw_email = self .get_raw_email ()
5444 if raw_email is not None :
55- counter = 1
56- for part in raw_email .walk ():
57- attachment = {}
58- if part .get_content_maintype () == 'multipart' :
59- continue
60- filename = part .get_filename ()
61- if not filename :
62- ext = mimetypes .guess_extension (part .get_content_type ())
63- if not ext :
64- ext = '.bin'
65- filename = 'part-%03d%s' % (counter , ext )
66- counter += 1
67- attachment ['type' ] = part .get_content_type ()
68- attachment ['filename' ] = filename
69- attachment ['contents' ] = part .get_payload (decode = False )
45+ attachments = self ._get_attachments_raw (raw_email )
46+ return attachments
47+
48+ def _get_attachments (self , request ):
49+ attachments = []
50+ for _ , filestorage in request .files .iteritems ():
51+ attachment = {}
52+ if filestorage .filename not in (None , 'fdopen' , '<fdopen>' ):
53+ filename = secure_filename (filestorage .filename )
54+ attachment ['type' ] = filestorage .content_type
55+ attachment ['file_name' ] = filename
56+ attachment ['contents' ] = base64 .b64encode (filestorage .getvalue ())
7057 attachments .append (attachment )
71- return attachments
72- return None
58+ return attachments
59+
60+ def _get_attachments_raw (self , raw_email ):
61+ attachments = []
62+ counter = 1
63+ for part in raw_email .walk ():
64+ attachment = {}
65+ if part .get_content_maintype () == 'multipart' :
66+ continue
67+ filename = part .get_filename ()
68+ if not filename :
69+ ext = mimetypes .guess_extension (part .get_content_type ())
70+ if not ext :
71+ ext = '.bin'
72+ filename = 'part-%03d%s' % (counter , ext )
73+ counter += 1
74+ attachment ['type' ] = part .get_content_type ()
75+ attachment ['filename' ] = filename
76+ attachment ['contents' ] = part .get_payload (decode = False )
77+ attachments .append (attachment )
78+ return attachments
7379
7480 @property
7581 def keys (self ):
0 commit comments