UNPKG

n8n-nodes-smartgent

Version:

SmartGent custom nodes for n8n - AI-powered automation and intelligent workflow integrations including LiteLLM chat completions, SharePoint file monitoring, and enterprise search

276 lines 11.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SmartgentMinio = void 0; const n8n_workflow_1 = require("n8n-workflow"); const client_s3_1 = require("@aws-sdk/client-s3"); const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner"); const client_s3_2 = require("@aws-sdk/client-s3"); class SmartgentMinio { constructor() { this.description = { displayName: 'SmartGent MinIO', name: 'smartgentMinio', icon: 'file:s3.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Upload files and generate presigned URLs for S3/MinIO', defaults: { name: 'SmartGent MinIO', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 's3MinIO', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Upload File', value: 'upload', description: 'Upload a file to S3/MinIO', action: 'Upload a file', }, { name: 'Generate Presigned URL', value: 'presignedUrl', description: 'Generate a presigned URL for a file', action: 'Generate presigned URL', }, { name: 'Upload and Get Presigned URL', value: 'uploadAndPresign', description: 'Upload a file and immediately generate a presigned URL', action: 'Upload file and get presigned URL', }, ], default: 'uploadAndPresign', }, { displayName: 'Bucket Name', name: 'bucketName', type: 'string', default: '', required: true, description: 'Name of the S3/MinIO bucket', }, { displayName: 'File Name', name: 'fileName', type: 'string', default: '', required: true, description: 'Name of the file in the bucket', displayOptions: { show: { operation: ['upload', 'uploadAndPresign'], }, }, }, { displayName: 'Object Key', name: 'objectKey', type: 'string', default: '', required: true, description: 'Key (path) of the object in the bucket', displayOptions: { show: { operation: ['presignedUrl'], }, }, }, { displayName: 'Binary File', name: 'binaryFile', type: 'boolean', default: true, description: 'Whether the file is binary data', displayOptions: { show: { operation: ['upload', 'uploadAndPresign'], }, }, }, { displayName: 'Input Binary Field', name: 'binaryPropertyName', type: 'string', default: 'data', required: true, description: 'Name of the input binary field containing the file to be uploaded', displayOptions: { show: { operation: ['upload', 'uploadAndPresign'], binaryFile: [true], }, }, }, { displayName: 'File Content', name: 'fileContent', type: 'string', default: '', description: 'Content of the file as text', displayOptions: { show: { operation: ['upload', 'uploadAndPresign'], binaryFile: [false], }, }, }, { displayName: 'Content Type', name: 'contentType', type: 'string', default: 'application/octet-stream', description: 'MIME type of the file', displayOptions: { show: { operation: ['upload', 'uploadAndPresign'], }, }, }, { displayName: 'Expiration Time (Seconds)', name: 'expirationTime', type: 'number', default: 3600, description: 'Time in seconds until the presigned URL expires', displayOptions: { show: { operation: ['presignedUrl', 'uploadAndPresign'], }, }, }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; for (let i = 0; i < items.length; i++) { try { const operation = this.getNodeParameter('operation', i); const bucketName = this.getNodeParameter('bucketName', i); const credentials = await this.getCredentials('s3MinIO'); const s3Config = { region: credentials.region, credentials: { accessKeyId: credentials.accessKeyId, secretAccessKey: credentials.secretAccessKey, }, }; if (credentials.endpointUrl) { s3Config.endpoint = credentials.endpointUrl; s3Config.forcePathStyle = credentials.forcePathStyle; } const s3Client = new client_s3_1.S3Client(s3Config); if (operation === 'upload' || operation === 'uploadAndPresign') { const fileName = this.getNodeParameter('fileName', i); const binaryFile = this.getNodeParameter('binaryFile', i); const contentType = this.getNodeParameter('contentType', i); let fileContent; let fileSize; if (binaryFile) { const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i); fileContent = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName); fileSize = fileContent.length; } else { const textContent = this.getNodeParameter('fileContent', i); fileContent = Buffer.from(textContent, 'utf8'); fileSize = fileContent.length; } const uploadCommand = new client_s3_1.PutObjectCommand({ Bucket: bucketName, Key: fileName, Body: fileContent, ContentType: contentType, ContentLength: fileSize, }); await s3Client.send(uploadCommand); const objectUrl = credentials.endpointUrl ? `${credentials.endpointUrl}/${bucketName}/${fileName}` : `https://${bucketName}.s3.${credentials.region}.amazonaws.com/${fileName}`; let result = { operation: 'upload', bucketName, fileName, fileSize, contentType, uploadedAt: new Date().toISOString(), url: objectUrl, }; if (operation === 'uploadAndPresign') { const expirationTime = this.getNodeParameter('expirationTime', i); const getObjectCommand = new client_s3_2.GetObjectCommand({ Bucket: bucketName, Key: fileName, }); const presignedUrl = await (0, s3_request_presigner_1.getSignedUrl)(s3Client, getObjectCommand, { expiresIn: expirationTime, }); result.presignedUrl = presignedUrl; result.expiresAt = new Date(Date.now() + expirationTime * 1000).toISOString(); result.operation = 'uploadAndPresign'; } returnData.push({ json: result, pairedItem: { item: i }, }); } else if (operation === 'presignedUrl') { const objectKey = this.getNodeParameter('objectKey', i); const expirationTime = this.getNodeParameter('expirationTime', i); const getObjectCommand = new client_s3_2.GetObjectCommand({ Bucket: bucketName, Key: objectKey, }); const presignedUrl = await (0, s3_request_presigner_1.getSignedUrl)(s3Client, getObjectCommand, { expiresIn: expirationTime, }); returnData.push({ json: { operation: 'presignedUrl', bucketName, objectKey, presignedUrl, expiresAt: new Date(Date.now() + expirationTime * 1000).toISOString(), generatedAt: new Date().toISOString(), }, pairedItem: { item: i }, }); } } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message, operation: this.getNodeParameter('operation', i), timestamp: new Date().toISOString(), }, pairedItem: { item: i }, }); } else { throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i, }); } } } return [returnData]; } } exports.SmartgentMinio = SmartgentMinio; //# sourceMappingURL=SmartgentMinio.node.js.map