n8n-nodes-piapi
Version:
Community n8n nodes for PiAPI - integrate generative AI capabilities (image, video, audio, 3D) into your workflows
175 lines • 6.89 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.KlingEffects = void 0;
const GenericFunctions_1 = require("../shared/GenericFunctions");
class KlingEffects {
constructor() {
this.description = {
displayName: 'PiAPI Kling Effects',
name: 'klingEffects',
icon: 'file:../piapi.svg',
group: ['transform'],
version: 1,
description: 'Generate videos with special effects using PiAPI Kling',
defaults: {
name: 'Kling Effects',
},
inputs: ["main"],
outputs: ["main"],
credentials: [
{
name: 'piAPIApi',
required: true,
},
],
properties: [
{
displayName: 'Image Source',
name: 'imageSource',
type: 'options',
options: [
{
name: 'URL',
value: 'url',
description: 'Load image from URL',
},
{
name: 'Binary Data',
value: 'binaryData',
description: 'Use image from binary field',
},
],
default: 'url',
description: 'Where to get the image from',
},
{
displayName: 'Image URL',
name: 'imageUrl',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
imageSource: ['url'],
},
},
description: 'URL of the image to apply effects to',
},
{
displayName: 'Binary Property',
name: 'binaryPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
imageSource: ['binaryData'],
},
},
description: 'Name of the binary property containing the image',
},
{
displayName: 'Effect',
name: 'effect',
type: 'options',
options: [
{
name: 'Squish',
value: 'squish',
description: 'Apply a squish effect to generate video',
},
{
name: 'Expansion',
value: 'expansion',
description: 'Apply an expansion effect to generate video',
},
],
default: 'squish',
description: 'Type of effect to apply',
},
{
displayName: 'Wait for Completion',
name: 'waitForCompletion',
type: 'boolean',
default: false,
description: 'Whether to wait for the task to complete before returning',
},
],
};
}
async execute() {
var _a;
const items = this.getInputData();
const returnData = [];
for (let i = 0; i < items.length; i++) {
try {
const imageSource = this.getNodeParameter('imageSource', i);
const effect = this.getNodeParameter('effect', i);
const waitForCompletion = this.getNodeParameter('waitForCompletion', i, false);
let imageUrl = '';
if (imageSource === 'url') {
imageUrl = this.getNodeParameter('imageUrl', i);
try {
new URL(imageUrl);
}
catch (error) {
throw new Error(`Invalid image URL: ${error.message}`);
}
}
else if (imageSource === 'binaryData') {
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
const binaryItem = (_a = items[i].binary) === null || _a === void 0 ? void 0 : _a[binaryPropertyName];
if (!binaryItem) {
throw new Error(`No binary data found in property ${binaryPropertyName}`);
}
const binaryData = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
if (!binaryData) {
throw new Error(`No binary data found in property ${binaryPropertyName}`);
}
const binaryMimeType = binaryItem.mimeType || 'image/png';
imageUrl = `data:${binaryMimeType};base64,${binaryData.toString('base64')}`;
}
const body = {
model: 'kling',
task_type: 'effects',
input: {
image_url: imageUrl,
effect,
},
config: {
webhook_config: {
endpoint: '',
secret: '',
},
},
};
const response = await GenericFunctions_1.piApiRequest.call(this, 'POST', '/api/v1/task', body);
if (response.code !== 200) {
throw new Error(`API Error: ${response.message}`);
}
const taskId = response.data.task_id;
let taskData = response.data;
if (waitForCompletion) {
taskData = await GenericFunctions_1.waitForTaskCompletion.call(this, taskId);
}
returnData.push({
json: taskData,
});
}
catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: {
error: error.message,
},
});
continue;
}
throw error;
}
}
return [returnData];
}
}
exports.KlingEffects = KlingEffects;
//# sourceMappingURL=KlingEffects.node.js.map
;