n8n/packages/nodes-base/nodes/ReadBinaryFiles/ReadBinaryFiles.node.ts
Michael Kret 5e16dd4ab4
feat(core): Improvements/overhaul for nodes working with binary data (#7651)
Github issue / Community forum post (link here to close automatically):

---------

Co-authored-by: Giulio Andreini <andreini@netseven.it>
Co-authored-by: Marcus <marcus@n8n.io>
2024-01-03 13:08:16 +02:00

69 lines
1.7 KiB
TypeScript

import type {
IExecuteFunctions,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import glob from 'fast-glob';
import { generatePairedItemData } from '../../utils/utilities';
export class ReadBinaryFiles implements INodeType {
description: INodeTypeDescription = {
hidden: true,
displayName: 'Read Binary Files',
name: 'readBinaryFiles',
icon: 'fa:file-import',
group: ['input'],
version: 1,
description: 'Reads binary files from disk',
defaults: {
name: 'Read Binary Files',
color: '#44AA44',
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'File Selector',
name: 'fileSelector',
type: 'string',
default: '',
required: true,
placeholder: '*.jpg',
description: 'Pattern for files to read',
},
{
displayName: 'Property Name',
name: 'dataPropertyName',
type: 'string',
default: 'data',
required: true,
description: 'Name of the binary property to which to write the data of the read files',
},
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const fileSelector = this.getNodeParameter('fileSelector', 0) as string;
const dataPropertyName = this.getNodeParameter('dataPropertyName', 0);
const pairedItem = generatePairedItemData(this.getInputData().length);
const files = await glob(fileSelector);
const items: INodeExecutionData[] = [];
for (const filePath of files) {
const stream = await this.helpers.createReadStream(filePath);
items.push({
binary: {
[dataPropertyName]: await this.helpers.prepareBinaryData(stream, filePath),
},
json: {},
pairedItem,
});
}
return [items];
}
}