UNPKG

n8n-nodes-mallabe-images

Version:

n8n community node to alter images using Mallabe Images

309 lines 15.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MallabeImages = void 0; const GenericFunctions_1 = require("./GenericFunctions"); const ResizeOperation_1 = require("./ResizeOperation"); const CompressOperation_1 = require("./CompressOperation"); const MetadataOperation_1 = require("./MetadataOperation"); const CropOperation_1 = require("./CropOperation"); const FlipOperation_1 = require("./FlipOperation"); const RotateOperation_1 = require("./RotateOperation"); const BlurOperation_1 = require("./BlurOperation"); const GreyscaleOperation_1 = require("./GreyscaleOperation"); const JoinOperation_1 = require("./JoinOperation"); class MallabeImages { constructor() { this.description = { displayName: 'Mallabe Images', name: 'mallabeImages', icon: 'file:mallabe.svg', group: ['transform'], version: 1, subtitle: '={{ $parameter["operation"] + ": " + $parameter["resource"] }}', description: 'Mallabe Images is an automation toolchain that allows you to process images, resize, crop and apply other manipulations on images on the fly.', defaults: { name: 'Mallabe Images', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'mallabeImagesApi', required: true, }, ], properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Image', value: 'image', }, ], default: 'image', required: true, }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['image'], }, }, options: [ { name: 'Blur Image', value: 'blur', description: 'Blur images and send the new image to another integration', action: 'Blur image', }, { name: 'Compress Image', value: 'compress', description: "Compress images and drive the new image to another integration.Note: if a given URL isn't an image file (png, jpg/jpeg, etc..) - the file will not convert. Note: some Zapier integrations give URLs that are protected - these can't be used & compressed.", action: 'Compress image', }, { name: 'Crop Image', value: 'crop', description: "Crop images and send the new image to another integration. Note: If a given URL isn't an image file (e.g., png, jpg/jpeg, gif), the file will not convert. Also, some Zapier integrations provide protected URLs that cannot be cropped.", action: 'Crop image', }, { name: 'Extract Image Metadata', value: 'metadata', description: "Extract image metadata. Note: some integrations give URLs that are protected - these can't be used & resized.", action: 'Extract image metadata', }, { name: 'Flip Image', value: 'flip', description: 'Flip images and send the new image to another integration', action: 'Flip image', }, { name: 'Greyscale Image', value: 'greyscale', description: 'Greyscale images and send the new image to another integration', action: 'Greyscale image', }, { name: 'Join/Combine Images', value: 'join', description: 'Join images into one image and send the new image to another integration', action: 'Join combine images', }, { name: 'Resize Image', value: 'resize', description: "Resize or Scale images and drive the new image to another integration. Note: if a given URL isn't an image file (png, jpg/jpeg, gif, etc..) - the file will not convert.Note: some Zapier integrations give URLs that are protected - these can't be used & resized.", action: 'Resize image', }, { name: 'Rotate Image', value: 'rotate', description: 'Rotate images and send the new image to another integration', action: 'Rotate image', }, ], default: 'resize', }, ...ResizeOperation_1.resizeFields, ...CompressOperation_1.compressFields, ...MetadataOperation_1.metadataFields, ...CropOperation_1.cropFields, ...FlipOperation_1.flipFields, ...RotateOperation_1.rotateFields, ...BlurOperation_1.blurFields, ...GreyscaleOperation_1.greyscaleFields, ...JoinOperation_1.joinFields, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const length = items.length; const qs = {}; let response; let responseData; let download; for (let i = 0; i < length; i++) { try { const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); if (resource === 'image') { if (operation === 'resize') { const url = this.getNodeParameter('url', i); const strategy = this.getNodeParameter('strategy', i); const width = this.getNodeParameter('width', i); const height = this.getNodeParameter('height', i); const removeExif = this.getNodeParameter('removeExif', i); const fileName = this.getNodeParameter('fileName', i); const fileExtension = this.getNodeParameter('fileExtension', i); const webhookUrl = this.getNodeParameter('webhookUrl', i); const body = { url, strategy, width, height, removeExif, fileName, fileExtension, webhookUrl, }; response = await GenericFunctions_1.mallabeImagesRequest.call(this, 'POST', '/v1/images/resize', body, qs); } else if (operation === 'compress') { const url = this.getNodeParameter('url', i); const quality = this.getNodeParameter('quality', i); const fileName = this.getNodeParameter('fileName', i); const fileExtension = this.getNodeParameter('fileExtension', i); const webhookUrl = this.getNodeParameter('webhookUrl', i); const body = { url, quality, fileName, fileExtension, webhookUrl, }; response = await GenericFunctions_1.mallabeImagesRequest.call(this, 'POST', '/v1/images/compress', body, qs); } else if (operation === 'metadata') { const url = this.getNodeParameter('url', i); const webhookUrl = this.getNodeParameter('webhookUrl', i); const body = { url, webhookUrl, }; response = await GenericFunctions_1.mallabeImagesRequest.call(this, 'POST', '/v1/images/metadata', body, qs); } else if (operation === 'crop') { const url = this.getNodeParameter('url', i); const gravity = this.getNodeParameter('gravity', i); const width = this.getNodeParameter('width', i); const height = this.getNodeParameter('height', i); const x = gravity === 10 ? this.getNodeParameter('x', i) : null; const y = gravity === 10 ? this.getNodeParameter('x', i) : null; const webhookUrl = this.getNodeParameter('webhookUrl', i); const body = { url, gravity, width, height, x, y, webhookUrl, }; response = await GenericFunctions_1.mallabeImagesRequest.call(this, 'POST', '/v1/images/crop', body, qs); } else if (operation === 'flip') { const url = this.getNodeParameter('url', i); const vertical = this.getNodeParameter('vertical', i); const horizontal = this.getNodeParameter('horizontal', i); const webhookUrl = this.getNodeParameter('webhookUrl', i); const body = { url, vertical, horizontal, webhookUrl, }; response = await GenericFunctions_1.mallabeImagesRequest.call(this, 'POST', '/v1/images/flip', body, qs); } else if (operation === 'rotate') { const url = this.getNodeParameter('url', i); const angle = this.getNodeParameter('angle', i); const backgroundColor = this.getNodeParameter('backgroundColor', i); const webhookUrl = this.getNodeParameter('webhookUrl', i); const body = { url, angle, backgroundColor, webhookUrl, }; response = await GenericFunctions_1.mallabeImagesRequest.call(this, 'POST', '/v1/images/rotate', body, qs); } else if (operation === 'blur') { const url = this.getNodeParameter('url', i); const value = this.getNodeParameter('value', i); const webhookUrl = this.getNodeParameter('webhookUrl', i); const body = { url, value, webhookUrl, }; response = await GenericFunctions_1.mallabeImagesRequest.call(this, 'POST', '/v1/images/blur', body, qs); } else if (operation === 'greyscale') { const url = this.getNodeParameter('url', i); const webhookUrl = this.getNodeParameter('webhookUrl', i); const body = { url, webhookUrl, }; response = await GenericFunctions_1.mallabeImagesRequest.call(this, 'POST', '/v1/images/greyscale', body, qs); } else if (operation === 'join') { const image1Url = this.getNodeParameter('image1Url', i); const image2Url = this.getNodeParameter('image2Url', i); const direction = this.getNodeParameter('direction', i); const align = this.getNodeParameter('align', i); const backgroundColor = this.getNodeParameter('backgroundColor', i); const offset = this.getNodeParameter('offset', i); const webhookUrl = this.getNodeParameter('webhookUrl', i); const body = { image1Url, image2Url, direction, align, backgroundColor, offset, webhookUrl, }; response = await GenericFunctions_1.mallabeImagesRequest.call(this, 'POST', '/v1/images/join', body, qs); } download = this.getNodeParameter('download', i); if (download) { const output = this.getNodeParameter('output', i); const buffer = (await GenericFunctions_1.mallabeImagesCdnRequest.call(this, response.data.url)); responseData = { json: response, binary: { [output]: await this.helpers.prepareBinaryData(buffer), }, }; } else { responseData = response; } } if (Array.isArray(responseData)) { returnData.push.apply(returnData, responseData); } else { returnData.push(responseData); } } catch (error) { if (this.continueOnFail()) { returnData.push({ error: error.message }); continue; } throw error; } } if (download) { return [returnData]; } return [this.helpers.returnJsonArray(returnData)]; } } exports.MallabeImages = MallabeImages; //# sourceMappingURL=MallabeImages.node.js.map