forked from titarenko/fast-exif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (43 loc) · 1.33 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
'use strict';
var promisify = require('util').promisify;
var fs = require('fs');
var exifReader = require('exif-reader');
var open = promisify(fs.open);
var read = promisify(fs.read);
var close = promisify(fs.close);
module.exports = {
read: readExif
};
function readExif (filename, maxIterations) {
if (maxIterations === true) { // former isDeepSearch boolean argument
maxIterations = Number.MAX_SAFE_INTEGER;
}
if (maxIterations === undefined) {
maxIterations = 1;
}
return open(filename, 'r').then(function (fd) {
var buffer = new Buffer(512);
return searchExif(fd, buffer, 0, maxIterations)
.then(function (exifBuffer) {
return exifBuffer && exifReader(exifBuffer);
})
.finally(function () {
return close(fd);
});
});
}
function searchExif (fd, buffer, fileOffset, remainingIterations) {
var offset = 0, length = buffer.length;
return read(fd, buffer, 0, length, null).then(function (bytesRead) {
if (!bytesRead) {
return null;
}
while (offset < length) {
if (buffer[offset++] == 0xFF && buffer[offset] == 0xE1) {
var exifBuffer = new Buffer(buffer.readUInt16BE(++offset));
return read(fd, exifBuffer, 0, exifBuffer.length, fileOffset + offset + 2).return(exifBuffer);
}
}
return remainingIterations > 1 ? searchExif(fd, buffer, fileOffset + length, remainingIterations - 1) : null;
});
}