2019-06-23 03:35:23 -07:00
|
|
|
import {
|
2021-02-16 01:42:46 -08:00
|
|
|
IExecuteFunctions,
|
2019-06-23 03:35:23 -07:00
|
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeTypeDescription,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeOperationError,
|
2019-06-23 03:35:23 -07:00
|
|
|
} from 'n8n-workflow';
|
2019-06-24 03:47:44 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
writeFile as fsWriteFile,
|
2021-04-30 19:22:15 -07:00
|
|
|
} from 'fs/promises';
|
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',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Path to which the file should be written',
|
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 which contains the data for the file to be written',
|
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-02-16 01:42:46 -08:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
2021-03-29 02:20:10 -07:00
|
|
|
|
2021-02-16 01:42:46 -08:00
|
|
|
const items = this.getInputData();
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-02-16 01:42:46 -08:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2021-02-16 01:42:46 -08:00
|
|
|
let item: INodeExecutionData;
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-02-16 01:42:46 -08:00
|
|
|
for (let itemIndex = 0; itemIndex < length; itemIndex++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
const dataPropertyName = this.getNodeParameter('dataPropertyName', itemIndex) as string;
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const fileName = this.getNodeParameter('fileName', itemIndex) as string;
|
2021-03-29 02:20:10 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
item = items[itemIndex];
|
2021-03-29 02:20:10 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (item.binary === undefined) {
|
2022-07-12 08:51:01 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No binary data set. So file can not be written!', { itemIndex });
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (item.binary[dataPropertyName] === undefined) {
|
2022-07-12 08:51:01 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), `The binary property "${dataPropertyName}" does not exist. So no file can be written!`, { itemIndex });
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const newItem: INodeExecutionData = {
|
|
|
|
json: {},
|
2022-06-03 08:25:07 -07:00
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
2021-07-19 23:58:54 -07:00
|
|
|
};
|
|
|
|
Object.assign(newItem.json, item.json);
|
2021-02-16 01:42:46 -08:00
|
|
|
|
2021-08-20 09:08:40 -07:00
|
|
|
const binaryDataBuffer = await this.helpers.getBinaryDataBuffer(itemIndex, dataPropertyName);
|
|
|
|
|
|
|
|
// Write the file to disk
|
|
|
|
await fsWriteFile(fileName, binaryDataBuffer, 'binary');
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
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
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// Add the file name to data
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
(newItem.json as IDataObject).fileName = fileName;
|
2021-03-29 02:20:10 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push(newItem);
|
2021-02-16 01:42:46 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-06-03 08:25:07 -07:00
|
|
|
returnData.push({
|
|
|
|
json: {
|
|
|
|
error: error.message,
|
|
|
|
},
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-02-16 01:42:46 -08:00
|
|
|
}
|
|
|
|
return this.prepareOutputData(returnData);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
2021-02-16 01:42:46 -08:00
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|