multipart data

This commit is contained in:
Ricardo Espinoza 2020-02-06 11:21:25 -05:00
parent 24a3a02180
commit ec3421266f

View file

@ -8,12 +8,21 @@ import {
INodeTypeDescription,
INodeType,
IWebhookResponseData,
IRunExecutionData,
} from 'n8n-workflow';
import * as basicAuth from 'basic-auth';
import { Response } from 'express';
import { set } from 'lodash';
import * as fs from 'fs';
import * as formidable from 'formidable';
import { kgsearch } from 'googleapis/build/src/apis/kgsearch';
import { runInThisContext } from 'vm';
function authorizationError(resp: Response, realm: string, responseCode: number, message?: string) {
if (message === undefined) {
message = 'Authorization problem!';
@ -307,6 +316,37 @@ export class Webhook implements INodeType {
// @ts-ignore
const mimeType = headers['content-type'] || 'application/json';
if (mimeType.includes('multipart/form-data')) {
const form = new formidable.IncomingForm();
return new Promise((resolve, reject) => {
form.parse(req, async (err, data, files) => {
const returnData: INodeExecutionData[] = this.helpers.returnJsonArray({
body: data,
headers,
query: this.getQueryData(),
});
for (let file of Object.keys(files)) {
const fileJson = files[file].toJSON() as IDataObject;
const [fileName, fileExtension] = (fileJson.name as string).split('.');
const fileContent = await fs.promises.readFile(files[file].path);
set(returnData[0], `binary[${fileName}]`, {
data: fileContent,
mimeType: fileJson.type,
fileName: fileJson.name,
fileExtension,
})
}
resolve({
workflowData: [
returnData,
],
});
});
});
}
const response: INodeExecutionData = {
json: {