2019-06-23 03:35:23 -07:00
|
|
|
import { IExecuteSingleFunctions } from 'n8n-core';
|
|
|
|
import {
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeTypeDescription,
|
2019-06-23 03:35:23 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2019-06-24 03:47:44 -07:00
|
|
|
import {
|
|
|
|
readFile as fsReadFile,
|
|
|
|
} from 'fs';
|
2019-09-19 05:14:37 -07:00
|
|
|
import { promisify } from 'util';
|
2019-06-24 03:47:44 -07:00
|
|
|
|
|
|
|
const fsReadFileAsync = promisify(fsReadFile);
|
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',
|
|
|
|
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<br />write the data of the read file.',
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
],
|
2019-06-23 03:35:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
async executeSingle(this: IExecuteSingleFunctions): Promise<INodeExecutionData> {
|
|
|
|
const item = this.getInputData();
|
|
|
|
|
|
|
|
const dataPropertyName = this.getNodeParameter('dataPropertyName') as string;
|
|
|
|
const filePath = this.getNodeParameter('filePath') as string;
|
|
|
|
|
|
|
|
let data;
|
|
|
|
try {
|
2019-06-24 03:47:44 -07:00
|
|
|
data = await fsReadFileAsync(filePath) as Buffer;
|
2019-06-23 03:35:23 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'ENOENT') {
|
|
|
|
throw new Error(`The file "${filePath}" could not be found.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2019-08-01 13:55:33 -07:00
|
|
|
const newItem: INodeExecutionData = {
|
|
|
|
json: item.json,
|
|
|
|
binary: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (item.binary !== undefined) {
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
return newItem;
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|