-
Notifications
You must be signed in to change notification settings - Fork 17
/
imageViewer.js
195 lines (174 loc) · 7.91 KB
/
imageViewer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* CREDIT...
This image viewer node is heavily based on the excellent works by rikukissa and dceejay
* rikukissa (https://github.com/rikukissa)
* dceejay (https://github.com/dceejay)
* src: https://github.com/rikukissa/node-red-contrib-image-output
MIT License included as per Copyright (c) 2018 Riku Rouvila
MIT License
Copyright (c) 2018 Riku Rouvila, 2021 Steve-Mcl
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
module.exports = function (RED) {
function imageViewer(config) {
var Jimp = require('jimp');
const isBase64 = require('is-base64');
const _prefixURIMap = {
"iVBOR": "data:image/png;base64,",
"R0lGO": "data:image/gif;base64,",
"/9j/4": "data:image/jpeg;base64,",
"Qk02U": "data:image/bmp;base64,"
}
const createDataURI = function (rawImage) {
let first5 = rawImage.substr(0, 5)
if (_prefixURIMap[first5]) {
return _prefixURIMap[first5] + rawImage;
}
return _prefixURIMap["iVBOR"] + rawImage;//default to png
}
RED.nodes.createNode(this, config);
var node = this;
node.data = config.data || "";//data
node.dataType = config.dataType || "msg";
this.active = config.active;
node.on("input", function (msg) {
//first clear any error status
node.status({});
if (this.active !== true) {
node.send(msg);//pass it on & return
return;
}
var nodeStatusError = function (err, msg, statusText) {
console.error(err);
node.error(err, msg);
node.status({ fill: "red", shape: "dot", text: statusText });
}
var data;
try {
/* **************** Get Image Data Parameter **************** */
var dataInput;
RED.util.evaluateNodeProperty(node.data, node.dataType, node, msg, (err, value) => {
if (err) {
nodeStatusError(err, msg, "Error getting Image Data parameter");
return;//halt flow!
} else {
dataInput = value;
}
});
if (!dataInput) {
nodeStatusError("dataInput is empty (Image parameter)", msg, "Error. Image is null");
return null;
}
let isBuffer = Buffer.isBuffer(dataInput);
let isArray = Array.isArray(dataInput);
let isString = typeof dataInput === 'string';
let hasMime = false, isBase64Image = false;
let gif = false;
if (isString) {
hasMime = dataInput.startsWith("data:");
isBase64Image = isBase64(dataInput, { mimeRequired: hasMime });
if (isBase64Image && !hasMime) {
dataInput = createDataURI(dataInput);
hasMime = true;
}
}
let isfileName = isString && !isBase64Image;
//hack to support gif. Oddly, Jimp can read a gif but fails if you try to do most operations
if (dataInput instanceof Jimp && dataInput._originalMime == "image/gif") {
dataInput.getBuffer(Jimp.MIME_PNG, (e, b) => {
if (e) {
throw e;
}
gif = true;
dataInput = b;
isBuffer = true;
})
}
if (isString && isBase64Image && hasMime) {
//its already base 64 with mime
node.send(msg);//pass it on before displaying
RED.comms.publish("image-tools-image-viewer", { id: this.id, data: dataInput });
} else if (dataInput instanceof Jimp && !gif) {
dataInput.getBase64(Jimp.AUTO, (err, b64) => {
if (err) {
nodeStatusError(err, msg, "Error getting base64 image")
return;
}
node.send(msg);//pass it on before displaying
RED.comms.publish("image-tools-image-viewer", { id: this.id, data: b64 });
});
} else {
var imageData;
if (isString && isBase64Image && !hasMime) {
imageData = Buffer.from(dataInput, 'base64')
} else if (Buffer.isBuffer(dataInput)) {
//make a copy of the buffer before sending it on
imageData = new Buffer.alloc(dataInput.length);
dataInput.copy(imageData);
} else {
imageData = dataInput;
}
node.send(msg);//we have a copy of the data - pass ont the msg now
//now generate an image from the buffer/url/path
Jimp.read(imageData)
.then(img => {
try {
img.getBase64(Jimp.AUTO, (err, b64) => {
if (err) {
nodeStatusError(err, msg, "Error getting base64 image")
return;
}
RED.comms.publish("image-tools-image-viewer", { id: this.id, data: b64 });
});
} catch (err) {
nodeStatusError(err, msg, "Error getting base64 image")
}
})
.catch(err => {
nodeStatusError(err, msg, "Error reading image")
});
}
}
catch (e) {
nodeStatusError(e, msg, "Error reading image")
}
});
node.on("close", function () {
RED.comms.publish("image-tools-image-viewer", { id: this.id });
node.status({});
});
}
RED.nodes.registerType("image viewer", imageViewer);
RED.httpAdmin.post("/image-viewer/:id/:state", RED.auth.needsPermission("image-viewer.write"), function (req, res) {
var state = req.params.state;
var node = RED.nodes.getNode(req.params.id);
if (node === null || typeof node === "undefined") {
res.sendStatus(404);
return;
}
if (state === "enable") {
node.active = true;
res.send('activated');
}
else if (state === "disable") {
node.active = false;
res.send('deactivated');
}
else {
res.sendStatus(404);
}
});
};