From cb0de3a15b00167f452ce56c8116ea5096c69633 Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Fri, 21 Jul 2017 21:58:25 +0300 Subject: [PATCH] replace image links --- api.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/api.js b/api.js index 636dfed9..f93f8d39 100644 --- a/api.js +++ b/api.js @@ -1809,9 +1809,14 @@ server.get('/users/:user/mailboxes/:mailbox/messages/:message', (req, res, next) const schema = Joi.object().keys({ user: Joi.string().hex().lowercase().length(24).required(), mailbox: Joi.string().hex().lowercase().length(24).required(), - message: Joi.string().regex(/^[0-9a-f]{24}:\d{1,10}/).lowercase().required() + message: Joi.string().regex(/^[0-9a-f]{24}:\d{1,10}/).lowercase().required(), + replaceCidLinks: Joi.boolean().truthy(['Y', 'true', 'yes', 1]).default(false) }); + if (req.query.replaceCidLinks) { + req.params.replaceCidLinks = req.query.replaceCidLinks; + } + const result = Joi.validate(req.params, schema, { abortEarly: false, convert: true @@ -1829,6 +1834,7 @@ server.get('/users/:user/mailboxes/:mailbox/messages/:message', (req, res, next) let mailbox = new ObjectID(result.value.mailbox); let message = new ObjectID(messageparts[0]); let uid = Number(messageparts[1]); + let replaceCidLinks = result.value.replaceCidLinks; db.users.collection('messages').findOne({ _id: message, @@ -1921,6 +1927,16 @@ server.get('/users/:user/mailboxes/:mailbox/messages/:message', (req, res, next) expires = new Date(messageData.rdate).toISOString(); } + if (replaceCidLinks) { + messageData.html = (messageData.html || []) + .map(html => + html.replace( + /attachment:([a-f0-9]+)\/(ATT\d+)/g, + (str, mid, aid) => '/users/' + user + '/mailboxes/' + mailbox + '/messages/' + message + ':' + uid + '/attachments/' + aid + ) + ); + } + res.json({ success: true, id: message.toString() + ':' + uid,