|
@@ -0,0 +1,34 @@
|
|
|
+const CHUNK_SIZE = 1024 * 1024 * 5;
|
|
|
+const THREAD_COUNT = navigator.hardwareConcurrency || 4;
|
|
|
+
|
|
|
+export async function cutFile(file){
|
|
|
+ new Promise((resolve) => {
|
|
|
+ const chunkCount = Math.ceil(file.size / CHUNK_SIZE);
|
|
|
+ const threadChunkCount = Math.ceil(chunkCount / THREAD_COUNT);
|
|
|
+ let result = [], finishCount = 0;
|
|
|
+ for(let i = 0; i < chunkCount; i++){
|
|
|
+ const worker = new Worker("./worker.js", {
|
|
|
+ type: "module"
|
|
|
+ });
|
|
|
+ let start = i * threadChunkCount;
|
|
|
+ let end = (i + 1 ) * threadChunkCount;
|
|
|
+ if(end > chunkCount) end = chunkCount;
|
|
|
+ worker.postMessage({
|
|
|
+ file,
|
|
|
+ chunkSize: CHUNK_SIZE,
|
|
|
+ startChunkIndex: start,
|
|
|
+ endChunkIndex: end
|
|
|
+ })
|
|
|
+ worker.onmessage = e => {
|
|
|
+ for(let i = start; i< end; i++){
|
|
|
+ result[i] = e.data[i - start]
|
|
|
+ }
|
|
|
+ worker.terminate();
|
|
|
+ finishCount++;
|
|
|
+ if(finishCount === THREAD_COUNT){
|
|
|
+ resolve(result)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|