大文件上传之多线程md5文件加密

96 阅读1分钟
import type { UploadRawFile } from "element-plus";
import md5 from "md5";
const chunkSize = 1024 * 1024 * 10; //设置分片大小为10m
const upload = async (raw: UploadRawFile) => {
  const chunkCount = Math.ceil(raw.size / chunkSize); //计算总分片数量
  const result: any = [];
  const threadChunk = Math.ceil(chunkCount / 6); //对每个work的分片数量
  let threadIsEnd = 0; //完成的work数量
  console.time("计算");
  // for (let i = 0; i < chunkCount; i++) {
  //   const res = await createChunk(raw, i);
  //   result.push(res);
  // }
  for (let i = 0; i < 6; i++) {
  //创建线程
    const worker = new Worker(new URL("./worker/MD5.ts", import.meta.url), {
      type: "module",
    }); 
    //判断是否为最后一个线程,为最后一个线程取chunkCount为结尾 
    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; //通过index去排序
      });
      worker.terminate();
      threadIsEnd++;

      if (threadIsEnd === 6) {
        console.timeEnd("计算");
        console.log(result);
      }
    };
  }
  // console.timeEnd("计算");
  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);
};