aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/utilities/base64.ts
blob: 6eece9796ee8bd576cd25fe42751b9593135a717 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function bytesToBase64(bytes: Uint8Array): string {
  const binString = Array.from(bytes, (x) => String.fromCodePoint(x)).join("");
  return btoa(binString);
}

export default function base64(
  data: Blob | Uint8Array | string,
): Promise<string> {
  if (typeof data === "string") {
    // From https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
    const binString = new TextEncoder().encode(data);
    return Promise.resolve(bytesToBase64(binString));
  }

  if (data instanceof Uint8Array) {
    return Promise.resolve(bytesToBase64(data));
  }

  return new Promise<string>((resolve) => {
    const reader = new FileReader();
    reader.onload = function () {
      resolve((reader.result as string).replace(/^data:.*;base64,/, ""));
    };
    reader.readAsDataURL(data);
  });
}