From 80fa41c8fbfaf528d13dded712fb3fae8db89d04 Mon Sep 17 00:00:00 2001 From: Flemmli97 Date: Thu, 2 Jan 2025 18:00:06 +0100 Subject: [PATCH] update wasm --- .../messaging/AttachmentRenderer.svelte | 2 +- .../messaging/PinnedMessages.svelte | 2 +- src/lib/utils/Functions.ts | 4 ++++ src/lib/wasm/ConstellationStore.ts | 22 +++---------------- src/lib/wasm/RaygunStore.ts | 15 +++++++------ src/routes/chat/+page.svelte | 6 ++--- src/routes/files/+page.svelte | 16 ++++---------- 7 files changed, 24 insertions(+), 43 deletions(-) diff --git a/src/lib/components/messaging/AttachmentRenderer.svelte b/src/lib/components/messaging/AttachmentRenderer.svelte index 63127f522..22f261395 100644 --- a/src/lib/components/messaging/AttachmentRenderer.svelte +++ b/src/lib/components/messaging/AttachmentRenderer.svelte @@ -13,7 +13,7 @@ export let contextBuilder: (attachments: Attachment) => ContextItem[] async function download_attachment(message: string, attachment: Attachment) { - await RaygunStoreInstance.downloadAttachment(chatID, message, attachment.name, attachment.size) + await RaygunStoreInstance.downloadAttachment(chatID, message, attachment.name) } const dispatch = createEventDispatcher() const dispatcher = (event: string, detail: string) => { diff --git a/src/lib/components/messaging/PinnedMessages.svelte b/src/lib/components/messaging/PinnedMessages.svelte index dcb268ffe..ce2e8c53c 100644 --- a/src/lib/components/messaging/PinnedMessages.svelte +++ b/src/lib/components/messaging/PinnedMessages.svelte @@ -19,7 +19,7 @@ export let top: number | string = "var(--padding)" async function download_attachment(message: string, attachment: Attachment) { - await RaygunStoreInstance.downloadAttachment(chatID, message, attachment.name, attachment.size) + await RaygunStoreInstance.downloadAttachment(chatID, message, attachment.name) } async function unpin(message: string) { diff --git a/src/lib/utils/Functions.ts b/src/lib/utils/Functions.ts index 3f43c72a4..5c05d821a 100644 --- a/src/lib/utils/Functions.ts +++ b/src/lib/utils/Functions.ts @@ -104,6 +104,10 @@ export async function shareFile(fileName: string, combinedArray: Buffer) { export async function downloadFileFromWeb(data: any[], size: number, name: string) { let options: { size?: number; type?: string } = { size } let blob = new File([new Uint8Array(data)], name, { type: options?.type }) + downloadBlobFromWeb(blob, name) +} + +export async function downloadBlobFromWeb(blob: Blob, name: string) { const elem = window.document.createElement("a") elem.href = window.URL.createObjectURL(blob) elem.download = name diff --git a/src/lib/wasm/ConstellationStore.ts b/src/lib/wasm/ConstellationStore.ts index 205d9fb2a..db8e29817 100644 --- a/src/lib/wasm/ConstellationStore.ts +++ b/src/lib/wasm/ConstellationStore.ts @@ -288,28 +288,12 @@ class ConstellationStore { return regex.test(path) } - async downloadFile(fileName: string): Promise> { + async downloadFile(fileName: string): Promise> { const constellation = get(this.constellationWritable) if (constellation) { try { - let get_stream_async_iterator = await constellation.get_stream(fileName) - let get_stream = { - [Symbol.asyncIterator]() { - return get_stream_async_iterator - }, - } - - const chunks = [] - try { - for await (const value of get_stream) { - if (value.Ok != null) { - chunks.push(Buffer.from(value.Ok)) - } - } - } finally { - const combinedArray = Buffer.concat(chunks) - return success(combinedArray) - } + let get_stream = await constellation.get_stream(fileName) + return success(new Response(get_stream)) } catch (error) { return failure(handleErrors(error)) } diff --git a/src/lib/wasm/RaygunStore.ts b/src/lib/wasm/RaygunStore.ts index d0b1fa8ea..dc7dfc93a 100644 --- a/src/lib/wasm/RaygunStore.ts +++ b/src/lib/wasm/RaygunStore.ts @@ -20,7 +20,7 @@ import { ToastMessage } from "$lib/state/ui/toast" import { page } from "$app/stores" import { goto } from "$app/navigation" import { isAndroidOriOS } from "$lib/utils/Mobile" -import { downloadFileFromWeb, shareFile } from "$lib/utils/Functions" +import { downloadBlobFromWeb, shareFile } from "$lib/utils/Functions" const MAX_PINNED_MESSAGES = 100 export type FetchMessagesConfig = @@ -361,22 +361,23 @@ class RaygunStore { return await this.get(r => r.edit(conversation_id, message_id, message), "Error editing message") } - async downloadAttachment(conversation_id: string, message_id: string, file: string, size?: number) { + async downloadAttachment(conversation_id: string, message_id: string, file: string) { return await this.get(async r => { let result = await r.download_stream(conversation_id, message_id, file) - let data = await createFileDownloadHandler(file, result, size) + let res = new Response(result) if (isAndroidOriOS()) { - await shareFile(file, Buffer.from(data)) + await shareFile(file, Buffer.from(await res.arrayBuffer())) } else { - await downloadFileFromWeb(data, size || 0, file) + let blob = await res.blob() + await downloadBlobFromWeb(blob, file) } }, `Error downloading attachment from ${conversation_id} for message ${message_id}`) } - async getAttachmentRaw(conversation_id: string, message_id: string, file: string, options?: { size?: number; type?: string }) { + async getAttachmentRaw(conversation_id: string, message_id: string, file: string) { return await this.get(async r => { let result = await r.download_stream(conversation_id, message_id, file) - return createFileDownloadHandlerRaw(file, result, options) + return new Response(result) }, `Error downloading attachment from ${conversation_id} for message ${message_id}`) } diff --git a/src/routes/chat/+page.svelte b/src/routes/chat/+page.svelte index ab6f31ecc..37a1162ad 100644 --- a/src/routes/chat/+page.svelte +++ b/src/routes/chat/+page.svelte @@ -285,12 +285,12 @@ async function copyFile(message: string, attachment: Attachment) { if (attachment.kind !== MessageAttachmentKind.Image) return - let result = await RaygunStoreInstance.getAttachmentRaw($conversation!.id, message, attachment.name, { size: attachment.size, type: "image/png" }) - result.onSuccess(async blob => { + let result = await RaygunStoreInstance.getAttachmentRaw($conversation!.id, message, attachment.name) + result.onSuccess(async res => { try { await navigator.clipboard.write([ new ClipboardItem({ - [blob.type]: blob, + [res.type]: await res.blob(), }), ]) $clipboardWrite = Permission.ALLOWED diff --git a/src/routes/files/+page.svelte b/src/routes/files/+page.svelte index 850db81b0..b73967c45 100644 --- a/src/routes/files/+page.svelte +++ b/src/routes/files/+page.svelte @@ -26,7 +26,7 @@ import path from "path" import { MultipassStoreInstance } from "$lib/wasm/MultipassStore" import { isAndroidOriOS } from "$lib/utils/Mobile" - import { shareFile } from "$lib/utils/Functions" + import { downloadBlobFromWeb, shareFile } from "$lib/utils/Functions" export let browseFilesForChatMode: boolean = false @@ -458,20 +458,12 @@ err => { Store.addToastNotification(new ToastMessage("", err, 2)) }, - async combinedArray => { + async res => { if (isAndroidOriOS()) { - await shareFile(fileName, combinedArray) + await shareFile(fileName, Buffer.from(await res.arrayBuffer())) return } - const blob = new Blob([new Uint8Array(combinedArray)], { type: "application/octet-stream" }) - const url = URL.createObjectURL(blob) - const a = document.createElement("a") - a.href = url - a.download = path.basename(fileName) - document.body.appendChild(a) - a.click() - document.body.removeChild(a) - URL.revokeObjectURL(url) + downloadBlobFromWeb(await res.blob(), path.basename(fileName)) } ) }