-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageDetector.js
69 lines (52 loc) · 1.34 KB
/
ImageDetector.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
/*
Detects the file and calls the call back with appropriate string
*/
var io = require ('fs');
var ImageDetector = {
JPGSign: [255, 216],
PNGSign: [137,80,78,71,13,10,26,10],
GIFSign: [71, 73, 70, 56, 57],
Detect: function(fileName, cb) {
var that = this;
io.readFile(fileName, function (error, data) {
if (that._compare(that.JPGSign, data.slice(0, that.JPGSign.length))) {
cb('JPG');
return;
}
if (that._compare(that.PNGSign, data.slice(0, that.PNGSign.length))) {
cb('PNG');
return;
}
if (that._compare(that.GIFSign, data.slice(0, that.GIFSign.length))) {
cb('GIF');
return;
}
cb('UNKNOWN');
}); // end of readFile
},
_compare: function(first, second) {
for(var f=0; f < first.length; f+=1) {
if (first[f] !== second[f])
return false;
return true;
}
} // end of _compare
};
/* Some simple tests to see ImageDetector works just fine */
ImageDetector.Detect('jpeg.jpg', function(i) {
console.log(i);
});
ImageDetector.Detect('png.png', function(i) {
console.log(i);
});
ImageDetector.Detect('server.js', function(o) {
console.log(o);
});
ImageDetector.Detect('gif.gif', function(o) {
console.log(o);
});
// Just adding a new test where the file is not there
ImageDetector.Detect('abc2', function(o){
console.log(o);
});
ImageDetector.a? == 1