Blobify images so users can open them easier
This commit is contained in:
@@ -26,6 +26,22 @@
|
|||||||
const key = getTagValue("decryption-key", meta)
|
const key = getTagValue("decryption-key", meta)
|
||||||
const nonce = getTagValue("decryption-nonce", meta)
|
const nonce = getTagValue("decryption-nonce", meta)
|
||||||
const algorithm = getTagValue("encryption-algorithm", meta)
|
const algorithm = getTagValue("encryption-algorithm", meta)
|
||||||
|
const mime = getTagValue("m", meta)
|
||||||
|
const fileName =
|
||||||
|
getTagValue("filename", meta) ||
|
||||||
|
getTagValue("name", meta) ||
|
||||||
|
decodeURIComponent(new URL(url).pathname.split("/").filter(Boolean).at(-1) || "image")
|
||||||
|
|
||||||
|
const revokeSrc = () => {
|
||||||
|
if (src.startsWith("blob:")) {
|
||||||
|
URL.revokeObjectURL(src)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const setBlobSrc = (data: Blob | Uint8Array<ArrayBuffer>, type?: string) => {
|
||||||
|
revokeSrc()
|
||||||
|
src = URL.createObjectURL(new File([data], fileName, type ? {type} : undefined))
|
||||||
|
}
|
||||||
|
|
||||||
const onError = once(async () => {
|
const onError = once(async () => {
|
||||||
// If the image failed to load, try authenticating
|
// If the image failed to load, try authenticating
|
||||||
@@ -36,7 +52,8 @@
|
|||||||
const res = await getBlob(server, hash, {authEvent})
|
const res = await getBlob(server, hash, {authEvent})
|
||||||
|
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
src = URL.createObjectURL(await res.blob())
|
const blob = await res.blob()
|
||||||
|
setBlobSrc(blob, blob.type || undefined)
|
||||||
} else {
|
} else {
|
||||||
hasError = true
|
hasError = true
|
||||||
}
|
}
|
||||||
@@ -57,7 +74,7 @@
|
|||||||
const ciphertext = new Uint8Array(await response.arrayBuffer())
|
const ciphertext = new Uint8Array(await response.arrayBuffer())
|
||||||
const decryptedData = await decryptFile({ciphertext, key, nonce, algorithm})
|
const decryptedData = await decryptFile({ciphertext, key, nonce, algorithm})
|
||||||
|
|
||||||
src = URL.createObjectURL(new Blob([new Uint8Array(decryptedData)]))
|
setBlobSrc(new Uint8Array(decryptedData), mime)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
src = url
|
src = url
|
||||||
@@ -65,7 +82,7 @@
|
|||||||
})
|
})
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
URL.revokeObjectURL(src)
|
revokeSrc()
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user