mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-02 07:01:30 -08:00
refactor(WhatsApp Node): Avoid using BinaryDataManager directly from n8n-core (#5544)
This commit is contained in:
parent
e251439333
commit
1bff044252
|
@ -3,38 +3,42 @@ import { NodeOperationError } from 'n8n-workflow';
|
||||||
|
|
||||||
import FormData from 'form-data';
|
import FormData from 'form-data';
|
||||||
|
|
||||||
|
export async function getUploadFormData(
|
||||||
|
this: IExecuteSingleFunctions,
|
||||||
|
): Promise<{ fileName: string; formData: FormData }> {
|
||||||
|
const mediaPropertyName = ((this.getNodeParameter('mediaPropertyName') as string) || '').trim();
|
||||||
|
if (!mediaPropertyName)
|
||||||
|
throw new NodeOperationError(this.getNode(), 'Parameter "mediaPropertyName" is not defined');
|
||||||
|
|
||||||
|
const { binary: binaryData } = this.getInputData();
|
||||||
|
if (!binaryData) throw new NodeOperationError(this.getNode(), 'Binary data missing in input');
|
||||||
|
|
||||||
|
const binaryFile = binaryData[mediaPropertyName];
|
||||||
|
if (binaryFile === undefined)
|
||||||
|
throw new NodeOperationError(this.getNode(), 'Could not find file in node input data');
|
||||||
|
|
||||||
|
const mediaFileName = (this.getNodeParameter('additionalFields') as IDataObject).mediaFileName as
|
||||||
|
| string
|
||||||
|
| undefined;
|
||||||
|
|
||||||
|
const fileName = mediaFileName || binaryFile.fileName;
|
||||||
|
if (!fileName)
|
||||||
|
throw new NodeOperationError(this.getNode(), 'No file name given for media upload.');
|
||||||
|
|
||||||
|
const buffer = await this.helpers.getBinaryDataBuffer(mediaPropertyName);
|
||||||
|
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('file', buffer, { contentType: binaryFile.mimeType, filename: fileName });
|
||||||
|
formData.append('messaging_product', 'whatsapp');
|
||||||
|
|
||||||
|
return { fileName, formData };
|
||||||
|
}
|
||||||
|
|
||||||
export async function setupUpload(
|
export async function setupUpload(
|
||||||
this: IExecuteSingleFunctions,
|
this: IExecuteSingleFunctions,
|
||||||
requestOptions: IHttpRequestOptions,
|
requestOptions: IHttpRequestOptions,
|
||||||
) {
|
) {
|
||||||
const mediaPropertyName = this.getNodeParameter('mediaPropertyName') as string;
|
const uploadData = await getUploadFormData.call(this);
|
||||||
if (!mediaPropertyName) {
|
requestOptions.body = uploadData.formData;
|
||||||
return requestOptions;
|
|
||||||
}
|
|
||||||
if (this.getInputData().binary?.[mediaPropertyName] === undefined || !mediaPropertyName.trim()) {
|
|
||||||
throw new NodeOperationError(this.getNode(), 'Could not find file in node input data', {
|
|
||||||
description: `There’s no key called '${mediaPropertyName}' with binary data in it`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const binaryFile = this.getInputData().binary![mediaPropertyName]!;
|
|
||||||
const mediaFileName = (this.getNodeParameter('additionalFields') as IDataObject).mediaFileName as
|
|
||||||
| string
|
|
||||||
| undefined;
|
|
||||||
const binaryFileName = binaryFile.fileName;
|
|
||||||
if (!mediaFileName && !binaryFileName) {
|
|
||||||
throw new NodeOperationError(this.getNode(), 'No file name given for media upload.');
|
|
||||||
}
|
|
||||||
const mimeType = binaryFile.mimeType;
|
|
||||||
|
|
||||||
const buffer = await this.helpers.getBinaryDataBuffer(mediaPropertyName);
|
|
||||||
|
|
||||||
const data = new FormData();
|
|
||||||
data.append('file', buffer, {
|
|
||||||
contentType: mimeType,
|
|
||||||
filename: mediaFileName || binaryFileName,
|
|
||||||
});
|
|
||||||
data.append('messaging_product', 'whatsapp');
|
|
||||||
|
|
||||||
requestOptions.body = data;
|
|
||||||
return requestOptions;
|
return requestOptions;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import set from 'lodash.set';
|
import set from 'lodash.set';
|
||||||
import { BinaryDataManager } from 'n8n-core';
|
|
||||||
import type {
|
import type {
|
||||||
IDataObject,
|
IDataObject,
|
||||||
IExecuteSingleFunctions,
|
IExecuteSingleFunctions,
|
||||||
|
@ -8,9 +7,9 @@ import type {
|
||||||
INodeExecutionData,
|
INodeExecutionData,
|
||||||
JsonObject,
|
JsonObject,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
import { NodeApiError } from 'n8n-workflow';
|
||||||
|
|
||||||
import FormData from 'form-data';
|
import { getUploadFormData } from './MediaFunctions';
|
||||||
|
|
||||||
interface WhatsAppApiError {
|
interface WhatsAppApiError {
|
||||||
error: {
|
error: {
|
||||||
|
@ -62,29 +61,7 @@ export async function mediaUploadFromItem(
|
||||||
this: IExecuteSingleFunctions,
|
this: IExecuteSingleFunctions,
|
||||||
requestOptions: IHttpRequestOptions,
|
requestOptions: IHttpRequestOptions,
|
||||||
) {
|
) {
|
||||||
const mediaPropertyName = this.getNodeParameter('mediaPropertyName') as string;
|
const uploadData = await getUploadFormData.call(this);
|
||||||
if (this.getInputData().binary?.[mediaPropertyName] === undefined) {
|
|
||||||
throw new NodeOperationError(
|
|
||||||
this.getNode(),
|
|
||||||
`The binary property "${mediaPropertyName}" does not exist. So no file can be written!`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const binaryFile = this.getInputData().binary![mediaPropertyName]!;
|
|
||||||
const mediaFileName = (this.getNodeParameter('additionalFields') as IDataObject).mediaFilename as
|
|
||||||
| string
|
|
||||||
| undefined;
|
|
||||||
const binaryFileName = binaryFile.fileName;
|
|
||||||
if (!mediaFileName && !binaryFileName) {
|
|
||||||
throw new NodeOperationError(this.getNode(), 'No file name given for media upload.');
|
|
||||||
}
|
|
||||||
const mimeType = binaryFile.mimeType;
|
|
||||||
|
|
||||||
const data = new FormData();
|
|
||||||
data.append('file', await BinaryDataManager.getInstance().retrieveBinaryData(binaryFile), {
|
|
||||||
contentType: mimeType,
|
|
||||||
filename: mediaFileName || binaryFileName,
|
|
||||||
});
|
|
||||||
data.append('messaging_product', 'whatsapp');
|
|
||||||
|
|
||||||
const phoneNumberId = this.getNodeParameter('phoneNumberId') as string;
|
const phoneNumberId = this.getNodeParameter('phoneNumberId') as string;
|
||||||
|
|
||||||
|
@ -92,7 +69,7 @@ export async function mediaUploadFromItem(
|
||||||
url: `/${phoneNumberId}/media`,
|
url: `/${phoneNumberId}/media`,
|
||||||
baseURL: requestOptions.baseURL,
|
baseURL: requestOptions.baseURL,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: data,
|
body: uploadData.formData,
|
||||||
})) as IDataObject;
|
})) as IDataObject;
|
||||||
|
|
||||||
const operation = this.getNodeParameter('messageType') as string;
|
const operation = this.getNodeParameter('messageType') as string;
|
||||||
|
@ -101,11 +78,7 @@ export async function mediaUploadFromItem(
|
||||||
}
|
}
|
||||||
set(requestOptions.body as IDataObject, `${operation}.id`, result.id);
|
set(requestOptions.body as IDataObject, `${operation}.id`, result.id);
|
||||||
if (operation === 'document') {
|
if (operation === 'document') {
|
||||||
set(
|
set(requestOptions.body as IDataObject, `${operation}.filename`, uploadData.fileName);
|
||||||
requestOptions.body as IDataObject,
|
|
||||||
`${operation}.filename`,
|
|
||||||
mediaFileName || binaryFileName,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return requestOptions;
|
return requestOptions;
|
||||||
|
|
Loading…
Reference in a new issue