n8n-nodes-base
Version:
Base nodes of n8n
143 lines • 5.65 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.description = exports.properties = void 0;
exports.execute = execute;
const fast_glob_1 = __importDefault(require("fast-glob"));
const n8n_workflow_1 = require("n8n-workflow");
const utilities_1 = require("../../../../utils/utilities");
const utils_1 = require("../helpers/utils");
exports.properties = [
{
displayName: 'File(s) Selector',
name: 'fileSelector',
type: 'string',
default: '',
required: true,
placeholder: 'e.g. /home/user/Pictures/**/*.png',
hint: 'Supports patterns, learn more <a href="https://github.com/micromatch/picomatch#basic-globbing" target="_blank">here</a>',
description: "Specify a file's path or path pattern to read multiple files. Always use forward-slashes for path separator even on Windows.",
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add option',
default: {},
options: [
{
displayName: 'File Extension',
name: 'fileExtension',
type: 'string',
default: '',
placeholder: 'e.g. zip',
description: 'Extension of the file in the output binary',
},
{
displayName: 'File Name',
name: 'fileName',
type: 'string',
default: '',
placeholder: 'e.g. data.zip',
description: 'Name of the file in the output binary',
},
{
displayName: 'Mime Type',
name: 'mimeType',
type: 'string',
default: '',
placeholder: 'e.g. application/zip',
description: 'Mime type of the file in the output binary',
},
{
displayName: 'Put Output File in Field',
name: 'dataPropertyName',
type: 'string',
default: 'data',
placeholder: 'e.g. data',
description: "By default 'data' is used",
hint: 'The name of the output binary field to put the file in',
},
],
},
];
const displayOptions = {
show: {
operation: ['read'],
},
};
exports.description = (0, utilities_1.updateDisplayOptions)(displayOptions, exports.properties);
async function execute(items) {
const nodeVersion = this.getNode().typeVersion;
const returnData = [];
let fileSelector;
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
try {
fileSelector = (0, utils_1.normalizeFileSelector)(this.getNodeParameter('fileSelector', itemIndex));
const options = this.getNodeParameter('options', itemIndex, {});
let dataPropertyName = 'data';
if (options.dataPropertyName) {
dataPropertyName = options.dataPropertyName;
}
const files = await (0, fast_glob_1.default)(fileSelector);
if (files.length === 0 && nodeVersion > 1) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No file(s) found', {
itemIndex,
description: `No file matching the selector "${fileSelector}" found`,
});
}
const newItems = [];
for (const filePath of files) {
const stream = await this.helpers.createReadStream(await this.helpers.resolvePath(filePath));
const binaryData = await this.helpers.prepareBinaryData(stream, filePath);
if (options.fileName !== undefined) {
binaryData.fileName = options.fileName;
}
if (options.fileExtension !== undefined) {
binaryData.fileExtension = options.fileExtension;
}
if (options.mimeType !== undefined) {
binaryData.mimeType = options.mimeType;
}
newItems.push({
binary: {
[dataPropertyName]: binaryData,
},
json: {
mimeType: binaryData.mimeType,
fileType: binaryData.fileType,
fileName: binaryData.fileName,
fileExtension: binaryData.fileExtension,
fileSize: binaryData.fileSize,
},
pairedItem: {
item: itemIndex,
},
});
}
returnData.push(...newItems);
}
catch (error) {
const nodeOperationError = utils_1.errorMapper.call(this, error, itemIndex, {
filePath: fileSelector,
operation: 'read',
});
if (this.continueOnFail()) {
returnData.push({
json: {
error: nodeOperationError.message,
},
pairedItem: {
item: itemIndex,
},
});
continue;
}
throw new n8n_workflow_1.NodeApiError(this.getNode(), error, { itemIndex });
}
}
return returnData;
}
//# sourceMappingURL=read.operation.js.map