UNPKG

n8n-nodes-placid

Version:

n8n node to interact with Placid API for creative generation

101 lines 4.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.execute = execute; const n8n_workflow_1 = require("n8n-workflow"); const config_1 = require("../../utils/config"); const multipartUtils_1 = require("../../utils/multipartUtils"); async function execute(index) { var _a, _b; const files = this.getNodeParameter('files.fileItems', index, []); const additionalFields = this.getNodeParameter('additionalFields', index, {}); if (!files || files.length === 0) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'At least one file must be specified'); } if (files.length > 5) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Maximum of 5 files can be uploaded at once'); } const items = this.getInputData(); const item = items[index]; if (!item.binary) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No binary data found in input'); } const filesToUpload = []; for (let i = 0; i < files.length; i++) { const fileConfig = files[i]; const fieldName = fileConfig.file; if (!fieldName) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `File field name is required for file ${i + 1}`); } const binaryData = item.binary[fieldName]; if (!binaryData) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `No binary data found for field "${fieldName}"`); } const fileKey = fileConfig.fileKey || `file${i === 0 ? '' : i + 1}`; const fileBuffer = await this.helpers.getBinaryDataBuffer(index, fieldName); let fileName = fileConfig.fileName; if (!fileName && binaryData.fileName) { fileName = binaryData.fileName; } if (!fileName) { const extension = ((_a = binaryData.mimeType) === null || _a === void 0 ? void 0 : _a.split('/')[1]) || 'bin'; fileName = `file${i + 1}.${extension}`; } filesToUpload.push({ key: fileKey, buffer: fileBuffer, filename: fileName, contentType: binaryData.mimeType || 'application/octet-stream', }); } const { body, boundary } = (0, multipartUtils_1.buildMultipartBody)(filesToUpload); try { const response = await this.helpers.httpRequestWithAuthentication.call(this, '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 (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to parse API response: ${error.message}`); } if (!responseData.media || !Array.isArray(responseData.media)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid response format from Placid API'); } if (additionalFields.returnFullResponse) { return { json: responseData, pairedItem: { item: index }, }; } else { const mediaUrls = responseData.media.map((media) => ({ file_key: media.file_key, file_id: media.file_id, })); return { json: { success: true, uploaded_files: mediaUrls.length, media: mediaUrls, }, pairedItem: { item: index }, }; } } catch (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(this.getNode(), `Placid API error (${statusCode}): ${errorMessage}`); } throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Upload failed: ${error.message}`); } } //# sourceMappingURL=uploadMedia.operation.js.map