Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZMS-134 #655

Merged
merged 8 commits into from
Apr 8, 2024
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 35 additions & 2 deletions lib/api/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const TaskHandler = require('../task-handler');
const { prepareSearchFilter, uidRangeStringToQuery } = require('../prepare-search-filter');
const { getMongoDBQuery /*, getElasticSearchQuery*/ } = require('../search-query');
//const { getClient } = require('../elasticsearch');
let iconv = require('iconv-lite');

const BimiHandler = require('../bimi-handler');
const {
Expand Down Expand Up @@ -1505,7 +1506,11 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler, setti
summary: 'Download Attachment',
description: 'This method returns attachment file contents in binary form',
validationObjs: {
queryParams: {},
queryParams: {
sendAsString: booleanSchema
.default(false)
.description('If true then sends the original attachment back in string format with correct encoding.')
},
pathParams: {
user: userId,
mailbox: mailboxId,
Expand Down Expand Up @@ -1577,7 +1582,7 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler, setti
_id: true,
user: true,
attachments: true,
'mimeTree.attachmentMap': true
mimeTree: true
}
}
);
Expand Down Expand Up @@ -1614,6 +1619,8 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler, setti
});
}

let attachmentCharset = getAttachmentCharset(messageData.mimeTree, attachment)[0];
NickOvt marked this conversation as resolved.
Show resolved Hide resolved

res.writeHead(200, {
'Content-Type': attachmentData.contentType || 'application/octet-stream'
});
Expand Down Expand Up @@ -1655,6 +1662,10 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler, setti
} else if (attachmentData.transferEncoding === 'quoted-printable') {
attachmentStream.pipe(new libqp.Decoder()).pipe(res);
} else {
if (!/ascii|utf[-_]?8/.test(attachmentCharset.toLowerCase()) && result.value.sendAsString) {
NickOvt marked this conversation as resolved.
Show resolved Hide resolved
attachmentStream.pipe(iconv.decodeStream(attachmentCharset)).pipe(res);
return;
}
attachmentStream.pipe(res);
}
})
Expand Down Expand Up @@ -3878,3 +3889,25 @@ function parseAddresses(data) {
walk([].concat(data || []));
return Array.from(addresses);
}

function getAttachmentCharset(mimeTree, attachmentId) {
if (mimeTree.attachmentId && mimeTree.attachmentId === attachmentId) {
// current mimeTree (sub)object has the attachmentId field, and it is the one we search
// get the parsedHeader -> content-type -> params -> charset

return [mimeTree.parsedHeader['content-type'].params.charset || 'UTF-8', true];
NickOvt marked this conversation as resolved.
Show resolved Hide resolved
} else if (mimeTree.childNodes) {
// current mimetree (sub)object does not have the attachmentId field and it is not equal to the one we search
// loop childNodes
let charset;
for (const childNode of Object.values(mimeTree.childNodes)) {
charset = getAttachmentCharset(childNode, attachmentId);
if (charset[1] === true) {
// actually found the charset, early return
return charset;
}
}
}

return ['UTF-8', false];
}