mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-11 12:57:29 -08:00
feat(Write Binary File Node): Stream binary data for writes (#5306)
This commit is contained in:
parent
2b1f15150f
commit
d87ff130a4
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue