fix(Microsoft Outlook Node): Fix binary attachment upload (#4766)

fix(Microsoft Outlook Node): fix binary attachment upload in file-system mode
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2022-11-30 10:57:13 +01:00
committed by GitHub
parent 0a7a2f3e41
commit 528439cb4d
2 changed files with 57 additions and 78 deletions

View File

@@ -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),
};
}),
);
}

View File

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