forked from swang/FileReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileReader.js
389 lines (329 loc) · 10.7 KB
/
FileReader.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
384
385
386
387
388
389
//
// FileReader
//
// http://www.w3.org/TR/FileAPI/#dfn-filereader
// https://developer.mozilla.org/en/DOM/FileReader
(function () {
"use strict";
require ('@flyskywhy/react-native-browser-polyfill');
var fs = require('react-native-blob-util').default.fs
, mime = require('react-native-mime-types')
, EventEmitter = require("events").EventEmitter
;
function doop(fn, args, context) {
if ('function' === typeof fn) {
fn.apply(context, args);
}
}
function toDataUrl(data, type) {
// var data = self.result;
var dataUrl = 'data:';
if (type) {
dataUrl += type + ';';
}
if (/text/i.test(type)) {
dataUrl += 'charset=utf-8,';
dataUrl += data.toString('utf8');
} else {
dataUrl += 'base64,';
dataUrl += data.toString('base64');
}
return dataUrl;
}
function toArrayBuffer(buffer) {
var ab = new ArrayBuffer(buffer.length);
var view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
view[i] = buffer[i];
}
return view;
}
function mapDataToFormat(file, data, format, encoding) {
// var data = self.result;
switch(format) {
case 'buffer':
return toArrayBuffer(data);
break;
case 'binary':
return data.toString('binary');
break;
case 'dataUrl':
return toDataUrl(data, file.type);
break;
case 'text':
return data.toString(encoding || 'utf8');
break;
}
}
function FileReader() {
var self = this,
emitter = new EventEmitter,
file = {};
self.addEventListener = function (on, callback) {
emitter.on(on, callback);
};
self.removeEventListener = function (callback) {
emitter.removeListener(callback);
}
self.dispatchEvent = function (on) {
emitter.emit(on);
}
self.EMPTY = 0;
self.LOADING = 1;
self.DONE = 2;
self.error = undefined; // Read only
self.readyState = self.EMPTY; // Read only
self.result = undefined; // Road only
// non-standard
self.on = function () {
emitter.on.apply(emitter, arguments);
}
self.nodeChunkedEncoding = false;
self.setNodeChunkedEncoding = function (val) {
self.nodeChunkedEncoding = val;
};
// end non-standard
// Whatever the file object is, turn it into a Node.JS File.Stream
function createFileStream() {
var stream = new EventEmitter(),
chunked = self.nodeChunkedEncoding;
// The stream exists, do nothing more
if (file.stream) {
return;
}
// Create a read stream from a buffer
if (file.buffer) {
process.nextTick(function () {
stream.emit('data', file.buffer);
stream.emit('end');
});
file.stream = stream;
return;
}
// Create a read stream from a blob-polyfill Blob
if (file.data) {
process.nextTick(function () {
stream.emit('data', Buffer.from(file.data, "ascii"));
stream.emit('end');
});
file.stream = stream;
return;
}
// Create a read stream from a file
if (file.path) {
// TODO url
if (!chunked) {
fs.readFile(file.path, 'ascii').then(data => {
stream.emit('data', Buffer.from(data, 'ascii'));
stream.emit('end');
}, err => {
stream.emit('error', err);
});
file.stream = stream;
return;
}
fs.readStream(file.path, 'ascii').then((ifstream) => {
ifstream.open();
ifstream.onData((chunk) => {
// when encoding is `ascii`, chunk will be an array contains numbers
// otherwise it will be a string
stream.emit('data', Buffer.from(chunk, 'ascii'));
});
ifstream.onError((err) => {
stream.emit('error', err);
})
ifstream.onEnd(() => {
stream.emit('end');
})
})
file.stream = stream;
return;
}
}
// before any other listeners are added
emitter.on('abort', function () {
self.readyState = self.DONE;
});
// Map `error`, `progress`, `load`, and `loadend`
function mapStreamToEmitter(format, encoding) {
var stream = file.stream,
buffers = [],
chunked = self.nodeChunkedEncoding;
buffers.dataLength = 0;
stream.on('error', function (err) {
if (self.DONE === self.readyState) {
return;
}
self.readyState = self.DONE;
self.error = err;
emitter.emit('error', err);
});
stream.on('data', function (data) {
if (self.DONE === self.readyState) {
return;
}
buffers.dataLength += data.length;
buffers.push(data);
emitter.emit('progress', {
lengthComputable: (!isNaN(file.size)) ? true : false,
loaded: buffers.dataLength,
total: file.size
});
emitter.emit('data', data);
});
stream.on('end', function () {
if (self.DONE === self.readyState) {
return;
}
var data;
if (buffers.length > 1 ) {
data = Buffer.concat(buffers);
} else {
data = buffers[0];
}
self.readyState = self.DONE;
self.result = mapDataToFormat(file, data, format, encoding);
emitter.emit('load', {
target: {
// non-standard
nodeBufferResult: data,
result: self.result
}
});
emitter.emit('loadend');
});
}
// Abort is overwritten by readAsXyz
self.abort = function () {
if (self.readState == self.DONE) {
return;
}
self.readyState = self.DONE;
emitter.emit('abort');
};
//
function mapUserEvents() {
emitter.on('start', function () {
doop(self.onloadstart, arguments);
});
emitter.on('progress', function () {
doop(self.onprogress, arguments);
});
emitter.on('error', function (err) {
// TODO translate to FileError
if (self.onerror) {
self.onerror(err);
} else {
if (!emitter.listeners.error || !emitter.listeners.error.length) {
throw err;
}
}
});
emitter.on('load', function () {
doop(self.onload, arguments);
});
emitter.on('loadend', function () {
doop(self.onloadend, arguments);
});
emitter.on('abort', function () {
doop(self.onabort, arguments);
});
}
function _readFile(file, format, encoding) {
if (!file || !(file.name || file.data) || !(file.path || file.stream || file.buffer || file.data)) {
throw new Error('react-native-filereader error: cannot read as File: ' + JSON.stringify(file));
}
if (0 !== self.readyState) {
console.log("already loading, request to change format ignored");
return;
}
// 'process.nextTick' does not ensure order, (i.e. an fs.stat queued later may return faster)
// but `onloadstart` must come before the first `data` event and must be asynchronous.
// Hence we waste a single tick waiting
process.nextTick(function () {
self.readyState = self.LOADING;
emitter.emit('loadstart');
createFileStream();
mapStreamToEmitter(format, encoding);
mapUserEvents();
});
}
function readFile(input, format, encoding) {
if (!input) {
throw new Error('react-native-filereader error, input is: ' + input);
}
if ('string' === typeof input) {
file.path = input;
} else {
// Object.keys(input).forEach(function (k) {
// file[k] = input[k];
// });
// above will create a new object so that modification on file here will not affect
// input as a good lib should do, but since react-native-filereader can not be a
// polyfill as described in READEM.md and I still want as simple as possible, I do
// not want to `new File()` into `StateLoader.uploadGif()` in
// https://github.com/flyskywhy/PixelShapeRN/blob/v1.1.21/src/components/apptoolbox/Apptoolbox.js
// and need port https://github.com/node-file-api/File/blob/master/File.js as well,
// so I use below to reassign input to file so that I can just add
// `import FileReader from 'react-native-filereader'` in
// https://github.com/flyskywhy/PixelShapeRN/blob/v1.1.21/src/libs/GifLoader.js
// (and thus avoid crash in PixelShapeRN)
file = input;
}
if (!file.path) {
if (file.url) {
file.path = file.url;
} else if (file.uri) {
file.path = file.uri;
}
}
if (file.path) {
// iOS need this otherwise startsWith "file://" will
// `failed to stat path "file:///private/var/mobile/Containers/..."
// because it does not exist or it is not a folder` as err.message, ref to
// https://stackoverflow.com/a/68825349
let path = file.path.replace(/^file:\/\//, '');
// iOS need this otherwise file name contains spaces will
// `failed to stat path ".../some%20(7).gif"
// because it does not exist or it is not a folder` as err.message, ref to
// https://github.com/RonRadtke/react-native-blob-util/issues/117
path = decodeURIComponent(path);
fs.stat(path).then(function(stat) {
file.lastModified = stat.lastModified;
file.lastModifiedDate = new Date(stat.lastModified);
file.name = stat.filename;
file.path = stat.path;
file.size = stat.size;
file.type = mime.lookup(stat.filename);
_readFile(file, format, encoding);
}, function(err) {
console.warn('react-native-filereader error, maybe you forget request permission: ' + err.message);
return;
});
} else if (file.name) {
if (file.buffer) {
file.size = file.buffer.length;
} else if (!file.stream) {
throw new Error('react-native-filereader error: No input, nor stream, nor buffer.');
}
file.type = file.type || mime.lookup(file.name);
_readFile(file, format, encoding);
} else {
throw new Error('react-native-filereader error: No path, nor name.');
}
}
self.readAsArrayBuffer = function (file) {
readFile(file, 'buffer');
};
self.readAsBinaryString = function (file) {
readFile(file, 'binary');
};
self.readAsDataURL = function (file) {
readFile(file, 'dataUrl');
};
self.readAsText = function (file, encoding) {
readFile(file, 'text', encoding);
};
}
module.exports = FileReader;
}());