replace image links

This commit is contained in:
Andris Reinman 2017-07-21 21:58:25 +03:00
parent 650e622d7b
commit cb0de3a15b

18
api.js
View file

@ -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,