Skip to content

Commit

Permalink
Fix presign file upload for user avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
Zokhoi committed Nov 23, 2024
1 parent c7dca20 commit b17bb8c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
34 changes: 34 additions & 0 deletions framerail/src/lib/server/deepwell/file.ts
Original file line number Diff line number Diff line change
@@ -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<Blob> {
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
})
}
13 changes: 0 additions & 13 deletions framerail/src/lib/server/deepwell/getFile.ts

This file was deleted.

8 changes: 5 additions & 3 deletions framerail/src/lib/server/deepwell/user.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion framerail/src/lib/server/load/user.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
3 changes: 2 additions & 1 deletion framerail/src/routes/[x+2d]/user/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,7 +27,7 @@
})
} else {
isEdit = false
$page.data.user = res
invalidateAll()
}
}
</script>
Expand Down

0 comments on commit b17bb8c

Please sign in to comment.