forked from simme/node-exift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexift.js
More file actions
122 lines (112 loc) · 2.34 KB
/
Copy pathexift.js
File metadata and controls
122 lines (112 loc) · 2.34 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
//
// # Exift
//
// Read EXIF data.
//
//
// ## Constructor
//
// Creates a new Exift object.
//
// If `exiftool` is not in your path do:
//
// var exif = new Exift();
// exif.exiftool = '/path/to/exiftool';
//
var Exift = module.exports = function () {
/* Get dependencies */
this.lib = {};
this.lib.stat = require('fs').stat;
this.lib.watch = require('fs').watchFile;
this.lib.spawn = require('child_process').spawn;
/* Change this if exiftool is not in your path. */
this.exiftool = 'exiftool';
};
//
// ### Extends EventEmitter
//
require('util').inherits(Exift, require('events').EventEmitter);
//
// ## Read EXIF
//
// Tries to read data from the given path. If successfull the callback
// will recieve the read data.
//
// **parameters**
//
// * path
//
// Path to the file/directory to read.
//
// * callback
//
// A function that will be called when data is read. Follows the Node.js
// standard with an error argument first and result second.
//
Exift.prototype.readData = function (path, fn) {
var self = this;
self.lib.stat(path, function (err, stat) {
if (err) {
fn(err);
}
else {
var et = self.lib.spawn(self.exiftool, ['-j', '-sort', path]);
var exif = '';
var err = '';
var hasErr = false;
et.stdout.on('data', function (data) {
exif += data;
});
et.stderr.on('data', function (data) {
err += data;
hasErr = true;
});
et.on('exit', function (code) {
if (hasErr && code !== 0) {
if (err.length === 0) {
err = 'Exiftool exited with code: ' + code;
}
fn(new Error(err));
}
else {
var json = exif.toString();
fn(null, JSON.parse(json));
}
});
}
});
};
//
// ## Watch a path
//
// Watches the given path for changes and re-reads exif data.
//
// **parameters**
//
// * path
//
// The path to watch.
//
// **events**
//
// * data
//
// Emitted every time new data is parsed.
//
// * error
//
// Emitted when readData returns an error.
//
Exift.prototype.watch = function (path) {
var self = this;
this.lib.watch(path, function (e, filename) {
self.readData(path, function (err, data) {
if (err) {
self.emit('error', err);
}
else {
self.emit('data', data);
}
});
});
}