UNPKG

@picsart/n8n-nodes-picsart-apis

Version:

n8n nodes to integrate Picsart API: remove backgrounds and enhance images with AI-powered tools.

188 lines 8.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PicsartEnhance = void 0; const n8n_workflow_1 = require("n8n-workflow"); class PicsartEnhance { constructor() { this.description = { displayName: 'Picsart Enhance', name: 'picsartEnhance', icon: 'file:../icons/image2vector.svg', group: ['transform'], version: 1, description: 'Node to enhance image with upscale factor', defaults: { name: 'Picsart Enhance', }, inputs: ["main"], outputs: ["main"], usableAsTool: true, credentials: [ { name: 'picsartApi', required: true, }, ], properties: [ { displayName: 'Image URL', name: 'image_url', type: 'string', default: '', placeholder: '{{$json["image_url"]}}', description: 'URL image for processing', }, { displayName: 'Upscale Factor', name: 'upscale_factor', type: 'options', default: '2', options: [ { name: '2x', value: '2' }, { name: '4x', value: '4' }, { name: '6x', value: '6' }, { name: '8x', value: '8' }, ], }, { displayName: 'Format', name: 'format', type: 'options', default: 'PNG', noDataExpression: true, options: [ { name: 'JPG', value: 'JPG' }, { name: 'PNG', value: 'PNG' }, { name: 'WEBP', value: 'WEBP' }, ], }, ], }; } async execute() { var _a, _b, _c; const items = this.getInputData(); const returnData = []; for (let itemIndex = 0; itemIndex < items.length; itemIndex++) { try { const credentials = await this.getCredentials('picsartApi'); const imageUrl = this.getNodeParameter('image_url', itemIndex); const apiKey = credentials.apiKey; const upscaleFactor = this.getNodeParameter('upscale_factor', itemIndex); const format = this.getNodeParameter('format', itemIndex); if (!apiKey) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'invalid token', { itemIndex }); } if (!imageUrl || imageUrl.length < 1 || imageUrl.length > 2083) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'image_url is required and must be 1..2083 chars', { itemIndex }); } try { new URL(imageUrl); } catch (_) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'image_url must be a valid URL', { itemIndex, }); } const allowedFormats = ['JPG', 'PNG', 'WEBP']; const normalizedFormat = (format || 'JPG').toUpperCase(); if (!allowedFormats.includes(normalizedFormat)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'format must be one of: JPG, PNG, WEBP', { itemIndex, }); } try { const urlObj = new URL(imageUrl); const pathname = urlObj.pathname || ''; const extRaw = pathname.split('.').pop() || ''; const ext = extRaw.toLowerCase(); const extMap = { jpg: 'JPG', jpeg: 'JPG', png: 'PNG', webp: 'WEBP', }; if (ext && !Object.keys(extMap).includes(ext)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'image_url must point to JPG, PNG, or WEBP', { itemIndex }); } const urlFormat = ext ? extMap[ext] : undefined; if (urlFormat && urlFormat !== normalizedFormat) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `format (${normalizedFormat}) must match image_url extension (${urlFormat})`, { itemIndex }); } } catch (_) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'image_url must point to JPG, PNG, or WEBP', { itemIndex }); } let balanceChecker = null; try { balanceChecker = await this.helpers.httpRequest({ method: 'GET', url: 'https://api.picsart.io/tools/1.0/balance', headers: { 'x-picsart-api-key': apiKey, accept: 'application/json', }, }); } catch (err) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'invalid token', { itemIndex }); } let result = null; console.log('normalizedFormat', normalizedFormat); console.log('upscaleFactor', upscaleFactor); const formData = new FormData(); formData.append('upscale_factor', upscaleFactor); formData.append('format', normalizedFormat); formData.append('image_url', imageUrl); let imageBuffer = null; console.log('formData', formData); try { result = await this.helpers.httpRequest({ method: 'POST', url: 'https://api.picsart.io/tools/1.0/upscale', headers: { 'x-picsart-api-key': apiKey, Accept: 'application/json', }, body: formData, }); imageBuffer = await this.helpers.httpRequest({ method: 'GET', url: (_a = result === null || result === void 0 ? void 0 : result.data) === null || _a === void 0 ? void 0 : _a.url, encoding: 'arraybuffer', }); } catch (err) { console.log('err', err.response); throw new n8n_workflow_1.NodeOperationError(this.getNode(), (_c = (_b = err.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.detail, { itemIndex }); } const credits = (balanceChecker === null || balanceChecker === void 0 ? void 0 : balanceChecker.data) || {}; returnData.push({ binary: { data: await this.helpers.prepareBinaryData(imageBuffer, 'result.png'), }, json: { imageUrl, result, credits: { balance: credits.balance || 0, credits: credits.credits || credits.balance || 0, ...credits, }, }, }); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: items[itemIndex].json, error, pairedItem: itemIndex }); } else { throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex }); } } } return [returnData]; } } exports.PicsartEnhance = PicsartEnhance; //# sourceMappingURL=PicsartEnhance.node.js.map