From f4bc3acdd593f453eae7126d242c7664e9187ae0 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Thu, 15 Feb 2018 11:02:38 +0100 Subject: [PATCH] [FIX] mail: correct errors in b64 images handling Rev 666b59484201004e2cd8890a9c09b6dcf023a725 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 --- addons/mail/models/mail_message.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py index 56eaba8d3bd..d0ed1b9fbbb 100644 --- a/addons/mail/models/mail_message.py +++ b/addons/mail/models/mail_message.py @@ -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