import type { UploadRawFile } from "element-plus";
import md5 from "md5";
const chunkSize = 1024 * 1024 * 10;
const upload = async (raw: UploadRawFile) => {
const chunkCount = Math.ceil(raw.size / chunkSize);
const result: any = [];
const threadChunk = Math.ceil(chunkCount / 6);
let threadIsEnd = 0;
console.time("计算");
for (let i = 0; i < 6; i++) {
const worker = new Worker(new URL("./worker/MD5.ts", import.meta.url), {
type: "module",
});
let end = (i + 1) * threadChunk;
if (end > chunkCount) {
end = chunkCount;
}
worker.postMessage({
raw,
chunkSize,
statrChunkIndex: i * threadChunk,
endChunkIndex: end,
chunkCount,
});
worker.onmessage = (e) => {
e.data.forEach((item: any) => {
result[item.index] = item;
});
worker.terminate();
threadIsEnd++;
if (threadIsEnd === 6) {
console.timeEnd("计算");
console.log(result);
}
};
}
console.log(result);
return false;
};
const createChunk = async (raw: UploadRawFile, index: number) => {
const blob = raw.slice(index * chunkSize, (index + 1) * chunkSize);
const arrayBuffer = await blob.arrayBuffer();
const uni8 = new Uint8Array(arrayBuffer);
let end: number = 0;
end = (index + 1) * chunkSize;
if (raw.size < (index + 1) * chunkSize) {
end = raw.size;
}
const obj = {
blob: blob,
hash: md5(uni8),
index: index,
start: index * chunkSize,
end,
};
return obj;
};
import md5 from "md5";
onmessage = async (e) => {
const { raw, chunkSize, statrChunkIndex, endChunkIndex, chunkCount } = e.data;
const res: any = [];
for (let index = statrChunkIndex; index < endChunkIndex; index++) {
const blob = raw.slice(index * chunkSize, (index + 1) * chunkSize);
const arrayBuffer = await blob.arrayBuffer();
const uni8 = new Uint8Array(arrayBuffer);
let end: number = 0;
end = (index + 1) * chunkSize;
if (raw.size < (index + 1) * chunkSize) {
end = raw.size;
}
const obj = {
blob: blob,
hash: md5(uni8),
index: index,
start: index * chunkSize,
end,
};
res.push(obj);
}
postMessage(res);
};