n8n-nodes-base
Version:
Base nodes of n8n
86 lines • 3.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReadBinaryFile = void 0;
const n8n_workflow_1 = require("n8n-workflow");
class ReadBinaryFile {
description = {
displayName: 'Read Binary File',
name: 'readBinaryFile',
icon: 'fa:file-import',
group: ['input'],
version: 1,
hidden: true,
description: 'Reads a binary file from disk',
defaults: {
name: 'Read Binary File',
color: '#449922',
},
inputs: [n8n_workflow_1.NodeConnectionTypes.Main],
outputs: [n8n_workflow_1.NodeConnectionTypes.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() {
const items = this.getInputData();
const returnData = [];
const length = items.length;
let item;
for (let itemIndex = 0; itemIndex < length; itemIndex++) {
try {
item = items[itemIndex];
const newItem = {
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(await this.helpers.resolvePath(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 [returnData];
}
}
exports.ReadBinaryFile = ReadBinaryFile;
//# sourceMappingURL=ReadBinaryFile.node.js.map