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