Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions packages/nodes-base/nodes/WriteBinaryFile/WriteBinaryFile.node.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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!`,
Expand All @@ -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
Expand Down