From 7a896a66d1b60b5593c45e747419c63d5d8eee46 Mon Sep 17 00:00:00 2001 From: Felix Hammerl Date: Wed, 25 Jun 2014 16:05:14 +0200 Subject: [PATCH] [WO-409] improve method to inline pictures --- src/js/dao/email-dao.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index 8fd6e03..7614249 100644 --- a/src/js/dao/email-dao.js +++ b/src/js/dao/email-dao.js @@ -1477,32 +1477,33 @@ define(function(require) { /** * Helper function that looks through the HTML content for and - * inlines the images linked internally. Manipulates message.html as a side-effect + * inlines the images linked internally. Manipulates message.html as a side-effect. + * If no attachment matching the internal reference is found, or constructing a data + * uri fails, just remove the source. * * @param {Object} message DTO */ function inlineExternalImages(message) { - var imgRegex = /]+src=['"]cid:([^'">]+)['"]/ig, - match, internalReference, payload; + message.html = message.html.replace(/(]+\bsrc=['"])cid:([^'">]+)(['"])/ig, function(match, prefix, src, suffix) { + var localSource = '', + payload = ''; - // find internally referenced images and replace them with a base64 data uri - for (match = imgRegex.exec(message.html); !!match; match = imgRegex.exec(message.html)) { - internalReference = _.findWhere(message.attachments, { - id: match[1] + var internalReference = _.findWhere(message.attachments, { + id: src }); if (internalReference) { - payload = ''; for (var i = 0; i < internalReference.content.byteLength; i++) { payload += String.fromCharCode(internalReference.content[i]); } try { - // btoa fails for all kinds of angry reasons, so if it fails ignore it, we just don't see the img then - message.html = message.html.replace('cid:' + match[1], 'data:application/octet-stream;base64,' + btoa(payload)); - } catch (e) { /* ignore*/ } + localSource = 'data:application/octet-stream;base64,' + btoa(payload); // try to replace the source + } catch (e) {} } - } + + return prefix + localSource + suffix; + }); } return EmailDAO;