mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -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 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
|
||||||
|
|
Loading…
Reference in a new issue