1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-26 10:52:17 -05:00

[WO-409] improve method to inline pictures

This commit is contained in:
Felix Hammerl 2014-06-25 16:05:14 +02:00
parent 3626b4d556
commit 7a896a66d1

View File

@ -1477,32 +1477,33 @@ define(function(require) {
/**
* Helper function that looks through the HTML content for <img src="cid:..."> 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 = /<img[^>]+src=['"]cid:([^'">]+)['"]/ig,
match, internalReference, payload;
message.html = message.html.replace(/(<img[^>]+\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;