1 | /** global: aesCrypt */ |
||
2 | |||
3 | /** |
||
4 | * read file as string, bytes (uint8Array), int (1 byte) |
||
5 | * |
||
6 | * |
||
7 | * @return The FileBytesReader object. |
||
8 | * |
||
9 | * @static |
||
10 | * |
||
11 | * @example |
||
12 | * var fileElement = document.getElementById('file').files[0]; |
||
13 | * var file = new FileBytesReader(fileElement); |
||
14 | * |
||
15 | * @param file a fileElement object |
||
16 | */ |
||
17 | |||
18 | (function () { |
||
19 | let LIB = aesCrypt; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
20 | aesCrypt.FileBytesReader = function(file) { |
||
21 | let _i = 0; |
||
22 | let _fileSize = file.size; |
||
23 | let _reader = new FileReader(); |
||
0 ignored issues
–
show
The variable
FileReader seems to be never declared. If this is a global, consider adding a /** global: FileReader */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
24 | let _file = file; |
||
25 | |||
26 | function readChunk(length) { |
||
27 | return new Promise((resolve, reject) => { |
||
28 | let blob = _file.slice(_i, _i += length); |
||
29 | |||
30 | _reader.onload = () => { |
||
31 | // return Uint8Array |
||
32 | resolve(new Uint8Array(_reader.result)); |
||
33 | }; |
||
34 | |||
35 | _reader.onerror = reject; |
||
36 | |||
37 | _reader.readAsArrayBuffer(blob); |
||
38 | }); |
||
39 | } |
||
40 | |||
41 | async function readBytes(length) { |
||
42 | if( _fileSize - _i < length ) { |
||
43 | throw ("File is corrupted."); |
||
44 | } |
||
45 | |||
46 | return await readChunk(length); |
||
47 | } |
||
48 | |||
49 | async function readByte() { |
||
50 | let bytes = await readBytes(1); |
||
51 | return bytes[0]; |
||
52 | } |
||
53 | |||
54 | function getCurrentPosition() { |
||
55 | return _i; |
||
56 | } |
||
57 | |||
58 | function getLength() { |
||
59 | return _fileSize; |
||
60 | } |
||
61 | |||
62 | return { |
||
63 | readByte, |
||
64 | readBytes, |
||
65 | getCurrentPosition, |
||
66 | getLength, |
||
67 | }; |
||
68 | }; |
||
69 | |||
70 | }()); |