n8n/packages/nodes-base/nodes/ReadBinaryFiles/ReadBinaryFiles.node.ts

71 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-06-23 03:35:23 -07:00
import { IExecuteFunctions } from 'n8n-core';
import { INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow';
import glob from 'fast-glob';
import path from 'path';
2019-06-23 03:35:23 -07:00
import { readFile as fsReadFile } from 'fs/promises';
2019-06-23 03:35:23 -07:00
export class ReadBinaryFiles implements INodeType {
description: INodeTypeDescription = {
displayName: 'Read Binary Files',
name: 'readBinaryFiles',
icon: 'fa:file-import',
2019-06-23 03:35:23 -07:00
group: ['input'],
version: 1,
description: 'Reads binary files from disk',
defaults: {
name: 'Read Binary Files',
color: '#44AA44',
2019-06-23 03:35:23 -07:00
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'File Selector',
name: 'fileSelector',
type: 'string',
default: '',
required: true,
placeholder: '*.jpg',
description: 'Pattern for files to read',
2019-06-23 03:35:23 -07:00
},
{
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',
2019-06-23 03:35:23 -07:00
},
2020-10-22 06:46:03 -07:00
],
2019-06-23 03:35:23 -07:00
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const fileSelector = this.getNodeParameter('fileSelector', 0) as string;
const dataPropertyName = this.getNodeParameter('dataPropertyName', 0) as string;
const files = await glob(fileSelector);
const items: INodeExecutionData[] = [];
let item: INodeExecutionData;
let data: Buffer;
for (const filePath of files) {
data = (await fsReadFile(filePath)) as Buffer;
2019-06-23 03:35:23 -07:00
item = {
binary: {
2021-02-15 04:25:30 -08:00
[dataPropertyName]: await this.helpers.prepareBinaryData(data, filePath),
2019-06-23 03:35:23 -07:00
},
json: {},
pairedItem: {
item: 0,
},
2019-06-23 03:35:23 -07:00
};
items.push(item);
}
return this.prepareOutputData(items);
}
}