mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-11 12:57:29 -08:00
:add: Add iconv-lite for encoding/decoding for MoveBinaryData (#1473)
This commit is contained in:
parent
8699f174f7
commit
c3496845bf
|
@ -4,6 +4,10 @@ import {
|
||||||
unset,
|
unset,
|
||||||
} from 'lodash';
|
} from 'lodash';
|
||||||
|
|
||||||
|
import {
|
||||||
|
BINARY_ENCODING,
|
||||||
|
} from 'n8n-core';
|
||||||
|
|
||||||
import { IExecuteFunctions } from 'n8n-core';
|
import { IExecuteFunctions } from 'n8n-core';
|
||||||
import {
|
import {
|
||||||
IBinaryData,
|
IBinaryData,
|
||||||
|
@ -13,6 +17,18 @@ import {
|
||||||
INodeTypeDescription,
|
INodeTypeDescription,
|
||||||
} from 'n8n-workflow';
|
} 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 {
|
export class MoveBinaryData implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
|
@ -189,17 +205,47 @@ export class MoveBinaryData implements INodeType {
|
||||||
{
|
{
|
||||||
displayName: 'Encoding',
|
displayName: 'Encoding',
|
||||||
name: 'encoding',
|
name: 'encoding',
|
||||||
type: 'string',
|
type: 'options',
|
||||||
|
options: encodeDecodeOptions,
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
'/mode': [
|
'/mode': [
|
||||||
'binaryToJson',
|
'binaryToJson',
|
||||||
|
'jsonToBinary',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
default: 'utf8',
|
default: 'utf8',
|
||||||
description: 'Set the encoding of the data stream',
|
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',
|
displayName: 'File Name',
|
||||||
name: 'fileName',
|
name: 'fileName',
|
||||||
|
@ -336,19 +382,19 @@ export class MoveBinaryData implements INodeType {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const encoding = (options.encoding as BufferEncoding) || 'utf8';
|
const encoding = (options.encoding as string) || 'utf8';
|
||||||
let convertedValue = value.data;
|
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);
|
convertedValue = iconv.decode(Buffer.from(convertedValue, BINARY_ENCODING), encoding, {stripBOM: options.stripBOM as boolean});
|
||||||
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) {
|
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) {
|
if (options.jsonParse) {
|
||||||
|
@ -372,6 +418,7 @@ export class MoveBinaryData implements INodeType {
|
||||||
const convertAllData = this.getNodeParameter('convertAllData', itemIndex) as boolean;
|
const convertAllData = this.getNodeParameter('convertAllData', itemIndex) as boolean;
|
||||||
const destinationKey = this.getNodeParameter('destinationKey', itemIndex) as string;
|
const destinationKey = this.getNodeParameter('destinationKey', itemIndex) as string;
|
||||||
|
|
||||||
|
const encoding = (options.encoding as string) || 'utf8';
|
||||||
let value: IDataObject | string = item.json;
|
let value: IDataObject | string = item.json;
|
||||||
if (convertAllData === false) {
|
if (convertAllData === false) {
|
||||||
const sourceKey = this.getNodeParameter('sourceKey', itemIndex) as string;
|
const sourceKey = this.getNodeParameter('sourceKey', itemIndex) as string;
|
||||||
|
@ -396,7 +443,7 @@ export class MoveBinaryData implements INodeType {
|
||||||
value = JSON.stringify(value);
|
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 = {
|
const convertedValue: IBinaryData = {
|
||||||
|
|
|
@ -589,6 +589,7 @@
|
||||||
"get-system-fonts": "^2.0.2",
|
"get-system-fonts": "^2.0.2",
|
||||||
"glob-promise": "^3.4.0",
|
"glob-promise": "^3.4.0",
|
||||||
"gm": "^1.23.1",
|
"gm": "^1.23.1",
|
||||||
|
"iconv-lite": "^0.6.2",
|
||||||
"imap-simple": "^4.3.0",
|
"imap-simple": "^4.3.0",
|
||||||
"iso-639-1": "^2.1.3",
|
"iso-639-1": "^2.1.3",
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^8.5.1",
|
||||||
|
|
Loading…
Reference in a new issue