Skip to content

Commit

Permalink
update wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
Flemmli97 committed Jan 2, 2025
1 parent 8cfbc5e commit 80fa41c
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/lib/components/messaging/AttachmentRenderer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/messaging/PinnedMessages.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/utils/Functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 3 additions & 19 deletions src/lib/wasm/ConstellationStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,28 +288,12 @@ class ConstellationStore {
return regex.test(path)
}

async downloadFile(fileName: string): Promise<Result<WarpError, Buffer>> {
async downloadFile(fileName: string): Promise<Result<WarpError, Response>> {
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))
}
Expand Down
15 changes: 8 additions & 7 deletions src/lib/wasm/RaygunStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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}`)
}

Expand Down
6 changes: 3 additions & 3 deletions src/routes/chat/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 4 additions & 12 deletions src/routes/files/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
)
}
Expand Down

0 comments on commit 80fa41c

Please sign in to comment.