From 2f3c45d98e252be95a36ba284efdd9c59f4ae5f0 Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Mon, 29 Apr 2019 23:20:38 +0300 Subject: [PATCH] update reuse cid for same data url --- lib/api/messages.js | 5 +++++ lib/api/submit.js | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/lib/api/messages.js b/lib/api/messages.js index 0a42ecb9..6712f342 100644 --- a/lib/api/messages.js +++ b/lib/api/messages.js @@ -2569,7 +2569,11 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler) => { if (data.html && typeof data.html === 'string') { let fromAddress = (data.from && data.from.address).toString() || os.hostname(); + let cids = new Map(); data.html = data.html.replace(/(]* src\s*=[\s"']*)(data:[^"'>\s]+)/gi, (match, prefix, dataUri) => { + if (cids.has(dataUri)) { + return prefix + 'cid:' + cids.get(dataUri); + } let cid = uuid.v4() + '-attachments@' + fromAddress.split('@').pop(); data.attachments.push( processDataUrl({ @@ -2577,6 +2581,7 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler) => { cid }) ); + cids.set(dataUri, cid); return prefix + 'cid:' + cid; }); } diff --git a/lib/api/submit.js b/lib/api/submit.js index 586f4f22..9885cadf 100644 --- a/lib/api/submit.js +++ b/lib/api/submit.js @@ -330,7 +330,11 @@ module.exports = (db, server, messageHandler, userHandler) => { if (data.html && typeof data.html === 'string') { let fromAddress = (data.from && data.from.address).toString() || os.hostname(); + let cids = new Map(); data.html = data.html.replace(/(]* src\s*=[\s"']*)(data:[^"'>\s]+)/gi, (match, prefix, dataUri) => { + if (cids.has(dataUri)) { + return prefix + 'cid:' + cids.get(dataUri); + } let cid = uuid.v4() + '-attachments@' + fromAddress.split('@').pop(); data.attachments.push( processDataUrl({ @@ -338,6 +342,7 @@ module.exports = (db, server, messageHandler, userHandler) => { cid }) ); + cids.set(dataUri, cid); return prefix + 'cid:' + cid; }); }