mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 22:54:05 -08:00
1acc7fea89
* refactor(core): Add more overloads for string-type node parameter (no-changelog) continued from https://github.com/n8n-io/n8n/pull/4797 * include binaryPropertyOutput
88 lines
2.3 KiB
TypeScript
88 lines
2.3 KiB
TypeScript
import type { IExecuteFunctions } from 'n8n-core';
|
|
import type { INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow';
|
|
|
|
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 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);
|
|
}
|
|
|
|
const filePath = this.getNodeParameter('filePath', itemIndex);
|
|
const stream = await this.helpers.createReadStream(filePath);
|
|
const dataPropertyName = this.getNodeParameter('dataPropertyName', itemIndex);
|
|
newItem.binary![dataPropertyName] = await this.helpers.prepareBinaryData(stream, 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);
|
|
}
|
|
}
|