n8n-nodes-piapi
Version:
Community n8n nodes for PiAPI - integrate generative AI capabilities (image, video, audio, 3D) into your workflows
188 lines • 7.98 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.RemoveBackground = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const GenericFunctions_1 = require("../shared/GenericFunctions");
class RemoveBackground {
constructor() {
this.description = {
displayName: 'PiAPI Remove Background',
name: 'removeBackground',
icon: 'file:../piapi.svg',
group: ['transform'],
version: 1,
description: 'Remove the background from images using PiAPI',
defaults: {
name: 'Remove Background',
},
inputs: ["main"],
outputs: ["main"],
credentials: [
{
name: 'piAPIApi',
required: true,
},
],
properties: [
{
displayName: 'Image Input Method',
name: 'imageInputMethod',
type: 'options',
options: [
{
name: 'URL',
value: 'url',
},
{
name: 'Binary Data',
value: 'binaryData',
},
],
default: 'url',
description: 'Method to input the image data',
},
{
displayName: 'Image Binary Property',
name: 'imageBinaryPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
imageInputMethod: ['binaryData'],
},
},
description: 'Name of the binary property containing the image data',
},
{
displayName: 'Image URL',
name: 'imageUrl',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
imageInputMethod: ['url'],
},
},
description: 'URL of the image to process',
},
{
displayName: 'Image Requirements',
name: 'imageRequirements',
type: 'notice',
default: 'The image should be in a common format (JPEG, PNG) and must not exceed 10MB in size.',
},
{
displayName: 'Wait For Completion',
name: 'waitForCompletion',
type: 'boolean',
default: false,
description: 'Whether to wait for the background removal process to complete before continuing',
},
{
displayName: 'Max Retries',
name: 'maxRetries',
type: 'number',
default: 20,
description: 'Maximum number of retries to check task status',
displayOptions: {
show: {
waitForCompletion: [true],
},
},
},
{
displayName: 'Retry Interval',
name: 'retryInterval',
type: 'number',
default: 3000,
description: 'Interval between retries in milliseconds',
displayOptions: {
show: {
waitForCompletion: [true],
},
},
},
],
};
}
async execute() {
var _a, _b;
const items = this.getInputData();
const returnData = [];
for (let i = 0; i < items.length; i++) {
try {
const imageInputMethod = this.getNodeParameter('imageInputMethod', i);
const waitForCompletion = this.getNodeParameter('waitForCompletion', i, false);
let imageData;
if (imageInputMethod === 'url') {
imageData = this.getNodeParameter('imageUrl', i);
}
else {
const imageBinaryPropertyName = this.getNodeParameter('imageBinaryPropertyName', i);
const imageBinaryData = this.helpers.assertBinaryData(i, imageBinaryPropertyName);
if (imageBinaryData.mimeType && !imageBinaryData.mimeType.includes('image')) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'The provided binary data is not an image', { itemIndex: i });
}
const base64String = Buffer.from(await this.helpers.getBinaryDataBuffer(i, imageBinaryPropertyName)).toString('base64');
imageData = `data:${imageBinaryData.mimeType};base64,${base64String}`;
}
const requestBody = {
model: 'Qubico/image-toolkit',
task_type: 'background-remove',
input: {
image: imageData,
},
};
const response = await GenericFunctions_1.piApiRequest.call(this, 'POST', '/api/v1/task', requestBody);
const taskId = (_a = response.data) === null || _a === void 0 ? void 0 : _a.task_id;
if (!taskId) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Failed to get a valid task ID from the API');
}
let executionData;
if (waitForCompletion) {
const maxRetries = this.getNodeParameter('maxRetries', i, 20);
const retryInterval = this.getNodeParameter('retryInterval', i, 3000);
executionData = await GenericFunctions_1.waitForTaskCompletion.call(this, taskId, maxRetries, retryInterval);
}
else {
executionData = {
task_id: taskId,
status: ((_b = response.data) === null || _b === void 0 ? void 0 : _b.status) || 'pending',
};
}
returnData.push({
json: executionData,
});
}
catch (error) {
if (error.message && error.message.includes('failed to get valid image')) {
const errorMessage = 'The API could not process the provided image. Please ensure the image is accessible, in a common format (JPEG, PNG), and not too large (under 10MB).';
if (this.continueOnFail()) {
returnData.push({
json: {
error: errorMessage,
details: error.message,
},
});
continue;
}
throw new n8n_workflow_1.NodeOperationError(this.getNode(), errorMessage);
}
if (this.continueOnFail()) {
returnData.push({
json: {
error: error.message,
},
});
continue;
}
throw error;
}
}
return [returnData];
}
}
exports.RemoveBackground = RemoveBackground;
//# sourceMappingURL=RemoveBackground.node.js.map
;