-
Notifications
You must be signed in to change notification settings - Fork 13
/
testWithNode.js
383 lines (341 loc) · 14.1 KB
/
testWithNode.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
(function() {
//"use strict";
var walk = require('walk'), fs = require('node-fs');
var sys = require('sys')
var exec = require('child_process').exec;
var exiftoolJS = require('./exiftool.js');
var Gomfunkel = require('exif').ExifImage;
var Redaktor = require('exifr').ExifImage;
var programs = ['exiftool', 'exiftool.js', 'Gomfunkel', 'Redaktor'];
var coverageSummaryHolder = {};
/**
* Sort an object's key's alphabetically.
* Not strictly legit since key ordering is not guaranteed...
*/
var sortObject = function(o) {
var sorted = {},
key, a = [];
for (key in o) {
if (o.hasOwnProperty(key)) {
a.push(key);
}
}
a.sort();
for (key = 0; key < a.length; key++) {
sorted[a[key]] = o[a[key]];
}
return sorted;
}
/**
* Save the exif for the current image into a json file.
* json - js object, basically just map of tags to values.
* imgFile - path of image under test
* program - name of program that extracted the exif (exiftool.js or Redaktor etc.)
*/
var saveJson = function(json, imgFile, program) {
var pathString = json.img;
var jsonFile = 'generated/json/' + program + '/' + imgFile + '.json';
var parentDir = jsonFile.substring(0, jsonFile.lastIndexOf("/"));
fs.mkdirSync(parentDir, 0777, true);
fs.writeFileSync(jsonFile, JSON.stringify(json, null, '\t'));
};
/**
* Extract the exif from the given file using thegiven program. Pass to the callback object.
*/
var extractExif = function(imgFile, program, callback) {
var pathString = 'generated/json/' + program + '/' + imgFile + '.json';
switch(program) {
case 'exiftool' : {
extractExifUsingExiftool(imgFile, callback);
break;
}
case 'exiftool.js' : {
extractExifUsingExiftoolJS(imgFile, callback);
break;
}
case 'Gomfunkel' : {
extractExifUsingGomfunkel(imgFile, callback);
break;
}
case 'Redaktor' : {
extractExifUsingRedaktor(imgFile, callback);
break;
}
}
};
var extractExifUsingExiftool = function(imgFile, callback) {
fs.readFile('generated/json/exiftool/' + imgFile + '.json', 'utf8', function(err, data) {
try {
// First, try to load json from file
var exifFromExiftool = JSON.parse(data);
callback(exifFromExiftool);
} catch (error) {
// Failing that, let's fire up exiftool
var child = exec("exiftool -q -q -F -j --FileAccessDate --FileModifyDate --FileInodeChangeDate --SourceFile --ExifToolVersion --FileName --Directory --FilePermissions --FileSize --FileModifyDate --FileType --MIMEType '" + imgFile + "'", function(error, stdout, stderr) {
if (error !== null) {
console.log('exec error with ' + imgFile + ': ' + error);
callback();
} else {
// stdout string takes some munging...
var exifFromPerl = String(stdout);
exifFromPerl = exifFromPerl.replace(/\r?\n|\r/g, ""); // lose newlines
exifFromPerl = exifFromPerl.substring(1,
exifFromPerl.length - 1); // lose surrounding []
exifFromPerl = JSON.parse(exifFromPerl);
delete exifFromPerl["SourceFile"];
exifFromPerl = sortObject(exifFromPerl);
callback(exifFromPerl);
}
});
}
});
};
var extractExifUsingExiftoolJS = function(imgFile, callback) {
exiftoolJS.getExifFromLocalFileUsingNodeFs(fs, imgFile, callback);
};
var extractExifUsingGomfunkel = function(imgFile, callback) {
new Gomfunkel({ image : imgFile }, createNodeExifCallbackHandler('Gomfunkel', callback));
};
var extractExifUsingRedaktor = function(imgFile, callback) {
new Redaktor({ image : imgFile }, createNodeExifCallbackHandler('Redaktor', callback));
};
var createNodeExifCallbackHandler = function (program, callback) {
return function (error, exif) {
if (error) {
console.log(program + ' Error: ' + error.message);
} else {
// Loop over any children like "exif", "image" or "makernote"
for (var key in exif) {
// ...then loop over their children moving them up to the root
for (var exifKey in exif[key]) {
// skip Objects
var valueType = Object.prototype.toString.call(exif[key][exifKey]);
if (valueType != "[object Object]") {
exif[exifKey] = exif[key][exifKey];
}
};
if (typeof exif[key] === 'object') {
delete exif[key];
}
};
}
callback(exif);
};
};
/**
* Write the html coverage report, and update the coverage summary page.
*/
var updateReports = function(allExif, image, coverageSummary, callback) {
writeFileCoverageReport(allExif, image, coverageSummary, (function(cb) {
return function(cs) {
writeSummaryCoverageReport(cs, cb);
}
})(callback));
}
var writeFileCoverageReport = function(allExif, image, coverageSummary, callback) {
var html = "<h3>" + image + "</h3>\n" +
"<table class='table table-bordered'>" +
"<thead><tr>" +
"<th>tag</th>";
for (var i = 0 ; i < programs.length ; i++) {
var program = programs[i];
html += "<th>" + program + "</th>";
}
html += "</tr></thead>\n<tbody>\n";
for (var key in allExif.exiftool) {
var rowHtml = "<tr>\n";
rowHtml += "<td>" + key + "</td>\n";
for (var i = 0 ; i < programs.length ; i++) {
var program = programs[i];
if (allExif[program][key] == allExif.exiftool[key]) {
rowHtml += "<td class='match'>" + allExif[program][key] + "</td>\n";
if (!coverageSummary.supportedTags) {
coverageSummary.supportedTags = {};
coverageSummary.supportedTagsByFile = {};
}
if (!coverageSummary.supportedTagsByFile[image]) {
coverageSummary.supportedTagsByFile[image] = {};
}
coverageSummary.supportedTags[program] = ++coverageSummary.supportedTags[program] || 1;
coverageSummary.supportedTagsByFile[image][program] = ++coverageSummary.supportedTagsByFile[image][program] || 1;
} else {
if (typeof allExif[program][key] === 'undefined') {
rowHtml += "<td class='missing'>-</td>\n";
} else {
rowHtml += "<td class='diff'>" + allExif[program][key] + "</td>\n";
}
}
}
rowHtml += "</tr>\n";
html += rowHtml;
}
html += "</tbody></table>";
fs.readFile('report/template.html', 'utf8', function(err, data) {
if (err) {
return console.log(err);
}
var result = data.replace(/htmlbody/g, html);
var reportFile = 'generated/reports/' + image + '.html';
var parentDir = reportFile.substring(0, reportFile.lastIndexOf("/"));
if (!fs.existsSync(parentDir)) {
fs.mkdirSync(parentDir, 0777, true);
fs.linkSync('report/css', parentDir + '/css');
fs.linkSync('report/fonts', parentDir + '/fonts');
fs.linkSync('report/js', parentDir + '/js');
}
fs.writeFile(reportFile, result, 'utf8', function(err) {
if (err) {
return console.log(err);
} else {
callback(coverageSummary);
}
});
});
};
/**
* The coverageSummary object looks like this:
*
* {
* 'supportedTags' : {
* 'exiftool' : 20,
* 'exiftoolJS' : 10,
* 'redaktor' : 10
* },
* 'supportedTagsByFile' : {
* 'acer/acer2.jpg' : {
* 'exiftool' : 10,
* 'exiftoolJS' : 5,
* 'redaktor' : 5
* },
* 'acer/acer1.jpg' : {
* 'exiftool' : 10,
* 'exiftoolJS' : 5,
* 'redaktor' : 5
* }
* }
* }
*/
var writeSummaryCoverageReport = function(coverageSummary, callback) {
var html = "<h3>Total tag support</h3>\n" +
"<table class='table table-bordered'>";
for (var i = 0 ; i < programs.length ; i++) {
var program = programs[i];
html += "<tr><th>" + program + "</th><td>" + coverageSummary.supportedTags[program] + "/" + coverageSummary.supportedTags.exiftool + "</td></tr>\n";
}
html += "</table>";
html += "<h3>Manfacturer tag support</h3>";
html += "<table class='table table-bordered'><tr><th>File</th>";
for (var i = 0 ; i < programs.length ; i++) {
var program = programs[i];
html += "<th>" + program + "</th>\n";
}
html += "</tr>";
for (var key in coverageSummary.supportedTagsByFile) {
html += "<tr><td><a href='" + key +".html'>" + key + "</a></td>";
for (var i = 0 ; i < programs.length ; i++) {
var program = programs[i];
html += "<td>" + (coverageSummary.supportedTagsByFile[key][program] || 0) + "</td>";
}
html += "</tr>\n";
}
html += "<tr>";
html += "<th>Totals</th>";
for (var i = 0 ; i < programs.length ; i++) {
var program = programs[i];
html += "<th>" + coverageSummary.supportedTags[program] + "</th>";
}
html += "</tr>\n";
html += "</table>";
fs.readFile('report/template.html', 'utf8', function(err, data) {
if (err) {
return console.log(err);
}
var result = data.replace(/htmlbody/g, html);
var pDir = 'generated/reports/';
var reportFile = pDir + 'index.html';
if (!fs.existsSync(pDir + 'css')) {
fs.linkSync('report/css', pDir + 'css');
fs.linkSync('report/fonts', pDir + 'fonts');
fs.linkSync('report/js', pDir + 'js');
}
fs.writeFile(reportFile, result, 'utf8', function(err) {
if (err) {
return console.log(err);
}
callback(coverageSummary);
});
});
};
var start = function() {
var options = {
followLinks : false
};
var walker = walk.walk("sampleImages", options);
walker.on("names", function(root, nodeNamesArray) {
nodeNamesArray.sort(function(a, b) {
if (a < b)
return 1;
if (a > b)
return -1;
return 0;
});
});
walker.on("directories", function(root, dirStatsArray, next) {
next();
});
/**
* This is where the magic happens. Run this function for each file found in sampleImages.
*/
walker.on("file", function(root, fileStats, next) {
var imgFile = root + '/' + fileStats.name;
if (fileStats.name == 'PanasonicDMC-GM1.jpg'
|| fileStats.name == 'PanasonicDMC-GX7.jpg'
|| fileStats.name == 'PanasonicDMC-SZ5.jpg'
|| fileStats.name == 'PanasonicDMC-XS3.jpg'
|| fileStats.name == 'IMG_6756.JPG'
|| fileStats.name == 'big file - IMG_6756.JPG'
|| fileStats.name == 'SamsungGT-I9100.jpg'
) {
// Skip these files as they get stuck in infinite loops!
next();
} else {
try {
console.log("processing: " + imgFile);
var allExif = {};
for (var i = 0 ; i < programs.length ; i++) {
extractExif(imgFile, programs[i], (function(image, program) {
return function(ef) {
if (ef) {
saveJson(ef, image, program);
allExif[program] = ef;
if (Object.keys(allExif).length == programs.length) {
updateReports(allExif, image, coverageSummaryHolder, function(cs) {
coverageSummaryHolder = cs;
next();
});
}
} else {
next();
}
};
})(imgFile, programs[i]));
};
} catch (error) {
console.log('in Error: ' + error.message);
next();
}
}
});
};
/**
* Start everything!
*/
if (process.argv.length > 2 && process.argv[2] === 'clean') {
var child = exec('rm -rf generated', function(err,out) {
console.log("generated output dir cleaned.");
start();
});
} else {
start();
}
}());