From 9b3be0cfd8b0b58903d89ea3bf0b73be579a4f89 Mon Sep 17 00:00:00 2001 From: Michael Kret <88898367+michael-radency@users.noreply.github.com> Date: Mon, 13 Nov 2023 16:52:50 +0200 Subject: [PATCH] fix(Convert to/from binary data Node): Better mime type defaults (#7693) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Github issue / Community forum post (link here to close automatically): --------- Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ --- .../MoveBinaryData/MoveBinaryData.node.ts | 53 ++++++++++--------- .../test/MoveBinaryData.test.ts | 2 + 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/packages/nodes-base/nodes/MoveBinaryData/MoveBinaryData.node.ts b/packages/nodes-base/nodes/MoveBinaryData/MoveBinaryData.node.ts index 1467ba4d47..1081848431 100644 --- a/packages/nodes-base/nodes/MoveBinaryData/MoveBinaryData.node.ts +++ b/packages/nodes-base/nodes/MoveBinaryData/MoveBinaryData.node.ts @@ -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 { 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); diff --git a/packages/nodes-base/nodes/MoveBinaryData/test/MoveBinaryData.test.ts b/packages/nodes-base/nodes/MoveBinaryData/test/MoveBinaryData.test.ts index 411ca16cc3..698284f9be 100644 --- a/packages/nodes-base/nodes/MoveBinaryData/test/MoveBinaryData.test.ts +++ b/packages/nodes-base/nodes/MoveBinaryData/test/MoveBinaryData.test.ts @@ -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', }, }, },