UNPKG

n8n-nodes-image-sharp

Version:

n8n node for image processing and optimization with sharp

169 lines 7.43 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ImageSharp = void 0; const n8n_workflow_1 = require("n8n-workflow"); const ImageSharpDescription_1 = require("./ImageSharpDescription"); const sharp_1 = __importDefault(require("sharp")); const ImageSharpDefaults_1 = require("./ImageSharpDefaults"); const node_path_1 = __importDefault(require("node:path")); class ImageSharp { constructor() { this.description = { displayName: 'Image Sharp', name: 'imageSharp', icon: "file:imagesharp.svg", group: ['transform'], version: 1, description: 'Process and optimize an image', defaults: { name: 'Image Sharp', }, inputs: ['main'], outputs: ['main'], properties: [ ...ImageSharpDescription_1.imageSharpOperations, { displayName: 'Input Binary Field', name: 'binaryPropertyName', type: 'string', default: 'data', placeholder: '', required: true, description: 'The name of the input binary field containing the image', }, { displayName: 'Output Formats', name: 'formats', type: 'multiOptions', options: [ { name: 'Png', value: 'png', }, { name: 'Jpeg', value: 'jpeg', }, { name: 'Webp', value: 'webp', }, { name: 'Avif', value: 'avif', } ], default: ['png', 'jpeg'], required: true, description: 'The image output formats', } ], }; } async execute() { let returnData = []; const items = this.getInputData(); let item; let binaryPropertyName; for (let itemIndex = 0; itemIndex < items.length; itemIndex++) { try { binaryPropertyName = this.getNodeParameter('binaryPropertyName', itemIndex); item = items[itemIndex]; if (!item.binary) throw new n8n_workflow_1.NodeOperationError(this.getNode(), `input data required`, { itemIndex }); const inputData = item.binary[binaryPropertyName]; if (inputData.fileType && inputData.fileType !== 'image') throw new n8n_workflow_1.NodeOperationError(this.getNode(), `unsupported file type: ${inputData.fileType}`, { itemIndex }); const input = await this.helpers.getBinaryDataBuffer(itemIndex, binaryPropertyName); const formats = this.getNodeParameter('formats', itemIndex); if (itemIndex === 0) { const nodeOutputs = this.getNodeOutputs(); returnData = new Array(nodeOutputs.length).fill(0).map(() => []); } const imageOutputs = []; const imgSharp = (0, sharp_1.default)(input); for (const format of formats) { let pipe = imgSharp; switch (format) { case 'png': pipe = pipe.png(ImageSharpDefaults_1.optimizeDefaults.png); break; case 'jpeg': pipe = pipe.jpeg(ImageSharpDefaults_1.optimizeDefaults.jpeg); break; case 'webp': pipe = pipe.webp(ImageSharpDefaults_1.optimizeDefaults.webp); break; case 'avif': pipe = pipe.avif(ImageSharpDefaults_1.optimizeDefaults.avif); break; default: throw new n8n_workflow_1.NodeOperationError(this.getNode(), `unsupported image format '${format}'`, { itemIndex }); } const r = pipe.toBuffer({ resolveWithObject: true }) .then(n => { return { format, ...n }; }); imageOutputs.push(r); } for await (const imageOutput of imageOutputs) { const outputIndex = 0; if (outputIndex < 0) throw new n8n_workflow_1.NodeOperationError(this.getNode(), `output format '${imageOutput.format}' unknown`, { itemIndex }); let fileName = inputData.fileName; let ext = inputData.fileExtension; let mimeType = inputData.mimeType; switch (imageOutput.format) { case 'png': mimeType = 'image/png'; ext = 'png'; break; case 'jpeg': mimeType = 'image/jpeg'; ext = 'jpg'; break; case 'webp': mimeType = 'image/webp'; ext = 'webp'; break; case 'avif': mimeType = 'image/avif'; ext = 'avif'; break; } if (fileName) { const name = node_path_1.default.basename(fileName, node_path_1.default.extname(fileName)); fileName = `${name}.min.${ext}`; } const binary = await this.helpers.prepareBinaryData(imageOutput.data, fileName, mimeType); returnData[outputIndex].push({ pairedItem: { item: itemIndex }, json: { ...imageOutput.info }, binary: { [binaryPropertyName]: binary } }); } } catch (error) { console.error(error); if (this.continueOnFail()) { returnData[0].push({ pairedItem: itemIndex, json: { error: error.message } }); } else { if (error.context) { error.context.itemIndex = itemIndex; throw error; } throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex, }); } } } return returnData; } } exports.ImageSharp = ImageSharp; //# sourceMappingURL=ImageSharp.node.js.map