Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit 238b7979 authored by Manu Suresh's avatar Manu Suresh
Browse files

Show hash verification progress to user

parent 72544b0b
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -123,6 +123,9 @@ export class Controller {
            (loaded, total, name) => {
              this.view.onUnzip(name, loaded, total);
            },
            (loaded, total, name) => {
              this.view.onVerify(name, loaded, total);
            },
          );
          this.view.onDownloadingEnd();
          return true;
+2 −1
Original line number Diff line number Diff line
@@ -185,13 +185,14 @@ export class DeviceManager {
    }
  }

  async downloadAll(onProgress, onUnzip) {
  async downloadAll(onProgress, onUnzip, onVerify) {
    try {
      await this.downloader.downloadAndUnzipFolder(
        this.files,
        this.folder,
        onProgress,
        onUnzip,
        onVerify,
      );
    } catch (e) {
      throw new Error(`downloadAll error ${e.message || e}`);
+11 −4
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ export class Downloader {
    folder,
    onDownloadProgress,
    onUnzipProgress,
    onVerifyProgress,
  ) {
    let current_file;
    try {
@@ -55,7 +56,9 @@ export class Downloader {
              const expected = await this.fetchChecksum(
                `${file.path}.sha256sum`,
              );
              const actual = await this.computeSha256(blob);
              const actual = await this.computeSha256(blob, (loaded, total) => {
                onVerifyProgress(loaded, total, file.name)
              });
              if (expected && actual !== expected) {
                throw new Error(
                  `Checksum mismatch for ${file.name}: expected ${expected} got ${actual}`,
@@ -91,7 +94,9 @@ export class Downloader {
              if (filesRequired.includes(filename)) {
                await this.setInDBStore(unzippedEntry.blob, filename);
                this.stored[filename] = true;
                const fileSHA = await this.computeSha256(unzippedEntry.blob);
                const fileSHA = await this.computeSha256(unzippedEntry.blob, (loaded, total) => {
                  onVerifyProgress(loaded, total, filename)
                });
                console.log(`File: ${unzippedEntry.name} SHA256: ${fileSHA}`);
              }
            }
@@ -155,16 +160,18 @@ export class Downloader {
   * Streaming SHA-256 computation for large files.
   * Processes blob in 16MB chunks to avoid memory limits on low-memory devices.
   */
  async computeSha256(blob) {
  async computeSha256(blob, onVerifyProgress) {
    const CHUNK_SIZE = 16 * 1024 * 1024; // 16MB chunks
    const hasher = await createSHA256();
    const blobSize = blob.size;
    hasher.init();

    let offset = 0;
    while (offset < blob.size) {
    while (offset < blobSize) {
      const chunk = blob.slice(offset, offset + CHUNK_SIZE);
      const buffer = await chunk.arrayBuffer();
      hasher.update(new Uint8Array(buffer));
      onVerifyProgress(offset, blobSize);
      offset += CHUNK_SIZE;
    }

+15 −0
Original line number Diff line number Diff line
@@ -145,6 +145,21 @@ export default class ViewManager {
    this.WDebug.log(`Downloading ${name}: ${v}/${100}`, `downloading-${name}`);
  }

  onVerify(name, loaded, total) {
    const v = Math.round((loaded / total) * 100);
    let $progressBar = document.querySelector(
      `.active .downloading-progress-bar`,
    );
    let $progress = document.querySelector(`.active .downloading-progress`);
    if ($progressBar) {
      $progressBar.value = v;
    }
    if ($progress) {
      $progress.innerText = `Verifying ${name}: ${v}/${100}`;
    }
    this.WDebug.log(`Verifying ${name}: ${v}/${100}`, `verifying-${name}`);
  }

  onUnzip(name, loaded, total) {
    const v = Math.round((loaded / total) * 100);
    let $progressBar = document.querySelector(