forked from Turistforeningen/node-im-metadata
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
103 lines (82 loc) · 2.23 KB
/
index.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
/*jshint laxbreak:true */
var sizeParser = require('filesize-parser');
var exec = require('child_process').exec,
child;
module.exports = function (path, opts, cb) {
if (!cb) {
cb = opts;
opts = {};
}
var cmd = module.exports.cmd(path, opts);
opts.timeout = opts.timeout || 5000;
exec(cmd, opts, function (e, stdout, stderr) {
if (e) {
return cb(e);
}
if (stderr) {
return cb(new Error(stderr));
}
return cb(null, module.exports.parse(path, stdout, opts));
});
};
module.exports.cmd = function (path, opts) {
opts = opts || {};
var format = [
'name=',
'size=%[size]',
'format=%m',
'colorspace=%[colorspace]',
'height=%[height]',
'width=%[width]',
'orientation=%[orientation]', (opts.exif ? '%[exif:*]' : '')
].join("\n");
var quiet = opts.quiet ? ' -quiet' : '';
return 'identify' + quiet + ' -format "' + format + '" ' + path;
};
module.exports.parse = function (path, stdout, opts) {
var lines = stdout.split('\n');
var ret = {
path: path
};
var i;
for (i = 0; i < lines.length; i++) {
if (lines[i]) {
lines[i] = lines[i].split('=');
// Parse exif metadata keys
if (lines[i][0].substr(0, 5) === 'exif:') {
if (!ret.exif) {
ret.exif = {};
}
ret.exif[lines[i][0].substr(5)] = lines[i][1];
// Parse normal metadata keys
}
else {
ret[lines[i][0]] = lines[i][1];
}
}
}
if (ret.width) {
ret.width = parseInt(ret.width, 10);
}
if (ret.height) {
ret.height = parseInt(ret.height, 10);
}
if (ret.size) {
if (ret.size.substr(ret.size.length - 2) === 'BB') {
ret.size = ret.size.substr(0, ret.size.length - 1);
}
ret.size = parseInt(sizeParser(ret.size));
}
if (ret.colorspace && ret.colorspace === 'sRGB') {
ret.colorspace = 'RGB';
}
if (ret.orientation === 'Undefined') {
ret.orientation = '';
}
if (opts && opts.autoOrient && (ret.orientation === 'LeftTop' || ret.orientation === 'RightTop' || ret.orientation === 'LeftBottom' || ret.orientation === 'RightBottom')) {
ret.width = ret.width + ret.height;
ret.height = ret.width - ret.height;
ret.width = ret.width - ret.height;
}
return ret;
};