getters.js
7.86 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
* Extend proto.
*/
module.exports = function (gm) {
var proto = gm.prototype;
/**
* `identify` states
*/
const IDENTIFYING = 1;
const IDENTIFIED = 2;
/**
* Map getter functions to output names.
*
* - format: specifying the -format argument (see man gm)
* - verbose: use -verbose instead of -format (only if necessary b/c its slow)
* - helper: use the conversion helper
*/
var map = {
'format': { key: 'format', format: '%m ', helper: 'Format' }
, 'depth': { key: 'depth', format: '%q' }
, 'filesize': { key: 'Filesize', format: '%b' }
, 'size': { key: 'size', format: '%wx%h ', helper: 'Geometry' }
, 'color': { key: 'color', format: '%k', helper: 'Colors' }
, 'orientation': { key: 'Orientation', format: '%[EXIF:Orientation]', helper: 'Orientation' }
, 'res': { key: 'Resolution', verbose: true }
}
/**
* Getter functions
*/
Object.keys(map).forEach(function (getter) {
proto[getter] = function (opts, callback) {
if (!callback) callback = opts, opts = {};
if (!callback) return this;
var val = map[getter]
, key = val.key
, self = this;
if (self.data[key]) {
callback.call(self, null, self.data[key]);
return self;
}
self.on(getter, callback);
self.bufferStream = !!opts.bufferStream;
if (val.verbose) {
self.identify(opts, function (err, stdout, stderr, cmd) {
if (err) {
self.emit(getter, err, self.data[key], stdout, stderr, cmd);
} else {
self.emit(getter, err, self.data[key]);
}
});
return self;
}
var args = makeArgs(self, val);
self._exec(args, function (err, stdout, stderr, cmd) {
if (err) {
self.emit(getter, err, self.data[key], stdout, stderr, cmd);
return;
}
var result = (stdout||'').trim();
if (val.helper in helper) {
helper[val.helper](self.data, result);
} else {
self.data[key] = result;
}
self.emit(getter, err, self.data[key]);
});
return self;
}
});
/**
* identify command
*
* Overwrites all internal data with the parsed output
* which is more accurate than the fast shortcut
* getters.
*/
proto.identify = function identify (opts, callback) {
// identify with pattern
if (typeof(opts) === 'string') {
opts = {
format: opts
}
}
if (!callback) callback = opts, opts = {};
if (!callback) return this;
if (opts && opts.format) return identifyPattern.call(this, opts, callback);
var self = this;
if (IDENTIFIED === self._identifyState) {
callback.call(self, null, self.data);
return self;
}
self.on('identify', callback);
if (IDENTIFYING === self._identifyState) {
return self;
}
self._identifyState = IDENTIFYING;
self.bufferStream = !!opts.bufferStream;
var args = makeArgs(self, { verbose: true });
self._exec(args, function (err, stdout, stderr, cmd) {
if (err) {
self.emit('identify', err, self.data, stdout, stderr, cmd);
return;
}
err = parse(stdout, self);
if (err) {
self.emit('identify', err, self.data, stdout, stderr, cmd);
return;
}
self.data.path = self.source;
self.emit('identify', null, self.data);
self._identifyState = IDENTIFIED;
});
return self;
}
/**
* identify with pattern
*
* Execute `identify -format` with custom pattern
*/
function identifyPattern (opts, callback) {
var self = this;
self.bufferStream = !!opts.bufferStream;
var args = makeArgs(self, opts);
self._exec(args, function (err, stdout, stderr, cmd) {
if (err) {
return callback.call(self, err, undefined, stdout, stderr, cmd);
}
callback.call(self, err, (stdout||'').trim());
});
return self;
}
/**
* Parses `identify` responses.
*
* @param {String} stdout
* @param {Gm} self
* @return {Error} [optionally]
*/
function parse (stdout, self) {
// normalize
var parts = (stdout||"").trim().replace(/\r\n|\r/g, "\n").split("\n");
// skip the first line (its just the filename)
parts.shift();
try {
var len = parts.length
, rgx1 = /^( *)(.+?): (.*)$/ // key: val
, rgx2 = /^( *)(.+?):$/ // key: begin nested object
, out = { indent: {} }
, level = null
, lastkey
, i = 0
, res
, o
for (; i < len; ++i) {
res = rgx1.exec(parts[i]) || rgx2.exec(parts[i]);
if (!res) continue;
var indent = res[1].length
, key = res[2] ? res[2].trim() : '';
if ('Image' == key || 'Warning' == key) continue;
var val = res[3] ? res[3].trim() : null;
// first iteration?
if (null === level) {
level = indent;
o = out.root = out.indent[level] = self.data;
} else if (indent < level) {
// outdent
if (!(indent in out.indent)) {
continue;
}
o = out.indent[indent];
} else if (indent > level) {
// dropping into a nested object
out.indent[level] = o;
// weird format, key/val pair with nested children. discard the val
o = o[lastkey] = {};
}
level = indent;
if (val) {
// if previous key was exist and we got the same key
// cast it to an array.
if(o.hasOwnProperty(key)){
// cast it to an array and dont forget the previous value
if(!Array.isArray(o[key])){
var tmp = o[key];
o[key] = [tmp];
}
// set value
o[key].push(val);
} else {
o[key] = val;
}
if (key in helper) {
helper[key](o, val);
}
}
lastkey = key;
}
} catch (err) {
err.message = err.message + "\n\n Identify stdout:\n " + stdout;
return err;
}
}
/**
* Create an argument array for the identify command.
*
* @param {gm} self
* @param {Object} val
* @return {Array}
*/
function makeArgs (self, val) {
var args = [
'identify'
, '-ping'
];
if (val.format) {
args.push('-format', val.format);
}
if (val.verbose) {
args.push('-verbose');
}
args = args.concat(self.src());
return args;
}
/**
* Map exif orientation codes to orientation names.
*/
var orientations = {
'1': 'TopLeft'
, '2': 'TopRight'
, '3': 'BottomRight'
, '4': 'BottomLeft'
, '5': 'LeftTop'
, '6': 'RightTop'
, '7': 'RightBottom'
, '8': 'LeftBottom'
}
/**
* identify -verbose helpers
*/
var helper = gm.identifyHelpers = {};
helper.Geometry = function Geometry (o, val) {
// We only want the size of the first frame.
// Each frame is separated by a space.
var split = val.split(" ").shift().split("x");
var width = parseInt(split[0], 10);
var height = parseInt(split[1], 10);
if (o.size && o.size.width && o.size.height) {
if (width > o.size.width) o.size.width = width;
if (height > o.size.height) o.size.height = height;
} else {
o.size = {
width: width,
height: height
}
}
};
helper.Format = function Format (o, val) {
o.format = val.split(" ")[0];
};
helper.Depth = function Depth (o, val) {
o.depth = parseInt(val, 10);
};
helper.Colors = function Colors (o, val) {
o.color = parseInt(val, 10);
};
helper.Orientation = function Orientation (o, val) {
if (val in orientations) {
o['Profile-EXIF'] || (o['Profile-EXIF'] = {});
o['Profile-EXIF'].Orientation = val;
o.Orientation = orientations[val];
} else {
o.Orientation = val || 'Unknown';
}
};
}