2019-06-23 03:35:23 -07:00
|
|
|
import {
|
|
|
|
BINARY_ENCODING,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
INodeTypeDescription,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
} from 'n8n-workflow';
|
2019-06-24 03:47:44 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
writeFile as fsWriteFile,
|
|
|
|
} from 'fs';
|
2019-09-19 05:14:37 -07:00
|
|
|
import { promisify } from 'util';
|
2019-06-24 03:47:44 -07:00
|
|
|
|
|
|
|
const fsWriteFileAsync = promisify(fsWriteFile);
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
|
|
|
|
export class WriteBinaryFile implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Write Binary File',
|
|
|
|
name: 'writeBinaryFile',
|
2019-07-26 02:27:46 -07:00
|
|
|
icon: 'fa:file-export',
|
2019-06-23 03:35:23 -07:00
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Writes a binary file to disk',
|
|
|
|
defaults: {
|
|
|
|
name: 'Write Binary File',
|
|
|
|
color: '#CC2233',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'File Name',
|
|
|
|
name: 'fileName',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
required: true,
|
|
|
|
placeholder: '/data/example.jpg',
|
|
|
|
description: 'Path to which the file should be written.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Property Name',
|
|
|
|
name: 'dataPropertyName',
|
|
|
|
type: 'string',
|
|
|
|
default: 'data',
|
|
|
|
required: true,
|
|
|
|
description: 'Name of the binary property which contains<br />the data for the file to be written.',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
async executeSingle(this: IExecuteSingleFunctions): Promise<INodeExecutionData> {
|
|
|
|
const item = this.getInputData();
|
|
|
|
|
|
|
|
const dataPropertyName = this.getNodeParameter('dataPropertyName') as string;
|
|
|
|
const fileName = this.getNodeParameter('fileName') as string;
|
|
|
|
|
|
|
|
if (item.binary === undefined) {
|
2019-12-19 15:58:55 -08:00
|
|
|
throw new Error('No binary data set. So file can not be written!');
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (item.binary[dataPropertyName] === undefined) {
|
2019-12-19 15:58:55 -08:00
|
|
|
throw new Error(`The binary property "${dataPropertyName}" does not exist. So no file can be written!`);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the file to disk
|
2019-06-24 03:47:44 -07:00
|
|
|
await fsWriteFileAsync(fileName, Buffer.from(item.binary[dataPropertyName].data, BINARY_ENCODING), 'binary');
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2019-08-01 13:55:33 -07:00
|
|
|
const newItem: INodeExecutionData = {
|
|
|
|
json: {},
|
|
|
|
};
|
|
|
|
Object.assign(newItem.json, item.json);
|
|
|
|
|
|
|
|
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.
|
|
|
|
newItem.binary = {};
|
|
|
|
Object.assign(newItem.binary, item.binary);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the file name to data
|
2019-08-01 13:55:33 -07:00
|
|
|
(newItem.json as IDataObject).fileName = fileName;
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2019-08-01 13:55:33 -07:00
|
|
|
return newItem;
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
}
|