Skip to content

Commit

Permalink
fix(Convert to/from binary data Node): Better mime type defaults (n8n…
Browse files Browse the repository at this point in the history
…-io#7693)

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

---------

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
  • Loading branch information
michael-radency and netroy authored Nov 13, 2023
1 parent a262c45 commit 9b3be0c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
53 changes: 27 additions & 26 deletions packages/nodes-base/nodes/MoveBinaryData/MoveBinaryData.node.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

convertedValue.fileSize = prettyBytes(value.length);
if (!mimeType) {
mimeType = 'application/json';
}
}

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 (!mimeType && nodeVersion === 1) {
mimeType = 'application/json';
}

if (options.fileName) {
convertedValue.fileName = options.fileName as string;
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe('Test Move Binary Data Node', () => {
mimeType: 'application/json',
fileType: 'json',
fileSize: '59 B',
fileExtension: 'json',
},
},
},
Expand All @@ -103,6 +104,7 @@ describe('Test Move Binary Data Node', () => {
fileType: 'json',
fileSize: '10 B',
fileName: 'example.json',
fileExtension: 'json',
},
},
},
Expand Down

0 comments on commit 9b3be0c

Please sign in to comment.