n8n-nodes-cloudconvert
Version:
A Node to send file conversion jobs to cloudconvert.com
207 lines • 8.61 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CloudConvertTrigger = void 0;
const GenericFunctions_1 = require("./GenericFunctions");
const crypto = __importStar(require("crypto"));
class CloudConvertTrigger {
constructor() {
this.description = {
displayName: 'CloudConvert Trigger',
name: 'cloudConvertTrigger',
icon: 'file:cloudconvert-logo.png',
group: ['trigger'],
version: 1,
description: 'Process CloudConvert Job Webhooks',
defaults: {
name: 'CloudConvert Trigger',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'cloudConvertCredentialsApi',
required: true,
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: '<strong>INFO</strong>: this node requires an API key with the <strong><code>webhook.read</code></strong> and <strong><code>webhook.write</code></strong> scopes!',
name: 'notice_signature',
type: 'notice',
default: '',
displayOptions: {},
},
{
displayName: 'Events To Subscribe To',
name: 'events',
type: 'multiOptions',
required: true,
default: ['job.finished', 'job.failed'],
description: 'List of events to be notified for',
options: [
{
name: 'Job Created',
value: 'job.created',
},
{
name: 'Job Finished',
value: 'job.finished',
},
{
name: 'Job Failed',
value: 'job.failed',
},
],
},
{
displayName: 'Download?',
name: 'download',
type: 'boolean',
default: false,
description: 'Whether to download result files from a sync request and add them as binary attachments?',
displayOptions: {},
},
{
displayName: '<strong>NOTE</strong>: only <strong><code>export/url</code></strong> export tasks will be retrieved. The property name of the binary attachment will be the name of the export task in the job definition (e.g. <code>export-1</code>).',
name: 'notice_async',
type: 'notice',
default: '',
displayOptions: {
show: {
download: [true],
},
},
},
{
displayName: 'Verify Signature?',
name: 'verify',
type: 'boolean',
default: false,
description: 'Whether to validate the webhook signature',
displayOptions: {},
},
],
};
this.webhookMethods = {
default: {
async checkExists() {
const webhookData = this.getWorkflowStaticData('node');
const webhookUrl = this.getNodeWebhookUrl('default');
if (/^http(s)?:\/\/localhost.*/.test(webhookUrl)) {
return true;
}
const webhooks = await GenericFunctions_1.cloudConvertApiRequest.call(this, {
url: `/v2/users/me/webhooks`,
qs: {
'filter[url]': webhookUrl,
},
});
if (webhooks && webhooks.length) {
webhookData.webhookId = webhooks[0].id;
webhookData.webhookSigningSecret = webhooks[0].signing_secret;
return true;
}
return false;
},
async create() {
const webhookData = this.getWorkflowStaticData('node');
const webhookUrl = this.getNodeWebhookUrl('default');
const events = this.getNodeParameter('events');
if (/^http(s)?:\/\/localhost.*/.test(webhookUrl)) {
return true;
}
const response = await GenericFunctions_1.cloudConvertApiRequest.call(this, {
method: 'POST',
url: `/v2/webhooks`,
body: {
url: webhookUrl,
events,
},
});
if (response.id) {
webhookData.webhookId = response.id;
webhookData.webhookSigningSecret = response.signing_secret;
return true;
}
return false;
},
async delete() {
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId) {
try {
await GenericFunctions_1.cloudConvertApiRequest.call(this, {
method: 'DELETE',
url: `/v2/webhooks/${webhookData.webhookId}`,
});
}
catch (error) {
return false;
}
delete webhookData.webhookId;
delete webhookData.webhookSigningSecret;
}
return true;
},
},
};
this.methods = {};
}
async webhook() {
const webhookData = this.getWorkflowStaticData('node');
const req = this.getRequestObject();
const download = this.getNodeParameter('download', false);
const verify = this.getNodeParameter('verify', false);
const signKey = webhookData.webhookSigningSecret;
if (verify && signKey) {
const signature = req.header('CloudConvert-Signature');
const hmac = crypto.createHmac('sha256', signKey);
const hash = hmac.update(req.rawBody).digest('hex');
if (signature !== hash) {
return {};
}
}
const binaryItem = {
json: req.body,
binary: {},
};
if (download && req.body && req.body.job && 'job.finished' === req.body.event) {
await GenericFunctions_1.downloadExports.call(this, req.body.job, binaryItem);
}
return {
workflowData: [[binaryItem]],
};
}
}
exports.CloudConvertTrigger = CloudConvertTrigger;
//# sourceMappingURL=CloudConvertTrigger.node.js.map