🔀 Merge branch 'RicardoE105-feature/webhook-binary-data'

This commit is contained in:
Jan Oberhauser 2020-03-21 23:40:14 +01:00
commit d98b45d122

View file

@ -219,6 +219,35 @@ export class Webhook implements INodeType {
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Binary Data',
name: 'binaryData',
type: 'boolean',
displayOptions: {
show: {
'/httpMethod': [
'POST',
],
},
},
default: false,
description: 'Set to true if webhook will receive binary data.',
},
{
displayName: 'Binary Property',
name: 'binaryPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
binaryData: [
true,
],
},
},
description: 'Name of the binary property to which to<br />write the data of the received file.',
},
{
displayName: 'Response Content-Type',
name: 'responseContentType',
@ -258,6 +287,13 @@ export class Webhook implements INodeType {
displayName: 'Raw Body',
name: 'rawBody',
type: 'boolean',
displayOptions: {
hide: {
binaryData: [
true,
],
},
},
default: false,
description: 'Raw body (binary)',
},
@ -266,10 +302,9 @@ export class Webhook implements INodeType {
],
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const authentication = this.getNodeParameter('authentication', 0) as string;
const options = this.getNodeParameter('options', 0) as IDataObject;
const authentication = this.getNodeParameter('authentication') as string;
const options = this.getNodeParameter('options', {}) as IDataObject;
const req = this.getRequestObject();
const resp = this.getResponseObject();
const headers = this.getHeaderData();
@ -347,6 +382,43 @@ export class Webhook implements INodeType {
});
}
if (options.binaryData === true) {
return new Promise((resolve, reject) => {
const binaryPropertyName = options.binaryPropertyName || 'data';
const data: Buffer[] = [];
req.on('data', (chunk) => {
data.push(chunk);
});
req.on('end', async () => {
const returnItem: INodeExecutionData = {
binary: {},
json: {},
};
const returnData: IDataObject[] = [{ json: {} }];
set(returnData[0], `binary[${binaryPropertyName}]`, {
data: Buffer.concat(data).toString(BINARY_ENCODING),
mimeType: req.headers['content-type'],
});
returnItem.binary![binaryPropertyName as string] = await this.helpers.prepareBinaryData(Buffer.concat(data));
return resolve({
workflowData: [
[
returnItem
]
],
});
});
req.on('error', (err) => {
throw new Error(err.message);
});
});
}
const response: INodeExecutionData = {
json: {
body: this.getBodyData(),
@ -354,6 +426,7 @@ export class Webhook implements INodeType {
query: this.getQueryData(),
},
};
if (options.rawBody) {
response.binary = {
data: {