* Reads a File and returns the data as a Uint8Array. * * @param {File | for node: array|arrayBuffer|buffer|string} file * @returns {Uint8Array} * * @example * // returns Uint8Array(5) [104, 101, 108, 108, 111] * await Utils.readFile(new File(["hello"], "test"))
(file)
| 1181 | * await Utils.readFile(new File(["hello"], "test")) |
| 1182 | */ |
| 1183 | static readFile(file) { |
| 1184 | |
| 1185 | if (isNodeEnvironment()) { |
| 1186 | return Buffer.from(file).buffer; |
| 1187 | |
| 1188 | } else { |
| 1189 | return new Promise((resolve, reject) => { |
| 1190 | const reader = new FileReader(); |
| 1191 | const data = new Uint8Array(file.size); |
| 1192 | let offset = 0; |
| 1193 | const CHUNK_SIZE = 10485760; // 10MiB |
| 1194 | |
| 1195 | const seek = function() { |
| 1196 | if (offset >= file.size) { |
| 1197 | resolve(data); |
| 1198 | return; |
| 1199 | } |
| 1200 | const slice = file.slice(offset, offset + CHUNK_SIZE); |
| 1201 | reader.readAsArrayBuffer(slice); |
| 1202 | }; |
| 1203 | |
| 1204 | reader.onload = function(e) { |
| 1205 | data.set(new Uint8Array(reader.result), offset); |
| 1206 | offset += CHUNK_SIZE; |
| 1207 | seek(); |
| 1208 | }; |
| 1209 | |
| 1210 | reader.onerror = function(e) { |
| 1211 | reject(reader.error.message); |
| 1212 | }; |
| 1213 | |
| 1214 | seek(); |
| 1215 | }); |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | /** |
| 1220 | * Synchronously read the raw data from a File object. |
no test coverage detected