fix(Convert to/from binary data Node): Better mime type defaults (#7693)

Github issue / Community forum post (link here to close automatically):

---------

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
Michael Kret 2023-11-13 16:52:50 +02:00 committed by GitHub
parent a262c450f7
commit 9b3be0cfd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 26 deletions

View file

@ -1,26 +1,19 @@
import get from 'lodash/get';
import set from 'lodash/set';
import unset from 'lodash/unset';
import prettyBytes from 'pretty-bytes';
import type {
IExecuteFunctions,
IBinaryData,
IDataObject,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
BINARY_ENCODING,
deepCopy,
jsonParse,
NodeOperationError,
fileTypeFromMimeType,
} from 'n8n-workflow';
import { BINARY_ENCODING, deepCopy, jsonParse, NodeOperationError } from 'n8n-workflow';
import iconv from 'iconv-lite';
iconv.encodingExists('utf8');
// Create options for bomAware and encoding
@ -53,7 +46,7 @@ export class MoveBinaryData implements INodeType {
name: 'moveBinaryData',
icon: 'fa:exchange-alt',
group: ['transform'],
version: 1,
version: [1, 1.1],
subtitle: '={{$parameter["mode"]==="binaryToJson" ? "Binary to JSON" : "JSON to Binary"}}',
description: 'Move data between binary and JSON properties',
defaults: {
@ -326,9 +319,7 @@ export class MoveBinaryData implements INodeType {
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const mode = this.getNodeParameter('mode', 0) as string;
const returnData: INodeExecutionData[] = [];
let item: INodeExecutionData;
@ -421,29 +412,39 @@ export class MoveBinaryData implements INodeType {
newItem.binary = {};
}
const mimeType = (options.mimeType as string) || 'application/json';
const convertedValue: IBinaryData = {
data: '',
mimeType,
fileType: fileTypeFromMimeType(mimeType),
};
const nodeVersion = this.getNode().typeVersion;
let mimeType = options.mimeType as string;
let data: Buffer;
if (options.dataIsBase64 !== true) {
if (options.useRawData !== true || typeof value === 'object') {
value = JSON.stringify(value);
if (!mimeType) {
mimeType = 'application/json';
}
}
convertedValue.fileSize = prettyBytes(value.length);
convertedValue.data = iconv
.encode(value, encoding, { addBOM: options.addBOM as boolean })
.toString(BINARY_ENCODING);
data = iconv.encode(value, encoding, { addBOM: options.addBOM as boolean });
} else {
convertedValue.data = value as unknown as string;
data = Buffer.from(value as unknown as string, BINARY_ENCODING);
}
if (options.fileName) {
convertedValue.fileName = options.fileName as string;
if (!mimeType && nodeVersion === 1) {
mimeType = 'application/json';
}
const convertedValue = await this.helpers.prepareBinaryData(
data,
options.fileName as string,
mimeType,
);
if (!convertedValue.fileName && nodeVersion > 1) {
const fileExtension = convertedValue.fileExtension
? `.${convertedValue.fileExtension}`
: '';
convertedValue.fileName = `file${fileExtension}`;
}
set(newItem.binary, destinationKey, convertedValue);

View file

@ -84,6 +84,7 @@ describe('Test Move Binary Data Node', () => {
mimeType: 'application/json',
fileType: 'json',
fileSize: '59 B',
fileExtension: 'json',
},
},
},
@ -103,6 +104,7 @@ describe('Test Move Binary Data Node', () => {
fileType: 'json',
fileSize: '10 B',
fileName: 'example.json',
fileExtension: 'json',
},
},
},