-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileProcessor.js
More file actions
76 lines (66 loc) · 2.13 KB
/
FileProcessor.js
File metadata and controls
76 lines (66 loc) · 2.13 KB
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
/**
* Download a string to a file
*/
function downloadFile(content, filename, mimetype) {
let a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([content], {type: mimetype}));
a.download = filename;
// Append anchor to body.
document.body.appendChild(a);
a.click();
// Remove anchor from body
document.body.removeChild(a);
}
/**
* Read a local file in chunks, called onChunk(), onFinish() and onError()
* when appropriate.
*/
function processLocalFileInChunks(file, onChunk, onFinish, onError, chunksize=65536) {
let filesize = file.size
let offset = 0;
let currentChunk = 0;
let nchunks = filesize / chunksize;
// Read chunk-wise https://stackoverflow.com/a/28318964/2597135
// This will read a chunk from the file.
let chunkReaderBlock = function(_offset, length, _file) {
let reader = new FileReader();
let blob = _file.slice(_offset, length + _offset);
reader.onload = readEventHandler;
reader.readAsText(blob);
}
/*
* This is called once a chunk has been read.
* It will let onChunk() process the chunk,
* and then read the next chunk, if any
*/
let readEventHandler = function(evt) {
if (evt.target.error == null) { // if no error occured during reading
offset += evt.target.result.length;
currentChunk++;
// Process chunk
onChunk(evt.target.result, currentChunk, nchunks)
} else { // Error occured during reading
onError(evt.target.error)
return;
}
if (offset >= filesize) {
// Finished
onFinish()
} else {
// Read next chunk
chunkReaderBlock(offset, chunksize, file);
}
}
// Start reading the first chunk
chunkReaderBlock(offset, chunksize, file);
}
/*
* Read a local file in as a huge string
* when appropriate.
*/
function processLocalFile(file, onFinish, onError) {
var reader = new FileReader();
reader.onload = () => onFinish(reader.result)
reader.onerror = onError
reader.readAsText(file);
}