mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-13 16:14:07 -08:00
61e26804ba
* ⚡ enabled array-type * ⚡ await-thenable on * ⚡ ban-types on * ⚡ default-param-last on * ⚡ dot-notation on * ⚡ member-delimiter-style on * ⚡ no-duplicate-imports on * ⚡ no-empty-interface on * ⚡ no-floating-promises on * ⚡ no-for-in-array on * ⚡ no-invalid-void-type on * ⚡ no-loop-func on * ⚡ no-shadow on * ⚡ ban-ts-comment re enabled * ⚡ @typescript-eslint/lines-between-class-members on * address my own comment * @typescript-eslint/return-await on * @typescript-eslint/promise-function-async on * @typescript-eslint/no-unnecessary-boolean-literal-compare on * @typescript-eslint/no-unnecessary-type-assertion on * prefer-const on * @typescript-eslint/prefer-optional-chain on Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
109 lines
2.6 KiB
TypeScript
109 lines
2.6 KiB
TypeScript
import { IExecuteFunctions } from 'n8n-core';
|
|
import {
|
|
INodeExecutionData,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
NodeOperationError,
|
|
} from 'n8n-workflow';
|
|
|
|
import { readFile as fsReadFile } from 'fs/promises';
|
|
|
|
export class ReadBinaryFile implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'Read Binary File',
|
|
name: 'readBinaryFile',
|
|
icon: 'fa:file-import',
|
|
group: ['input'],
|
|
version: 1,
|
|
description: 'Reads a binary file from disk',
|
|
defaults: {
|
|
name: 'Read Binary File',
|
|
color: '#449922',
|
|
},
|
|
inputs: ['main'],
|
|
outputs: ['main'],
|
|
properties: [
|
|
{
|
|
displayName: 'File Path',
|
|
name: 'filePath',
|
|
type: 'string',
|
|
default: '',
|
|
required: true,
|
|
placeholder: '/data/example.jpg',
|
|
description: 'Path of the file to read',
|
|
},
|
|
{
|
|
displayName: 'Property Name',
|
|
name: 'dataPropertyName',
|
|
type: 'string',
|
|
default: 'data',
|
|
required: true,
|
|
description: 'Name of the binary property to which to write the data of the read file',
|
|
},
|
|
],
|
|
};
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
const items = this.getInputData();
|
|
|
|
const returnData: INodeExecutionData[] = [];
|
|
const length = items.length;
|
|
let item: INodeExecutionData;
|
|
|
|
for (let itemIndex = 0; itemIndex < length; itemIndex++) {
|
|
try {
|
|
item = items[itemIndex];
|
|
const dataPropertyName = this.getNodeParameter('dataPropertyName', itemIndex) as string;
|
|
const filePath = this.getNodeParameter('filePath', itemIndex) as string;
|
|
|
|
let data;
|
|
try {
|
|
data = await fsReadFile(filePath);
|
|
} catch (error) {
|
|
if (error.code === 'ENOENT') {
|
|
throw new NodeOperationError(
|
|
this.getNode(),
|
|
`The file "${filePath}" could not be found.`,
|
|
);
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
|
|
const newItem: INodeExecutionData = {
|
|
json: item.json,
|
|
binary: {},
|
|
pairedItem: {
|
|
item: itemIndex,
|
|
},
|
|
};
|
|
|
|
if (item.binary !== undefined && newItem.binary) {
|
|
// 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);
|
|
}
|
|
|
|
newItem.binary![dataPropertyName] = await this.helpers.prepareBinaryData(data, filePath);
|
|
returnData.push(newItem);
|
|
} catch (error) {
|
|
if (this.continueOnFail()) {
|
|
returnData.push({
|
|
json: {
|
|
error: error.message,
|
|
},
|
|
pairedItem: {
|
|
item: itemIndex,
|
|
},
|
|
});
|
|
continue;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
return this.prepareOutputData(returnData);
|
|
}
|
|
}
|