feat(Write Binary File Node): Stream binary data for writes (#5306)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2023-01-31 16:12:21 +01:00 committed by GitHub
parent 2b1f15150f
commit d87ff130a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,8 +1,10 @@
import type { IExecuteFunctions } from 'n8n-core';
import { BINARY_ENCODING } from 'n8n-core';
import type { INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import { writeFile as fsWriteFile } from 'fs/promises';
import type { Readable } from 'stream';
export class WriteBinaryFile implements INodeType {
description: INodeTypeDescription = {
@ -81,8 +83,8 @@ export class WriteBinaryFile implements INodeType {
{ itemIndex },
);
}
if (item.binary[dataPropertyName] === undefined) {
const itemBinaryData = item.binary[dataPropertyName];
if (itemBinaryData === undefined) {
throw new NodeOperationError(
this.getNode(),
`The binary property "${dataPropertyName}" does not exist. So no file can be written!`,
@ -98,13 +100,15 @@ export class WriteBinaryFile implements INodeType {
};
Object.assign(newItem.json, item.json);
const binaryDataBuffer = await this.helpers.getBinaryDataBuffer(
itemIndex,
dataPropertyName,
);
let fileContent: Buffer | Readable;
if (itemBinaryData.id) {
fileContent = this.helpers.getBinaryStream(itemBinaryData.id);
} else {
fileContent = Buffer.from(itemBinaryData.data, BINARY_ENCODING);
}
// Write the file to disk
await fsWriteFile(fileName, binaryDataBuffer, { encoding: 'binary', flag });
await fsWriteFile(fileName, fileContent, { encoding: 'binary', flag });
if (item.binary !== undefined) {
// Create a shallow copy of the binary data so that the old