mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 02:21:13 +00:00
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:
committed by
GitHub
parent
0a7a2f3e41
commit
528439cb4d
@@ -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),
|
||||
};
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user