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

67 lines
1.6 KiB
TypeScript
Raw Normal View History

import type {
IExecuteFunctions,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import glob from 'fast-glob';
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);
2019-06-23 03:35:23 -07:00
const files = await glob(fileSelector);
const items: INodeExecutionData[] = [];
for (const filePath of files) {
const stream = await this.helpers.createReadStream(filePath);
items.push({
2019-06-23 03:35:23 -07:00
binary: {
[dataPropertyName]: await this.helpers.prepareBinaryData(stream, filePath),
2019-06-23 03:35:23 -07:00
},
json: {},
pairedItem: {
item: 0,
},
});
2019-06-23 03:35:23 -07:00
}
return this.prepareOutputData(items);
}
}