2024-08-29 06:55:53 -07:00
|
|
|
import {
|
|
|
|
NodeConnectionType,
|
|
|
|
type IExecuteFunctions,
|
|
|
|
type INodeExecutionData,
|
|
|
|
type INodeType,
|
|
|
|
type INodeTypeDescription,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-07-31 05:20:39 -07:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import glob from 'fast-glob';
|
2023-10-10 08:36:20 -07:00
|
|
|
import { generatePairedItemData } from '../../utils/utilities';
|
2019-06-24 03:47:44 -07:00
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
export class ReadBinaryFiles implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
2024-01-03 03:08:16 -08:00
|
|
|
hidden: true,
|
2019-06-23 03:35:23 -07:00
|
|
|
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
|
|
|
},
|
2024-08-29 06:55:53 -07:00
|
|
|
inputs: [NodeConnectionType.Main],
|
|
|
|
outputs: [NodeConnectionType.Main],
|
2019-06-23 03:35:23 -07:00
|
|
|
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);
|
2023-10-10 08:36:20 -07:00
|
|
|
const pairedItem = generatePairedItemData(this.getInputData().length);
|
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: {},
|
2023-10-10 08:36:20 -07:00
|
|
|
pairedItem,
|
2023-01-06 05:15:46 -08:00
|
|
|
});
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
2023-09-05 03:59:02 -07:00
|
|
|
return [items];
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
}
|