[FIX] mail: correct errors in b64 images handling

Rev 666b594842 introduced an autodetection
mechanism for transforming base64 embedded images inside emails into
attachment with a Content-ID URL.

The patch suffered from several issues:

- the base64 detection regex failed to detect base64 data that is folded
  over multiple lines - which happens to be the default in many Python
  versions when a binary field is read, or when using the base64 codec
  to encode a string.
  (Those cases where left untouched and the image was therefore missing)
- there was a logic error in the code when the same image was present
  multiple times, leading to an exception (`name` variable was
  referenced before being assigned)

This patch addresses both issues.

Fixes OPW-815073
This commit is contained in:
Olivier Dony
2018-02-15 11:12:28 +01:00
parent 7792755946
commit f4bc3acdd5
+4 -3
View File
@@ -12,7 +12,7 @@ from odoo.osv import expression
_logger = logging.getLogger(__name__)
_image_dataurl = re.compile(r'(data:image/[a-z]+?);base64,([a-z0-9+/]{3,}=*)([\'"])', re.I)
_image_dataurl = re.compile(r'(data:image/[a-z]+?);base64,([a-z0-9+/\n]{3,}=*)\n*([\'"])', re.I)
class Message(models.Model):
@@ -757,8 +757,9 @@ class Message(models.Model):
'res_model': 'mail.message',
})
values['attachment_ids'].append((4, attachment.id))
data_to_url[key] = '/web/image/%s' % attachment.id
return '%s%s alt="%s"' % (data_to_url[key], match.group(3), name)
data_to_url[key] = (name, '/web/image/%s' % attachment.id)
alt, url = data_to_url[key]
return '%s%s alt="%s"' % (url, match.group(3), alt)
values['body'] = _image_dataurl.sub(base64_to_boundary, values['body'])
# delegate creation of tracking after the create as sudo to avoid access rights issues