UNPKG

@traien/n8n-nodes-espocrm

Version:
139 lines 6.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AttachmentHandler = void 0; const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("../GenericFunctions"); class AttachmentHandler { async create(index) { const inputSource = this.getNodeParameter('inputSource', index, 'binaryField'); const role = this.getNodeParameter('role', index) || 'Attachment'; const relatedType = this.getNodeParameter('relatedType', index) || 'Document'; const field = this.getNodeParameter('field', index) || 'file'; const additionalFields = this.getNodeParameter('additionalFields', index, {}); const guessMimeByExt = (fileName) => { var _a; if (!fileName) return undefined; const ext = (_a = fileName.split('.').pop()) === null || _a === void 0 ? void 0 : _a.toLowerCase(); switch (ext) { case 'pdf': return 'application/pdf'; case 'txt': return 'text/plain'; case 'csv': return 'text/csv'; case 'json': return 'application/json'; case 'jpg': case 'jpeg': return 'image/jpeg'; case 'png': return 'image/png'; case 'gif': return 'image/gif'; case 'webp': return 'image/webp'; case 'svg': return 'image/svg+xml'; case 'doc': return 'application/msword'; case 'docx': return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; case 'xls': return 'application/vnd.ms-excel'; case 'xlsx': return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; case 'ppt': return 'application/vnd.ms-powerpoint'; case 'pptx': return 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; case 'zip': return 'application/zip'; case 'rar': return 'application/x-rar-compressed'; case 'tar': return 'application/x-tar'; case 'gz': return 'application/gzip'; case 'mp3': return 'audio/mpeg'; case 'mp4': return 'video/mp4'; case 'wav': return 'audio/wav'; case 'html': return 'text/html'; case 'xml': return 'application/xml'; default: return undefined; } }; let base64Data; let fileName; let mimeType; let fileSize; if (inputSource === 'base64Direct') { base64Data = this.getNodeParameter('base64Data', index); fileName = this.getNodeParameter('fileName', index); const providedMimeType = this.getNodeParameter('mimeType', index, ''); if (!base64Data || base64Data.trim() === '') { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Base64 data is required'); } if (!fileName || fileName.trim() === '') { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'File name is required'); } if (base64Data.includes(',')) { base64Data = base64Data.split(',').pop() || base64Data; } base64Data = base64Data.replace(/\s/g, ''); if (!/^[A-Za-z0-9+/]*={0,2}$/.test(base64Data)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid base64 data format'); } mimeType = providedMimeType || guessMimeByExt(fileName) || 'application/octet-stream'; fileSize = Math.floor((base64Data.length * 3) / 4); if (base64Data.endsWith('==')) { fileSize -= 2; } else if (base64Data.endsWith('=')) { fileSize -= 1; } } else { const binaryPropertyName = this.getNodeParameter('binaryPropertyName', index); const item = this.getInputData()[index]; if (!item.binary || !item.binary[binaryPropertyName]) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `No binary data property "${binaryPropertyName}" exists on item!`); } const binary = item.binary[binaryPropertyName]; const binaryDataBuffer = await this.helpers.getBinaryDataBuffer(index, binaryPropertyName); base64Data = binaryDataBuffer.toString('base64'); fileName = binary.fileName || additionalFields.name || 'file'; mimeType = binary.mimeType || additionalFields.type || guessMimeByExt(fileName) || 'application/octet-stream'; fileSize = typeof binary.fileSize !== 'undefined' ? Number(binary.fileSize) : binaryDataBuffer.length; } const dataUri = `data:${mimeType};base64,${base64Data}`; const body = { role, relatedType, field, name: fileName, type: mimeType, file: dataUri, ...additionalFields, }; if (!isNaN(fileSize)) { body.size = fileSize; } const response = await GenericFunctions_1.espoApiRequest.call(this, 'POST', '/Attachment', body); return response; } async get(index) { const id = this.getNodeParameter('attachmentId', index); const response = await GenericFunctions_1.espoApiRequest.call(this, 'GET', `/Attachment/${id}`); return response; } async update(index) { void this.getNodeParameter('attachmentId', index, ''); throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Update operation is not supported for Attachment'); } async delete(index) { const id = this.getNodeParameter('attachmentId', index); await GenericFunctions_1.espoApiRequest.call(this, 'DELETE', `/Attachment/${id}`); return { success: true, entityType: 'attachment', id }; } async getAll(index) { const returnAll = this.getNodeParameter('returnAll', index, false); const qs = {}; if (returnAll) { return await GenericFunctions_1.espoApiRequestAllItems.call(this, 'GET', '/Attachment', {}, qs); } else { qs.maxSize = this.getNodeParameter('limit', index, 50); const response = await GenericFunctions_1.espoApiRequest.call(this, 'GET', '/Attachment', {}, qs); return response.list; } } async download(index) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Attachment download is temporarily disabled'); } } exports.AttachmentHandler = AttachmentHandler; //# sourceMappingURL=AttachmentHandler.js.map