diff --git a/framerail/src/lib/server/deepwell/file.ts b/framerail/src/lib/server/deepwell/file.ts new file mode 100644 index 0000000000..adfb0d01a1 --- /dev/null +++ b/framerail/src/lib/server/deepwell/file.ts @@ -0,0 +1,34 @@ +import { client } from "$lib/server/deepwell" + +export async function getFileByHash( + /** Either a Uint8Array or a hex string */ + fileHash: Uint8Array | string +): Promise { + let res = await client.request( + "blob_get", + typeof fileHash === "string" ? fileHash : Buffer.from(fileHash).toString("hex") + ) + + return new Blob([new Uint8Array(res.data)], { type: res.mime }) +} + +export async function startBlobUpload(userId: number, blobSize: number) { + return await client.request("blob_upload", { + user_id: userId, + blob_size: blobSize + }) +} + +export async function cancelBlobUpload(userId: number, pendingBlobId: string) { + return await client.request("blob_cancel", { + user_id: userId, + pending_blob_id: pendingBlobId + }) +} + +export async function uploadToPresignUrl(url: string, file: File) { + return await fetch(url, { + method: "PUT", + body: file + }) +} diff --git a/framerail/src/lib/server/deepwell/getFile.ts b/framerail/src/lib/server/deepwell/getFile.ts deleted file mode 100644 index 6f15712051..0000000000 --- a/framerail/src/lib/server/deepwell/getFile.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { client } from "$lib/server/deepwell" - -export async function getFileByHash( - /** Either a Uint8Array or a hex string */ - fileHash: Uint8Array | string -): Promise { - let res = await client.request( - "blob_get", - typeof fileHash === "string" ? fileHash : Buffer.from(fileHash).toString("hex") - ) - - return new Blob([new Uint8Array(res.data)], { type: res.mime }) -} diff --git a/framerail/src/lib/server/deepwell/user.ts b/framerail/src/lib/server/deepwell/user.ts index 833d3339bc..e103813259 100644 --- a/framerail/src/lib/server/deepwell/user.ts +++ b/framerail/src/lib/server/deepwell/user.ts @@ -1,5 +1,6 @@ import { client } from "$lib/server/deepwell" import type { Optional } from "$lib/types" +import { startBlobUpload, uploadToPresignUrl } from "./file" export async function userView( domain: string, @@ -51,9 +52,10 @@ export async function userEdit( if (Array.isArray(params.locales) && params.locales.every((v) => typeof v === "string")) data.locales = params.locales if (params.avatar instanceof File && params.avatar.type.startsWith("image/")) { - let srcBuf = await params.avatar.arrayBuffer() - data.avatar = Buffer.from(srcBuf).toString("hex") - } + let presign = await startBlobUpload(userId, params.avatar.size) + await uploadToPresignUrl(presign.presign_url, params.avatar) + data.avatar_uploaded_blob_id = presign.pending_blob_id + } else if (params.avatar !== undefined && params.avatar === null) data.avatar = null return client.request("user_edit", { user: userId, diff --git a/framerail/src/lib/server/load/user.ts b/framerail/src/lib/server/load/user.ts index 4bfd799d20..ddec6cde66 100644 --- a/framerail/src/lib/server/load/user.ts +++ b/framerail/src/lib/server/load/user.ts @@ -1,6 +1,6 @@ import defaults from "$lib/defaults" import { parseAcceptLangHeader } from "$lib/locales" -import { getFileByHash } from "$lib/server/deepwell/getFile" +import { getFileByHash } from "$lib/server/deepwell/file" import { translate } from "$lib/server/deepwell/translate" import { userView } from "$lib/server/deepwell/user" import type { TranslateKeys } from "$lib/types" diff --git a/framerail/src/routes/[x+2d]/user/+page.svelte b/framerail/src/routes/[x+2d]/user/+page.svelte index 0293782a5d..f5a38e1467 100644 --- a/framerail/src/routes/[x+2d]/user/+page.svelte +++ b/framerail/src/routes/[x+2d]/user/+page.svelte @@ -2,6 +2,7 @@ import Page from "./[slug]/page.svelte" import { page } from "$app/stores" import { useErrorPopup } from "$lib/stores" + import { invalidateAll } from "$app/navigation" let showErrorPopup = useErrorPopup() let isEdit = false @@ -26,7 +27,7 @@ }) } else { isEdit = false - $page.data.user = res + invalidateAll() } }