1234567891011121314151617181920212223242526272829303132333435 |
- const CHUNK_SIZE = 1024 * 1024 * 5;
- const THREAD_COUNT = navigator.hardwareConcurrency || 4;
- export async function cutFile(file){
- return 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("node_modules/wxj-tools/tools/worker.js", {
- type: "module",
- name: "cutFile"
- });
- 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 === chunkCount){
- return resolve(result)
- }
- }
- }
- })
- }
|