fix(Email Trigger (IMAP) Node): UTF-8 attachments are not correctly named (#6856)

This commit is contained in:
Michael Kret
2023-08-07 13:33:06 +03:00
committed by GitHub
parent 8126181e18
commit 72814d1f0f
3 changed files with 107 additions and 58 deletions

View File

@@ -12,6 +12,7 @@ import type {
INodeTypeBaseDescription,
INodeTypeDescription,
ITriggerResponse,
JsonObject,
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
@@ -19,9 +20,10 @@ import type { ImapSimple, ImapSimpleOptions, Message } from 'imap-simple';
import { connect as imapConnect, getParts } from 'imap-simple';
import type { Source as ParserSource } from 'mailparser';
import { simpleParser } from 'mailparser';
import rfc2047 from 'rfc2047';
import isEmpty from 'lodash/isEmpty';
import find from 'lodash/find';
import type { ICredentialsDataImap } from '../../../credentials/Imap.credentials';
import { isCredentialsDataImap } from '../../../credentials/Imap.credentials';
@@ -32,14 +34,14 @@ export async function parseRawEmail(
): Promise<INodeExecutionData> {
const responseData = await simpleParser(messageEncoded);
const headers: IDataObject = {};
const addidtionalData: IDataObject = {};
for (const header of responseData.headerLines) {
headers[header.key] = header.line;
}
// @ts-ignore
responseData.headers = headers;
// @ts-ignore
responseData.headerLines = undefined;
addidtionalData.headers = headers;
addidtionalData.headerLines = undefined;
const binaryData: IBinaryKeyData = {};
if (responseData.attachments) {
@@ -51,12 +53,12 @@ export async function parseRawEmail(
attachment.contentType,
);
}
// @ts-ignore
responseData.attachments = undefined;
addidtionalData.attachments = undefined;
}
return {
json: responseData as unknown as IDataObject,
json: { ...responseData, ...addidtionalData },
binary: Object.keys(binaryData).length ? binaryData : undefined,
} as INodeExecutionData;
}
@@ -260,7 +262,7 @@ export class EmailReadImapV2 implements INodeType {
} catch (error) {
return {
status: 'Error',
message: error.message,
message: (error as Error).message,
};
}
return {
@@ -296,14 +298,15 @@ export class EmailReadImapV2 implements INodeType {
// Returns the email text
const getText = async (parts: any[], message: Message, subtype: string) => {
const getText = async (parts: IDataObject[], message: Message, subtype: string) => {
if (!message.attributes.struct) {
return '';
}
const textParts = parts.filter((part) => {
return (
part.type.toUpperCase() === 'TEXT' && part.subtype.toUpperCase() === subtype.toUpperCase()
(part.type as string).toUpperCase() === 'TEXT' &&
(part.subtype as string).toUpperCase() === subtype.toUpperCase()
);
});
@@ -312,7 +315,7 @@ export class EmailReadImapV2 implements INodeType {
}
try {
return await connection.getPartData(message, textParts[0]);
return (await connection.getPartData(message, textParts[0])) as string;
} catch {
return '';
}
@@ -321,7 +324,7 @@ export class EmailReadImapV2 implements INodeType {
// Returns the email attachments
const getAttachment = async (
imapConnection: ImapSimple,
parts: any[],
parts: IDataObject[],
message: Message,
): Promise<IBinaryData[]> => {
if (!message.attributes.struct) {
@@ -330,20 +333,33 @@ export class EmailReadImapV2 implements INodeType {
// Check if the message has attachments and if so get them
const attachmentParts = parts.filter((part) => {
return part.disposition && part.disposition.type.toUpperCase() === 'ATTACHMENT';
return (
part.disposition &&
((part.disposition as IDataObject)?.type as string).toUpperCase() === 'ATTACHMENT'
);
});
const decodeFilename = (filename: string) => {
const regex = /=\?([\w-]+)\?Q\?.*\?=/i;
if (regex.test(filename)) {
return rfc2047.decode(filename);
}
return filename;
};
const attachmentPromises = [];
let attachmentPromise;
for (const attachmentPart of attachmentParts) {
attachmentPromise = imapConnection
.getPartData(message, attachmentPart)
.then(async (partData) => {
// Return it in the format n8n expects
return this.helpers.prepareBinaryData(
partData as Buffer,
attachmentPart.disposition.params.filename as string,
// if filename contains utf-8 encoded characters, decode it
const fileName = decodeFilename(
((attachmentPart.disposition as IDataObject)?.params as IDataObject)
?.filename as string,
);
// Return it in the format n8n expects
return this.helpers.prepareBinaryData(partData as Buffer, fileName);
});
attachmentPromises.push(attachmentPromise);
@@ -440,7 +456,7 @@ export class EmailReadImapV2 implements INodeType {
) {
staticData.lastMessageUid = message.attributes.uid;
}
const parts = getParts(message.attributes.struct!);
const parts = getParts(message.attributes.struct as IDataObject[]) as IDataObject[];
newEmail = {
json: {
@@ -454,14 +470,15 @@ export class EmailReadImapV2 implements INodeType {
return part.which === 'HEADER';
});
messageBody = messageHeader[0].body;
for (propertyName of Object.keys(messageBody as IDataObject)) {
if (messageBody[propertyName].length) {
messageBody = messageHeader[0].body as IDataObject;
for (propertyName of Object.keys(messageBody)) {
if ((messageBody[propertyName] as IDataObject[]).length) {
if (topLevelProperties.includes(propertyName)) {
newEmail.json[propertyName] = messageBody[propertyName][0];
newEmail.json[propertyName] = (messageBody[propertyName] as string[])[0];
} else {
(newEmail.json.metadata as IDataObject)[propertyName] =
messageBody[propertyName][0];
(newEmail.json.metadata as IDataObject)[propertyName] = (
messageBody[propertyName] as string[]
)[0];
}
}
}
@@ -501,7 +518,7 @@ export class EmailReadImapV2 implements INodeType {
// Return base64 string
newEmail = {
json: {
raw: part.body,
raw: part.body as string,
},
};
@@ -525,7 +542,9 @@ export class EmailReadImapV2 implements INodeType {
let searchCriteria = ['UNSEEN'] as Array<string | string[]>;
if (options.customEmailConfig !== undefined) {
try {
searchCriteria = JSON.parse(options.customEmailConfig as string);
searchCriteria = JSON.parse(options.customEmailConfig as string) as Array<
string | string[]
>;
} catch (error) {
throw new NodeOperationError(this.getNode(), 'Custom email config is not valid JSON.');
}
@@ -568,7 +587,7 @@ export class EmailReadImapV2 implements INodeType {
}
} catch (error) {
this.logger.error('Email Read Imap node encountered an error fetching new emails', {
error,
error: error as Error,
});
// Wait with resolving till the returnedPromise got resolved, else n8n will be unhappy
// if it receives an error before the workflow got activated
@@ -611,8 +630,10 @@ export class EmailReadImapV2 implements INodeType {
}
});
conn.on('error', async (error) => {
const errorCode = error.code.toUpperCase();
this.logger.verbose(`IMAP connection experienced an error: (${errorCode})`, { error });
const errorCode = ((error as JsonObject).code as string).toUpperCase();
this.logger.verbose(`IMAP connection experienced an error: (${errorCode})`, {
error: error as Error,
});
// eslint-disable-next-line @typescript-eslint/no-use-before-define
await closeFunction();
this.emitError(error as Error);
@@ -627,22 +648,24 @@ export class EmailReadImapV2 implements INodeType {
let reconnectionInterval: NodeJS.Timeout | undefined;
const handleReconect = async () => {
this.logger.verbose('Forcing reconnect to IMAP server');
try {
isCurrentlyReconnecting = true;
if (connection.closeBox) await connection.closeBox(false);
connection.end();
connection = await establishConnection();
await connection.openBox(mailbox);
} catch (error) {
this.logger.error(error as string);
} finally {
isCurrentlyReconnecting = false;
}
};
if (options.forceReconnect !== undefined) {
reconnectionInterval = setInterval(
async () => {
this.logger.verbose('Forcing reconnect to IMAP server');
try {
isCurrentlyReconnecting = true;
if (connection.closeBox) await connection.closeBox(false);
connection.end();
connection = await establishConnection();
await connection.openBox(mailbox);
} catch (error) {
this.logger.error(error as string);
} finally {
isCurrentlyReconnecting = false;
}
},
handleReconect,
(options.forceReconnect as number) * 1000 * 60,
);
}