2019-06-23 03:35:23 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2022-08-17 08:50:24 -07:00
|
|
|
import { INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow';
|
2022-04-08 14:32:08 -07:00
|
|
|
import glob from 'fast-glob';
|
|
|
|
import path from 'path';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { readFile as fsReadFile } from 'fs/promises';
|
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;
|
|
|
|
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) {
|
2022-08-17 08:50:24 -07:00
|
|
|
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: {},
|
2022-06-03 08:25:07 -07:00
|
|
|
pairedItem: {
|
|
|
|
item: 0,
|
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
items.push(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.prepareOutputData(items);
|
|
|
|
}
|
|
|
|
}
|