sharp package update / check is sharp is available
sharp package update / check is sharp is available

module.exports = (RED) => { module.exports = (RED) => {
"use strict"; "use strict";
let onvif = require("node-onvif"); let onvif = require("node-onvif");
const sharp = require('sharp'); let isSharpAvailable = false;
  try {
  const sharp = require('sharp');
  isSharpAvailable = true;
  }
  catch (ex) {}
   
function snapshot(config) { function snapshot(config) {
RED.nodes.createNode(this, config); RED.nodes.createNode(this, config);
let node = this; let node = this;
   
node.on("input", function (msg) { node.on("input", function (msg) {
   
config.name = msg.payload.name || config.name; config.name = msg.payload.name || config.name;
config.url = msg.payload.url || config.url; config.url = msg.payload.url || config.url;
config.username = msg.payload.username || config.username; config.username = msg.payload.username || config.username;
config.password = msg.payload.password || config.password; config.password = msg.payload.password || config.password;
config.resize = msg.payload.resize || config.resize; config.resize = msg.payload.resize || config.resize;
   
if(msg.hasOwnProperty("payload")) { if(msg.hasOwnProperty("payload")) {
msg._payload = msg.payload; msg._payload = msg.payload;
} }
msg.node = this.type; msg.node = this.type;
   
if (!config.url) { if (!config.url) {
node.warn("No URL is specified. Please specify in node configuration."); node.warn("No URL is specified. Please specify in node configuration.");
return; return;
} }
   
run(msg, node, config); run(msg, node, config);
}); });
} }
RED.nodes.registerType("ONVIF Snapshot", snapshot); RED.nodes.registerType("ONVIF Snapshot", snapshot);
   
function run(msg, node, config) { function run(msg, node, config) {
let onvifInstance = new onvif.OnvifDevice({ let onvifInstance = new onvif.OnvifDevice({
xaddr: config.url, xaddr: config.url,
user : config.username, user : config.username,
pass : config.password pass : config.password
}); });
   
onvifInstance.init().then((info) => { onvifInstance.init().then((info) => {
node.log('Fetching snapshot from ' + config.url); node.log('Fetching snapshot from ' + config.url);
return onvifInstance.fetchSnapshot(); return onvifInstance.fetchSnapshot();
}).then((res) => { }).then((res) => {
let prefix = 'data:' + res.headers['content-type'] + ';base64,'; let prefix = 'data:' + res.headers['content-type'] + ';base64,';
   
if (config.resize) { if (config.resize && isSharpAvailable) {
sharp(Buffer.from(res.body, 'binary')) sharp(Buffer.from(res.body, 'binary'))
.resize(config.resize) .resize(config.resize)
.toFormat('png') .toFormat('png')
.toBuffer() .toBuffer()
.then( data => { .then( data => {
msg.payload = { msg.payload = {
config: config, config: config,
image: { image: {
base64: (prefix + data.toString('base64')), base64: (prefix + data.toString('base64')),
binary: res.body binary: res.body
} }
}; };
node.send(msg); node.send(msg);
}).catch( err => { }).catch( err => {
   
}); });
} else { } else {
let base64Image = Buffer.from(res.body, 'binary').toString('base64'); let base64Image = Buffer.from(res.body, 'binary').toString('base64');
msg.payload = { msg.payload = {
config: config, config: config,
image: { image: {
base64: (prefix + base64Image), base64: (prefix + base64Image),
binary: res.body binary: res.body
} }
}; };
node.send(msg); node.send(msg);
} }
}).catch((error) => { }).catch((error) => {
msg.payload = null; msg.payload = null;
msg.error = error; msg.error = error;
node.send(msg); node.send(msg);
}); });
} }
} }
   
{ {
"name": "node-red-contrib-onvif", "name": "node-red-contrib-onvif",
"version": "1.0.1", "version": "1.0.2",
"description": "A Node-RED node that interacts with ip cameras using the ONVIF protocol", "description": "A Node-RED node that interacts with ip cameras using the ONVIF protocol",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/razvanstanga/node-red-contrib-onvif.git" "url": "https://github.com/razvanstanga/node-red-contrib-onvif.git"
}, },
"dependencies": { "dependencies": {
"node-onvif": "^0.1.7", "node-onvif": "^0.1.7",
"sharp": "^0.26.2" "sharp": "^0.28.3"
}, },
"inputs": 1, "inputs": 1,
"keywords": [ "keywords": [
"node-red", "node-red",
"node-red-contrib", "node-red-contrib",
"onvif", "onvif",
"ipcam" "ipcam"
], ],
"node-red": { "node-red": {
"nodes": { "nodes": {
"ONVIF Snapshot": "onvif/snapshot.js" "ONVIF Snapshot": "onvif/snapshot.js"
} }
}, },
"author": { "author": {
"name": "Razvan Stanga" "name": "Razvan Stanga"
}, },
"license": "Apache", "license": "Apache",
"bugs": { "bugs": {
"url": "https://github.com/razvanstanga/node-red-contrib-onvif/issues" "url": "https://github.com/razvanstanga/node-red-contrib-onvif/issues"
}, },
"homepage": "https://github.com/razvanstanga/node-red-contrib-onvif" "homepage": "https://github.com/razvanstanga/node-red-contrib-onvif"
} }
   
comments