2021-02-15 23:09:10 -08:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2019-06-23 03:35:23 -07:00
|
|
|
import {
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeTypeDescription,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeOperationError,
|
2019-06-23 03:35:23 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { readFile as fsReadFile } from 'fs/promises';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
export class ReadBinaryFile implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Read Binary File',
|
|
|
|
name: 'readBinaryFile',
|
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 a binary file from disk',
|
|
|
|
defaults: {
|
|
|
|
name: 'Read Binary File',
|
2019-07-26 02:41:08 -07:00
|
|
|
color: '#449922',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'File Path',
|
|
|
|
name: 'filePath',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
required: true,
|
|
|
|
placeholder: '/data/example.jpg',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Path of the file 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 file',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
],
|
2019-06-23 03:35:23 -07:00
|
|
|
};
|
|
|
|
|
2021-03-29 02:20:10 -07:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
2021-02-15 23:09:10 -08:00
|
|
|
const items = this.getInputData();
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-02-15 23:09:10 -08:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2021-02-15 23:09:10 -08:00
|
|
|
let item: INodeExecutionData;
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-02-15 23:09:10 -08:00
|
|
|
for (let itemIndex = 0; itemIndex < length; itemIndex++) {
|
|
|
|
try {
|
2021-07-19 23:58:54 -07:00
|
|
|
item = items[itemIndex];
|
|
|
|
const dataPropertyName = this.getNodeParameter('dataPropertyName', itemIndex) as string;
|
|
|
|
const filePath = this.getNodeParameter('filePath', itemIndex) as string;
|
|
|
|
|
|
|
|
let data;
|
|
|
|
try {
|
2022-12-02 12:54:28 -08:00
|
|
|
data = await fsReadFile(filePath);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'ENOENT') {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`The file "${filePath}" could not be found.`,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
2021-02-15 23:09:10 -08:00
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const newItem: INodeExecutionData = {
|
|
|
|
json: item.json,
|
|
|
|
binary: {},
|
2022-06-03 08:25:07 -07:00
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
2021-07-19 23:58:54 -07:00
|
|
|
};
|
2019-08-01 13:55:33 -07:00
|
|
|
|
2022-10-05 04:36:09 -07:00
|
|
|
if (item.binary !== undefined && newItem.binary) {
|
2021-07-19 23:58:54 -07:00
|
|
|
// Create a shallow copy of the binary data so that the old
|
|
|
|
// data references which do not get changed still stay behind
|
|
|
|
// but the incoming data does not get changed.
|
|
|
|
Object.assign(newItem.binary, item.binary);
|
|
|
|
}
|
2019-08-01 13:55:33 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
newItem.binary![dataPropertyName] = await this.helpers.prepareBinaryData(data, filePath);
|
|
|
|
returnData.push(newItem);
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-06-03 08:25:07 -07:00
|
|
|
returnData.push({
|
|
|
|
json: {
|
|
|
|
error: error.message,
|
|
|
|
},
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-03-29 02:20:10 -07:00
|
|
|
}
|
|
|
|
|
2021-02-15 23:09:10 -08:00
|
|
|
return this.prepareOutputData(returnData);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
}
|