2023-07-31 05:20:39 -07:00
|
|
|
import type { Readable } from 'stream';
|
|
|
|
|
|
|
|
import { BINARY_ENCODING } from 'n8n-workflow';
|
2023-03-06 08:33:32 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
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-08-17 08:50:24 -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
|
|
|
},
|
2022-10-24 02:41:43 -07:00
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add Option',
|
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Append',
|
|
|
|
name: 'append',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
|
|
|
description: 'Whether to append to an existing file',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
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[][]> {
|
|
|
|
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 {
|
2023-01-06 06:09:32 -08:00
|
|
|
const dataPropertyName = this.getNodeParameter('dataPropertyName', itemIndex);
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const fileName = this.getNodeParameter('fileName', itemIndex) as string;
|
2023-07-31 05:20:39 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
const options = this.getNodeParameter('options', 0, {});
|
2022-10-24 02:41:43 -07:00
|
|
|
|
2023-07-31 05:20:39 -07:00
|
|
|
const flag: string = options.append ? 'a' : 'w';
|
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
|
|
|
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
|
|
|
|
2023-03-06 08:33:32 -08:00
|
|
|
const binaryData = this.helpers.assertBinaryData(itemIndex, dataPropertyName);
|
|
|
|
|
2023-01-31 07:12:21 -08:00
|
|
|
let fileContent: Buffer | Readable;
|
2023-03-06 08:33:32 -08:00
|
|
|
if (binaryData.id) {
|
|
|
|
fileContent = this.helpers.getBinaryStream(binaryData.id);
|
2023-01-31 07:12:21 -08:00
|
|
|
} else {
|
2023-03-06 08:33:32 -08:00
|
|
|
fileContent = Buffer.from(binaryData.data, BINARY_ENCODING);
|
2023-01-31 07:12:21 -08:00
|
|
|
}
|
2021-08-20 09:08:40 -07:00
|
|
|
|
|
|
|
// Write the file to disk
|
2023-07-31 05:20:39 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
await this.helpers.writeContentToFile(fileName, fileContent, flag);
|
2021-08-20 09:08:40 -07:00
|
|
|
|
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
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
newItem.json.fileName = fileName;
|
2021-03-29 02:20:10 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push(newItem);
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-06-03 08:25:07 -07:00
|
|
|
returnData.push({
|
|
|
|
json: {
|
2023-07-31 05:20:39 -07:00
|
|
|
error: (error as Error).message,
|
2022-06-03 08:25:07 -07:00
|
|
|
},
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|