2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2022-04-08 14:32:08 -07:00
|
|
|
import glob from 'fast-glob';
|
2019-06-24 03:47:44 -07:00
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
export class ReadBinaryFiles implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Read Binary Files',
|
|
|
|
name: 'readBinaryFiles',
|
2019-07-26 02:27:46 -07:00
|
|
|
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',
|
2019-07-26 02:41:08 -07:00
|
|
|
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',
|
2022-05-06 14:01:25 -07:00
|
|
|
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,
|
2022-05-06 14:01:25 -07:00
|
|
|
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;
|
2023-01-06 06:09:32 -08:00
|
|
|
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) {
|
2023-01-06 05:15:46 -08:00
|
|
|
const stream = await this.helpers.createReadStream(filePath);
|
|
|
|
items.push({
|
2019-06-23 03:35:23 -07:00
|
|
|
binary: {
|
2023-01-06 05:15:46 -08:00
|
|
|
[dataPropertyName]: await this.helpers.prepareBinaryData(stream, filePath),
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
json: {},
|
2022-06-03 08:25:07 -07:00
|
|
|
pairedItem: {
|
|
|
|
item: 0,
|
|
|
|
},
|
2023-01-06 05:15:46 -08:00
|
|
|
});
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.prepareOutputData(items);
|
|
|
|
}
|
|
|
|
}
|