Make it possible to move base64 data around

This commit is contained in:
Jan Oberhauser 2020-03-30 20:11:39 +02:00
parent 554a32d64d
commit 263c1eee1d

View file

@ -6,6 +6,7 @@ import {
import { IExecuteFunctions } from 'n8n-core'; import { IExecuteFunctions } from 'n8n-core';
import { import {
IBinaryData,
IDataObject, IDataObject,
INodeExecutionData, INodeExecutionData,
INodeType, INodeType,
@ -163,6 +164,28 @@ export class MoveBinaryData implements INodeType {
placeholder: 'Add Option', placeholder: 'Add Option',
default: {}, default: {},
options: [ options: [
{
displayName: 'Data Is Base64',
name: 'dataIsBase64',
type: 'boolean',
displayOptions: {
hide: {
'useRawData': [
true,
],
},
show: {
'/mode': [
'jsonToBinary',
],
'/convertAllData': [
false,
],
},
},
default: false,
description: 'Keeps the binary data as base64 string.',
},
{ {
displayName: 'Encoding', displayName: 'Encoding',
name: 'encoding', name: 'encoding',
@ -177,11 +200,31 @@ export class MoveBinaryData implements INodeType {
default: 'utf8', default: 'utf8',
description: 'Set the encoding of the data stream', description: 'Set the encoding of the data stream',
}, },
{
displayName: 'File Name',
name: 'fileName',
type: 'string',
displayOptions: {
show: {
'/mode': [
'jsonToBinary',
],
},
},
default: '',
placeholder: 'example.json',
description: 'The file name to set.',
},
{ {
displayName: 'JSON Parse', displayName: 'JSON Parse',
name: 'jsonParse', name: 'jsonParse',
type: 'boolean', type: 'boolean',
displayOptions: { displayOptions: {
hide: {
'keepAsBase64': [
true,
],
},
show: { show: {
'/mode': [ '/mode': [
'binaryToJson', 'binaryToJson',
@ -192,7 +235,7 @@ export class MoveBinaryData implements INodeType {
}, },
}, },
default: false, default: false,
description: 'Run JSON parse on the data to get propery object data.', description: 'Run JSON parse on the data to get proper object data.',
}, },
{ {
displayName: 'Keep Source', displayName: 'Keep Source',
@ -201,6 +244,28 @@ export class MoveBinaryData implements INodeType {
default: false, default: false,
description: 'If the source key should be kept. By default does it get deleted.', description: 'If the source key should be kept. By default does it get deleted.',
}, },
{
displayName: 'Keep As Base64',
name: 'keepAsBase64',
type: 'boolean',
displayOptions: {
hide: {
'jsonParse': [
true,
],
},
show: {
'/mode': [
'binaryToJson',
],
'/setAllData': [
false,
],
},
},
default: false,
description: 'Keeps the binary data as base64 string.',
},
{ {
displayName: 'Mime Type', displayName: 'Mime Type',
name: 'mimeType', name: 'mimeType',
@ -221,6 +286,11 @@ export class MoveBinaryData implements INodeType {
name: 'useRawData', name: 'useRawData',
type: 'boolean', type: 'boolean',
displayOptions: { displayOptions: {
hide: {
'dataIsBase64': [
true,
],
},
show: { show: {
'/mode': [ '/mode': [
'jsonToBinary', 'jsonToBinary',
@ -267,15 +337,20 @@ export class MoveBinaryData implements INodeType {
} }
const encoding = (options.encoding as string) || 'utf8'; const encoding = (options.encoding as string) || 'utf8';
let convertedValue = Buffer.from(value.data, 'base64').toString(encoding); let convertedValue = value.data;
if (setAllData === true) { if (setAllData === true) {
// Set the full data // Set the full data
convertedValue = Buffer.from(convertedValue, 'base64').toString(encoding);
newItem.json = JSON.parse(convertedValue); newItem.json = JSON.parse(convertedValue);
} else { } else {
// Does get added to existing data so copy it first // Does get added to existing data so copy it first
newItem.json = JSON.parse(JSON.stringify(item.json)); newItem.json = JSON.parse(JSON.stringify(item.json));
if (options.keepAsBase64 !== true) {
convertedValue = Buffer.from(convertedValue, 'base64').toString(encoding);
}
if (options.jsonParse) { if (options.jsonParse) {
convertedValue = JSON.parse(convertedValue); convertedValue = JSON.parse(convertedValue);
} }
@ -316,14 +391,23 @@ export class MoveBinaryData implements INodeType {
newItem.binary = {}; newItem.binary = {};
} }
if (options.useRawData !== true) { if (options.dataIsBase64 !== true) {
value = JSON.stringify(value); if (options.useRawData !== true) {
value = JSON.stringify(value);
}
value = Buffer.from(value as string).toString('base64');
} }
const convertedValue = { const convertedValue: IBinaryData = {
data: Buffer.from(value as string).toString('base64'), data: value as string,
mimeType: options.mimeType || 'application/json', mimeType: (options.mimeType as string) || 'application/json',
}; };
if (options.fileName) {
convertedValue.fileName = options.fileName as string;
}
set(newItem.binary!, destinationKey, convertedValue); set(newItem.binary!, destinationKey, convertedValue);
if (options.keepSource === true) { if (options.keepSource === true) {