From 528439cb4d5ff3f61dddf75dea6377f508429155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Wed, 30 Nov 2022 10:57:13 +0100 Subject: [PATCH] fix(Microsoft Outlook Node): Fix binary attachment upload (#4766) fix(Microsoft Outlook Node): fix binary attachment upload in file-system mode --- .../Microsoft/Outlook/GenericFunctions.ts | 51 ++++++++++- .../Outlook/MicrosoftOutlook.node.ts | 84 ++----------------- 2 files changed, 57 insertions(+), 78 deletions(-) diff --git a/packages/nodes-base/nodes/Microsoft/Outlook/GenericFunctions.ts b/packages/nodes-base/nodes/Microsoft/Outlook/GenericFunctions.ts index 6b0056aed5..29ca1ee9d5 100644 --- a/packages/nodes-base/nodes/Microsoft/Outlook/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Microsoft/Outlook/GenericFunctions.ts @@ -1,8 +1,19 @@ import { OptionsWithUri } from 'request'; -import { IExecuteFunctions, IExecuteSingleFunctions, ILoadOptionsFunctions } from 'n8n-core'; +import { + BINARY_ENCODING, + IExecuteFunctions, + IExecuteSingleFunctions, + ILoadOptionsFunctions, +} from 'n8n-core'; -import { IDataObject, INodeExecutionData, NodeApiError } from 'n8n-workflow'; +import { + IBinaryKeyData, + IDataObject, + INodeExecutionData, + NodeApiError, + NodeOperationError, +} from 'n8n-workflow'; export async function microsoftApiRequest( this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, @@ -221,3 +232,39 @@ export async function downloadAttachments( } return elements; } + +export async function binaryToAttachments( + this: IExecuteFunctions, + attachments: IDataObject[], + items: INodeExecutionData[], + i: number, +) { + return Promise.all( + attachments.map(async (attachment) => { + const { binary } = items[i]; + + if (binary === undefined) { + throw new NodeOperationError(this.getNode(), 'No binary data exists on item!', { + itemIndex: i, + }); + } + + const binaryPropertyName = attachment.binaryPropertyName as string; + if (binary[binaryPropertyName] === undefined) { + throw new NodeOperationError( + this.getNode(), + `No binary data property "${binaryPropertyName}" does not exists on item!`, + { itemIndex: i }, + ); + } + + const binaryData = (binary as IBinaryKeyData)[binaryPropertyName]; + const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName); + return { + '@odata.type': '#microsoft.graph.fileAttachment', + name: binaryData.fileName, + contentBytes: dataBuffer.toString(BINARY_ENCODING), + }; + }), + ); +} diff --git a/packages/nodes-base/nodes/Microsoft/Outlook/MicrosoftOutlook.node.ts b/packages/nodes-base/nodes/Microsoft/Outlook/MicrosoftOutlook.node.ts index 03cfcca05b..54f2c6373c 100644 --- a/packages/nodes-base/nodes/Microsoft/Outlook/MicrosoftOutlook.node.ts +++ b/packages/nodes-base/nodes/Microsoft/Outlook/MicrosoftOutlook.node.ts @@ -13,6 +13,7 @@ import { } from 'n8n-workflow'; import { + binaryToAttachments, createMessage, downloadAttachments, makeRecipient, @@ -250,31 +251,8 @@ export class MicrosoftOutlook implements INodeType { const attachments = (additionalFields.attachments as IDataObject) .attachments as IDataObject[]; - // // Handle attachments - body['attachments'] = attachments.map((attachment) => { - const binaryPropertyName = attachment.binaryPropertyName as string; - - if (items[i].binary === undefined) { - throw new NodeOperationError(this.getNode(), 'No binary data exists on item!', { - itemIndex: i, - }); - } - //@ts-ignore - if (items[i].binary[binaryPropertyName] === undefined) { - throw new NodeOperationError( - this.getNode(), - `No binary data property "${binaryPropertyName}" does not exists on item!`, - { itemIndex: i }, - ); - } - - const binaryData = (items[i].binary as IBinaryKeyData)[binaryPropertyName]; - return { - '@odata.type': '#microsoft.graph.fileAttachment', - name: binaryData.fileName, - contentBytes: binaryData.data, - }; - }); + // Handle attachments + body['attachments'] = await binaryToAttachments.call(this, attachments, items, i); } responseData = await microsoftApiRequest.call(this, 'POST', `/messages`, body, {}); @@ -367,31 +345,8 @@ export class MicrosoftOutlook implements INodeType { if (additionalFields.attachments) { const attachments = (additionalFields.attachments as IDataObject) .attachments as IDataObject[]; - // // Handle attachments - const data = attachments.map((attachment) => { - const binaryPropertyName = attachment.binaryPropertyName as string; - - if (items[i].binary === undefined) { - throw new NodeOperationError(this.getNode(), 'No binary data exists on item!', { - itemIndex: i, - }); - } - //@ts-ignore - if (items[i].binary[binaryPropertyName] === undefined) { - throw new NodeOperationError( - this.getNode(), - `No binary data property "${binaryPropertyName}" does not exists on item!`, - { itemIndex: i }, - ); - } - - const binaryData = (items[i].binary as IBinaryKeyData)[binaryPropertyName]; - return { - '@odata.type': '#microsoft.graph.fileAttachment', - name: binaryData.fileName, - contentBytes: binaryData.data, - }; - }); + // Handle attachments + const data = await binaryToAttachments.call(this, attachments, items, i); for (const attachment of data) { await microsoftApiRequest.call( @@ -583,31 +538,8 @@ export class MicrosoftOutlook implements INodeType { const attachments = (additionalFields.attachments as IDataObject) .attachments as IDataObject[]; - // // Handle attachments - message['attachments'] = attachments.map((attachment) => { - const binaryPropertyName = attachment.binaryPropertyName as string; - - if (items[i].binary === undefined) { - throw new NodeOperationError(this.getNode(), 'No binary data exists on item!', { - itemIndex: i, - }); - } - //@ts-ignore - if (items[i].binary[binaryPropertyName] === undefined) { - throw new NodeOperationError( - this.getNode(), - `No binary data property "${binaryPropertyName}" does not exists on item!`, - { itemIndex: i }, - ); - } - - const binaryData = (items[i].binary as IBinaryKeyData)[binaryPropertyName]; - return { - '@odata.type': '#microsoft.graph.fileAttachment', - name: binaryData.fileName, - contentBytes: binaryData.data, - }; - }); + // Handle attachments + message['attachments'] = await binaryToAttachments.call(this, attachments, items, i); } const body: IDataObject = { @@ -716,7 +648,7 @@ export class MicrosoftOutlook implements INodeType { const body: IDataObject = { '@odata.type': '#microsoft.graph.fileAttachment', name: fileName, - contentBytes: binaryData.data, + contentBytes: dataBuffer.toString('base64'), }; responseData = await microsoftApiRequest.call(