-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfile-wrapper.js
101 lines (83 loc) · 3.15 KB
/
file-wrapper.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
;
/* https://github.com/anpur/line-navigator Anton Purin MIT 2016 */
var createFileWrapper = function() {
function FileWrapper (file, encoding) {
var self = this;
// Node JS
if (self.isNode(file)) {
self.fs = require('fs');
var StringDecoder = require('string_decoder').StringDecoder;
self.string_decoder = new StringDecoder(encoding);
if (self.fs.statSync(file) === undefined)
throw "File '" + file + "' doesn't exists!";
var fd = self.fs.openSync(file, 'r');
self.readChunk = function (offset, length, callback) {
var buffer = new Buffer(length);
self.fs.read(fd, buffer, 0, length, offset, function (e, br) { callback(e, buffer, br); });
};
self.decode = function(buffer, callback) {
callback(self.string_decoder.write(buffer));
};
self.getSize = function() {
return self.fs.statSync(file)["size"];
};
}
// HTML5 File
else if (self.isHtml5File(file)) {
self.readChunk = function (offset, length, callback) {
lastPosition = offset + length;
var reader = new FileReader();
reader.onloadend = function(progress) {
var buffer;
if (reader.result) {
buffer = new Int8Array(reader.result, 0);
buffer.slice = buffer.subarray;
}
callback(progress.err, buffer, progress.loaded);
};
reader.readAsArrayBuffer(file.slice(offset, offset + length));
};
self.decode = function(buffer, callback) {
var reader = new FileReader();
reader.onloadend = function(progress) {
callback(progress.currentTarget.result);
};
if (typeof encoding !== 'undefined') {
reader.readAsText(new Blob([buffer]), encoding);
} else {
reader.readAsText(new Blob([buffer]));
}
};
self.getSize = function() {
return file.size;
};
}
// Unknown
else {
throw "Given file should be instance of the File class for browser \
or a string containing a path to a file for Node.js, \
but it is neither: [" + (typeof file) + "] " + file;
}
}
FileWrapper.prototype.isNode = function (file) {
return typeof module !== 'undefined' && module.exports && typeof file === 'string';
};
FileWrapper.prototype.isHtml5File = function (file) {
return typeof File !== 'undefined' && file instanceof File;
}
return FileWrapper;
}
// Node.js
if (typeof module !== 'undefined' && module.exports) {
module.exports = createFileWrapper();
}
// AMD
else if (typeof define === 'function') {
define(function(){
return createFileWrapper();
});
}
// Vanilla
else {
FileWrapper = createFileWrapper();
}