UNPKG

n8n-nodes-piapi

Version:

Community n8n nodes for PiAPI - integrate generative AI capabilities (image, video, audio, 3D) into your workflows

215 lines 8.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileUpload = void 0; class FileUpload { constructor() { this.description = { displayName: 'PiAPI File Upload', name: 'fileUpload', icon: 'file:../piapi.svg', group: ['transform'], version: 1, description: 'Upload temporary files that will be automatically deleted after 24 hours', defaults: { name: 'File Upload', }, inputs: ["main"], outputs: ["main"], credentials: [ { name: 'piAPIApi', required: true, }, ], properties: [ { displayName: 'Input Method', name: 'inputMethod', type: 'options', options: [ { name: 'Binary File', value: 'binaryFile', }, { name: 'URL', value: 'url', }, { name: 'Manual Input', value: 'manualInput', }, ], default: 'binaryFile', description: 'How to input the file to upload', }, { displayName: 'Binary Property', name: 'binaryPropertyName', type: 'string', default: 'data', required: true, displayOptions: { show: { inputMethod: ['binaryFile'], }, }, description: 'The binary property containing the file to upload', }, { displayName: 'File Name', name: 'fileName', type: 'string', default: '', required: true, displayOptions: { show: { inputMethod: ['manualInput'], }, }, description: 'Name of the file with extension (jpg, jpeg, png, webp, mp4, wav, mp3)', }, { displayName: 'File Data', name: 'fileData', type: 'string', typeOptions: { rows: 4, }, default: '', required: true, displayOptions: { show: { inputMethod: ['manualInput'], }, }, description: 'Base64 encoded file data', }, { displayName: 'File URL', name: 'fileUrl', type: 'string', default: '', required: true, displayOptions: { show: { inputMethod: ['url'], }, }, description: 'URL of the file to download and upload', }, ], }; } async execute() { var _a, _b, _c; const items = this.getInputData(); const returnData = []; const generateHashFilename = (extension) => { const randomHash = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); return `${randomHash}.${extension}`; }; const getMimeTypeExtension = (mimeType) => { const mimeTypeToExtension = { 'image/jpeg': 'jpg', 'image/png': 'png', 'image/webp': 'webp', 'video/mp4': 'mp4', 'audio/wav': 'wav', 'audio/mpeg': 'mp3', }; return mimeTypeToExtension[mimeType] || 'jpg'; }; for (let i = 0; i < items.length; i++) { try { const inputMethod = this.getNodeParameter('inputMethod', i); let fileName; let fileData; let extension = ''; if (inputMethod === 'binaryFile') { const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i); if (items[i].binary === undefined) { throw new Error('No binary data exists on item!'); } const binaryData = items[i].binary[binaryPropertyName]; if (binaryData === undefined) { throw new Error(`No binary data found for field "${binaryPropertyName}"!`); } if (binaryData.fileName) { extension = ((_a = binaryData.fileName.split('.').pop()) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || getMimeTypeExtension(binaryData.mimeType); } else { extension = getMimeTypeExtension(binaryData.mimeType); } fileName = generateHashFilename(extension); fileData = binaryData.data; } else if (inputMethod === 'url') { const fileUrl = this.getNodeParameter('fileUrl', i); extension = ((_b = fileUrl.split('.').pop()) === null || _b === void 0 ? void 0 : _b.toLowerCase()) || 'jpg'; if (extension.includes('?')) { extension = extension.split('?')[0]; } fileName = generateHashFilename(extension); const response = await this.helpers.request({ method: 'GET', url: fileUrl, encoding: null, resolveWithFullResponse: true, }); if (response.headers['content-type']) { const contentTypeExt = getMimeTypeExtension(response.headers['content-type']); if (contentTypeExt) { fileName = generateHashFilename(contentTypeExt); } } const buffer = Buffer.from(response.body); fileData = buffer.toString('base64'); } else { fileName = this.getNodeParameter('fileName', i); fileData = this.getNodeParameter('fileData', i); extension = ((_c = fileName.split('.').pop()) === null || _c === void 0 ? void 0 : _c.toLowerCase()) || ''; } const supportedExtensions = ['jpg', 'jpeg', 'png', 'webp', 'mp4', 'wav', 'mp3']; if (!extension || !supportedExtensions.includes(extension)) { throw new Error(`File extension "${extension}" is not supported. Supported extensions: ${supportedExtensions.join(', ')}`); } const params = { file_name: fileName, file_data: fileData, }; const credentials = await this.getCredentials('piAPIApi'); const response = await this.helpers.request({ method: 'POST', url: 'https://upload.theapi.app/api/ephemeral_resource', headers: { 'Content-Type': 'application/json', 'x-api-key': credentials.apiKey, }, body: params, json: true, }); returnData.push({ json: response, }); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message, }, }); continue; } throw error; } } return [returnData]; } } exports.FileUpload = FileUpload; //# sourceMappingURL=FileUpload.node.js.map