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';
|
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,
|
2023-02-20 07:03:30 -08:00
|
|
|
hidden: true,
|
2019-06-23 03:35:23 -07:00
|
|
|
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
|
|
|
},
|
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 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 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
|
|
|
|
2023-01-06 06:09:32 -08:00
|
|
|
const filePath = this.getNodeParameter('filePath', itemIndex);
|
2023-07-31 05:20:39 -07:00
|
|
|
|
2023-01-06 05:15:46 -08:00
|
|
|
const stream = await this.helpers.createReadStream(filePath);
|
2023-01-06 06:09:32 -08:00
|
|
|
const dataPropertyName = this.getNodeParameter('dataPropertyName', itemIndex);
|
2023-08-01 06:32:33 -07:00
|
|
|
|
2023-01-06 05:15:46 -08:00
|
|
|
newItem.binary![dataPropertyName] = await this.helpers.prepareBinaryData(stream, filePath);
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push(newItem);
|
|
|
|
} catch (error) {
|
2024-08-30 00:59:30 -07:00
|
|
|
if (this.continueOnFail()) {
|
2022-06-03 08:25:07 -07:00
|
|
|
returnData.push({
|
|
|
|
json: {
|
2023-07-31 05:20:39 -07:00
|
|
|
error: (error as Error).message,
|
2022-06-03 08:25:07 -07:00
|
|
|
},
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-03-29 02:20:10 -07:00
|
|
|
}
|
|
|
|
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
}
|