UNPKG

n8n-nodes-placid

Version:

n8n node to interact with Placid API for creative generation

173 lines 8.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.execute = execute; const n8n_workflow_1 = require("n8n-workflow"); const get_operation_1 = require("./get.operation"); const config_1 = require("../../utils/config"); const multipartUtils_1 = require("../../utils/multipartUtils"); async function uploadBinaryFileToMedia(context, itemIndex, fieldName) { var _a, _b; const items = context.getInputData(); const item = items[itemIndex]; if (!item.binary) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'No binary data found in input'); } const binaryData = item.binary[fieldName]; if (!binaryData) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `No binary data found for field "${fieldName}"`); } const fileBuffer = await context.helpers.getBinaryDataBuffer(itemIndex, fieldName); let fileName = binaryData.fileName; if (!fileName) { const extension = ((_a = binaryData.mimeType) === null || _a === void 0 ? void 0 : _a.split('/')[1]) || 'pdf'; fileName = `file.${extension}`; } const filesToUpload = [{ key: 'file', buffer: fileBuffer, filename: fileName, contentType: binaryData.mimeType || 'application/pdf', }]; const { body, boundary } = (0, multipartUtils_1.buildMultipartBody)(filesToUpload); try { const response = await context.helpers.httpRequestWithAuthentication.call(context, 'placidApi', { method: 'POST', url: config_1.PlacidConfig.getRestUrl(config_1.PlacidConfig.ENDPOINTS.MEDIA), body, headers: { 'Content-Type': `multipart/form-data; boundary=${boundary}`, 'x-placid-integration': config_1.PlacidConfig.HTTP.HEADERS.PLACID_INTEGRATION, }, }); let responseData; try { responseData = typeof response === 'string' ? JSON.parse(response) : response; } catch (parseError) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Failed to parse upload response for "${fieldName}": ${parseError.message}`); } if (!responseData.media || !Array.isArray(responseData.media) || responseData.media.length === 0) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Failed to upload file "${fieldName}": Invalid response from Placid API`); } const uploadedMedia = responseData.media[0]; if (!uploadedMedia.file_id) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Failed to get URL for uploaded file "${fieldName}"`); } return uploadedMedia.file_id; } catch (error) { if (error instanceof n8n_workflow_1.NodeOperationError) { throw error; } if (error.response) { const statusCode = error.response.status; const errorMessage = ((_b = error.response.data) === null || _b === void 0 ? void 0 : _b.message) || error.response.data || error.message; throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Failed to upload file "${fieldName}": ${errorMessage} (${statusCode})`); } throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Failed to upload file "${fieldName}": ${error.message}`); } } async function execute(i) { const configurationMode = this.getNodeParameter('configurationMode', i, 'simple'); let urls = []; if (configurationMode === 'advanced') { const urlsJson = this.getNodeParameter('urlsJson', i); try { const parsedUrls = JSON.parse(urlsJson); if (!Array.isArray(parsedUrls)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'URLs JSON must be an array'); } urls = parsedUrls.filter((url) => typeof url === 'string' && url.trim().length > 0); } catch (error) { if (error instanceof n8n_workflow_1.NodeOperationError) { throw error; } throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Invalid JSON in URLs configuration: ${error.message}`); } if (urls.length < 2) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'At least 2 valid PDF URLs are required for merging'); } if (urls.length > 10) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Maximum 10 PDF URLs allowed for merging'); } } else { const pdfSourcesData = this.getNodeParameter('pdfSources', i); const sources = pdfSourcesData.sources || []; if (sources.length < 2) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'At least 2 PDF sources are required for merging'); } if (sources.length > 10) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Maximum 10 PDF sources allowed for merging'); } for (let sourceIndex = 0; sourceIndex < sources.length; sourceIndex++) { const source = sources[sourceIndex]; if (source.sourceType === 'binary') { if (!source.binaryField) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Binary field name is required for source ${sourceIndex + 1}`); } const url = await uploadBinaryFileToMedia(this, i, source.binaryField); urls.push(url); } else { const url = (source.url || '').trim(); if (!url) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `URL is required for source ${sourceIndex + 1}`); } urls.push(url); } } } const body = { urls, }; const additionalFields = this.getNodeParameter('additionalFields', i, {}); if (additionalFields.webhook_success) { body.webhook_success = additionalFields.webhook_success; } if (additionalFields.passthrough) { body.passthrough = additionalFields.passthrough; } const createOptions = { method: 'POST', url: config_1.PlacidConfig.getRestUrl(config_1.PlacidConfig.ENDPOINTS.PDFS_MERGE), headers: { 'Accept': config_1.PlacidConfig.HTTP.HEADERS.ACCEPT, 'Content-Type': config_1.PlacidConfig.HTTP.HEADERS.CONTENT_TYPE, 'x-placid-integration': config_1.PlacidConfig.HTTP.HEADERS.PLACID_INTEGRATION, }, body, }; const createResponse = await this.helpers.httpRequestWithAuthentication.call(this, 'placidApi', createOptions); if (createResponse.id) { const pdfId = createResponse.id; const pollingConfig = config_1.PlacidConfig.getPollingConfig('PDF'); const maxAttempts = pollingConfig.MAX_ATTEMPTS; const pollInterval = pollingConfig.INTERVAL_MS; let lastStatus = createResponse.status; for (let attempt = 0; attempt < maxAttempts; attempt++) { if (attempt > 0) { await (0, n8n_workflow_1.sleep)(pollInterval); } const pollResponse = await (0, get_operation_1.getPdfById)(this, pdfId); lastStatus = pollResponse.status; if (pollResponse.status === 'finished') { return { json: pollResponse, pairedItem: { item: i }, }; } else if (pollResponse.status === 'error') { const errorMsg = pollResponse.error || pollResponse.message || pollResponse.error_message || JSON.stringify(pollResponse); throw new n8n_workflow_1.NodeOperationError(this.getNode(), `PDF merge failed: ${errorMsg}`); } } throw new n8n_workflow_1.NodeOperationError(this.getNode(), `${pollingConfig.TIMEOUT_MESSAGE} after ${maxAttempts} attempts. Last status: ${lastStatus}`); } return { json: createResponse, pairedItem: { item: i }, }; } //# sourceMappingURL=merge.operation.js.map