Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
39 lines
1.4 KiB
TypeScript
39 lines
1.4 KiB
TypeScript
import FormData from 'form-data';
|
|
import type { IDataObject, IExecuteSingleFunctions, IHttpRequestOptions } from 'n8n-workflow';
|
|
import { NodeOperationError } from 'n8n-workflow';
|
|
|
|
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 binaryData = this.helpers.assertBinaryData(mediaPropertyName);
|
|
|
|
const mediaFileName = (this.getNodeParameter('additionalFields') as IDataObject).mediaFileName as
|
|
| string
|
|
| undefined;
|
|
|
|
const fileName = mediaFileName || binaryData.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: binaryData.mimeType, filename: fileName });
|
|
formData.append('messaging_product', 'whatsapp');
|
|
|
|
return { fileName, formData };
|
|
}
|
|
|
|
export async function setupUpload(
|
|
this: IExecuteSingleFunctions,
|
|
requestOptions: IHttpRequestOptions,
|
|
) {
|
|
const uploadData = await getUploadFormData.call(this);
|
|
requestOptions.body = uploadData.formData;
|
|
return requestOptions;
|
|
}
|