2022-10-10 12:50:21 +00:00
|
|
|
export function base64Decode(s: string): Uint8Array {
|
|
|
|
const binString = atob(s);
|
|
|
|
const len = binString.length;
|
|
|
|
const bytes = new Uint8Array(len);
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
bytes[i] = binString.charCodeAt(i);
|
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function base64Encode(buffer: Uint8Array): string {
|
|
|
|
let binary = "";
|
|
|
|
const len = buffer.byteLength;
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
binary += String.fromCharCode(buffer[i]);
|
|
|
|
}
|
|
|
|
return btoa(binary);
|
|
|
|
}
|
2022-10-19 07:52:29 +00:00
|
|
|
|
|
|
|
export function base64EncodedDataUrl(
|
|
|
|
mimeType: string,
|
|
|
|
buffer: Uint8Array,
|
|
|
|
): string {
|
|
|
|
return `data:${mimeType};base64,${base64Encode(buffer)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function base64DecodeDataUrl(dataUrl: string): Uint8Array {
|
|
|
|
const b64Encoded = dataUrl.split(",", 2)[1];
|
|
|
|
return base64Decode(b64Encoded);
|
|
|
|
}
|