layout sharing via url

[deploy]
This commit is contained in:
2023-07-09 01:20:38 +02:00
parent 391c9d8837
commit 26a6f70ccb
9 changed files with 166 additions and 10 deletions

View File

@@ -0,0 +1,30 @@
/**
* Encodes a gzipped binary blob to a base64 string.
*
* Note that the string is url-compatible base64,
* meaning some chars are swapped for compatibility
*/
export async function toBase64(blob: Blob): Promise<string> {
return new Promise(async resolve => {
const reader = new FileReader()
reader.onloadend = function () {
resolve(
(reader.result as string)
.replace(/^data:application\/octet-stream;base64,/, "")
.replaceAll("+", ".")
.replaceAll("/", "_")
.replaceAll("=", "-"),
)
}
reader.readAsDataURL(blob)
})
}
export async function fromBase64(base64: string): Promise<Blob> {
return fetch(
`data:application/octet-stream;base64,${base64
.replaceAll(".", "+")
.replaceAll("_", "/")
.replaceAll("-", "=")}`,
).then(it => it.blob())
}