1
1
mirror of https://github.com/n8n-io/n8n.git synced 2024-11-13 05:35:28 +03:00

:add: Add iconv-lite for encoding/decoding for MoveBinaryData (#1473)

This commit is contained in:
lublak 2021-03-25 12:58:54 +01:00 committed by GitHub
parent 8699f174f7
commit c3496845bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 7 deletions

View File

@ -4,6 +4,10 @@ import {
unset,
} from 'lodash';
import {
BINARY_ENCODING,
} from 'n8n-core';
import { IExecuteFunctions } from 'n8n-core';
import {
IBinaryData,
@ -13,6 +17,18 @@ import {
INodeTypeDescription,
} from 'n8n-workflow';
import * as iconv from 'iconv-lite';
iconv.encodingExists('utf8');
const bomAware: string[] = [];
const encodeDecodeOptions: INodePropertyOptions[] = [];
Object.keys((iconv as any).encodings).forEach(encoding => {
if(!(encoding.startsWith('_') || typeof (iconv as any).encodings[encoding] == 'string')) { // only encodings without direct alias or internals
if((iconv as any).encodings[encoding].bomAware) {
bomAware.push(encoding);
}
encodeDecodeOptions.push({ name: encoding, value: encoding});
}
});
export class MoveBinaryData implements INodeType {
description: INodeTypeDescription = {
@ -189,17 +205,47 @@ export class MoveBinaryData implements INodeType {
{
displayName: 'Encoding',
name: 'encoding',
type: 'string',
type: 'options',
options: encodeDecodeOptions,
displayOptions: {
show: {
'/mode': [
'binaryToJson',
'jsonToBinary',
],
},
},
default: 'utf8',
description: 'Set the encoding of the data stream',
},
{
displayName: 'Strip BOM',
name: 'stripBOM',
displayOptions: {
show: {
'/mode': [
'binaryToJson',
],
encoding: bomAware
},
},
type: 'boolean',
default: true,
},
{
displayName: 'Add BOM',
name: 'addBOM',
displayOptions: {
show: {
'/mode': [
'jsonToBinary',
],
encoding: bomAware
},
},
type: 'boolean',
default: false,
},
{
displayName: 'File Name',
name: 'fileName',
@ -336,19 +382,19 @@ export class MoveBinaryData implements INodeType {
continue;
}
const encoding = (options.encoding as BufferEncoding) || 'utf8';
const encoding = (options.encoding as string) || 'utf8';
let convertedValue = value.data;
if (setAllData === true) {
// Set the full data
convertedValue = Buffer.from(convertedValue, 'base64').toString(encoding);
convertedValue = iconv.decode(Buffer.from(convertedValue, BINARY_ENCODING), encoding, {stripBOM: options.stripBOM as boolean});
newItem.json = JSON.parse(convertedValue);
} else {
// Does get added to existing data so copy it first
newItem.json = JSON.parse(JSON.stringify(item.json));
if (options.keepAsBase64 !== true) {
convertedValue = Buffer.from(convertedValue, 'base64').toString(encoding);
convertedValue = iconv.decode(Buffer.from(convertedValue, BINARY_ENCODING), encoding, {stripBOM: options.stripBOM as boolean});
}
if (options.jsonParse) {
@ -372,6 +418,7 @@ export class MoveBinaryData implements INodeType {
const convertAllData = this.getNodeParameter('convertAllData', itemIndex) as boolean;
const destinationKey = this.getNodeParameter('destinationKey', itemIndex) as string;
const encoding = (options.encoding as string) || 'utf8';
let value: IDataObject | string = item.json;
if (convertAllData === false) {
const sourceKey = this.getNodeParameter('sourceKey', itemIndex) as string;
@ -396,7 +443,7 @@ export class MoveBinaryData implements INodeType {
value = JSON.stringify(value);
}
value = Buffer.from(value as string).toString('base64');
value = iconv.encode(value as string, encoding, {addBOM: options.addBOM as boolean}).toString(BINARY_ENCODING);
}
const convertedValue: IBinaryData = {
@ -437,4 +484,4 @@ export class MoveBinaryData implements INodeType {
return [returnData];
}
}
}

View File

@ -589,6 +589,7 @@
"get-system-fonts": "^2.0.2",
"glob-promise": "^3.4.0",
"gm": "^1.23.1",
"iconv-lite": "^0.6.2",
"imap-simple": "^4.3.0",
"iso-639-1": "^2.1.3",
"jsonwebtoken": "^8.5.1",
@ -639,4 +640,4 @@
"json"
]
}
}
}