Commit 04b4152e authored by Nicolas Delaby's avatar Nicolas Delaby

Use email library to get filename attchments (get_filename). In case of failures, fallback

to old implementation by intropsecting message headers.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@34395 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent e412d318
...@@ -216,35 +216,42 @@ class EmailDocument(File, TextDocument): ...@@ -216,35 +216,42 @@ class EmailDocument(File, TextDocument):
**kw - support for listbox (TODO: improve it) **kw - support for listbox (TODO: improve it)
""" """
result = [] result = []
i = 0 for i, part in enumerate(self._getMessage().walk()):
for part in self._getMessage().walk():
if not part.is_multipart(): if not part.is_multipart():
kw = dict(part.items()) kw = dict(part.items())
kw['uid'] = 'part_%s' % i kw['uid'] = 'part_%s' % i
kw['index'] = i kw['index'] = i
if kw.has_key('Content-Disposition'): file_name = part.get_filename()
content_disposition = kw['Content-Disposition'] if not file_name:
if content_disposition.split(';')[0] == 'attachment': # get_filename return name only from Content-Disposition header
file_name = re.findall(file_name_regexp, content_disposition, re.MULTILINE) # of the message but sometimes this value is stored in
if file_name: # Content-Type header
kw['file_name'] = file_name[0] if 'Content-Type' in kw:
else: content_type_header = kw['Content-Type']
kw['file_name'] = 'attachment_%s' % i elif 'Content-type' in kw:
elif content_disposition.split(';')[0] == 'inline': content_type_header = kw['Content-Type']
file_name = re.findall(file_name_regexp, content_disposition, re.MULTILINE) file_name_list = re.findall(file_name_regexp,
if file_name: content_type_header,
kw['file_name'] = file_name[0] re.MULTILINE)
else: if file_name_list:
kw['file_name'] = 'inline_%s' % i file_name = file_name_list[0]
else: if file_name:
kw['file_name'] = 'part_%s' % i kw['file_name'] = file_name
if kw.has_key('Content-Type'): else:
content_type = kw['Content-Type'] content_disposition = None
file_name = re.findall(file_name_regexp, content_type, re.MULTILINE) prefix = 'part_'
if file_name: kw['file_name'] = file_name[0] if 'Content-Disposition' in kw:
kw['content_type'] = content_type.split(';')[0] content_disposition = kw['Content-Disposition']
elif 'Content-disposition' in kw:
content_disposition = kw['Content-disposition']
if content_disposition:
if content_disposition.split(';')[0] == 'attachment':
prefix = 'attachment_'
elif content_disposition.split(';')[0] == 'inline':
prefix = 'inline_'
kw['file_name'] = '%s%s' % (prefix, i)
kw['content_type'] = part.get_content_type()
result.append(kw) result.append(kw)
i += 1
return result return result
security.declareProtected(Permissions.AccessContentsInformation, 'getAttachmentData') security.declareProtected(Permissions.AccessContentsInformation, 'getAttachmentData')
...@@ -256,34 +263,27 @@ class EmailDocument(File, TextDocument): ...@@ -256,34 +263,27 @@ class EmailDocument(File, TextDocument):
if index == i: if index == i:
# This part should be handled in skin script # This part should be handled in skin script
# but it was a bit easier to access items here # but it was a bit easier to access items here
kw = dict(part.items())
content_type = part.get_content_type()
if REQUEST is not None: if REQUEST is not None:
kw = dict(part.items()) file_name = part.get_filename()
if not file_name:
# get_filename return name only from Content-Disposition header
# of the message but sometimes this value is stored in
# Content-Type header
if 'Content-Type' in kw:
content_type_header = kw['Content-Type']
elif 'Content-type' in kw:
content_type_header = kw['Content-Type']
file_name_list = re.findall(file_name_regexp,
content_type_header,
re.MULTILINE)
if file_name_list:
file_name = file_name_list[0]
RESPONSE = REQUEST.RESPONSE RESPONSE = REQUEST.RESPONSE
RESPONSE.setHeader('Accept-Ranges', 'bytes') RESPONSE.setHeader('Accept-Ranges', 'bytes')
if kw.has_key('Content-Type'): if content_type and file_name:
content_type = kw['Content-Type']
RESPONSE.setHeader('Content-Type', content_type) RESPONSE.setHeader('Content-Type', content_type)
elif kw.has_key('Content-type'):
content_type = kw['Content-type']
RESPONSE.setHeader('Content-Type', content_type)
else:
content_type = None
if kw.has_key('Content-Disposition'):
content_disposition = kw['Content-Disposition']
elif kw.has_key('Content-disposition'):
content_disposition = kw['Content-disposition']
else:
content_disposition = None
file_name = None
if content_type:
file_name = re.findall(file_name_regexp, content_type, re.MULTILINE)
if content_disposition:
if not file_name:
file_name = re.findall(file_name_regexp,
content_disposition,
re.MULTILINE)
if file_name:
file_name = file_name[0]
RESPONSE.setHeader('Content-disposition', RESPONSE.setHeader('Content-disposition',
'attachment; filename="%s"' % file_name) 'attachment; filename="%s"' % file_name)
if 'text/html' in content_type: if 'text/html' in content_type:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment