From 7e68de5a17f51350345a131a0e5bc23f89b2635e Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Tue, 5 Mar 2024 23:24:41 -0500 Subject: [PATCH] [F] Fix profile picture upload api --- AquaNet/src/libs/sdk.ts | 8 +++++--- src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt | 9 ++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/AquaNet/src/libs/sdk.ts b/AquaNet/src/libs/sdk.ts index 566e0e5e..77bdb8c3 100644 --- a/AquaNet/src/libs/sdk.ts +++ b/AquaNet/src/libs/sdk.ts @@ -56,9 +56,6 @@ export async function post(endpoint: string, params: any, init?: RequestInitWith let res = await fetchWithParams(AQUA_HOST + endpoint, { method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, params, ...init }).catch(e => { @@ -123,6 +120,11 @@ export const USER = { post('/api/v2/user/keychip', {}).then(it => it.keychip), setting: (key: string, value: string) => post('/api/v2/user/setting', { key: key === 'password' ? 'pwHash' : key, value }), + uploadPfp: (file: File) => { + const formData = new FormData() + formData.append('file', file) + return post('/api/v2/user/upload-pfp', { }, { method: 'POST', body: formData }) + }, isLoggedIn, ensureLoggedIn, } diff --git a/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt b/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt index 9e1e2d1a..e0e29b2c 100644 --- a/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt +++ b/src/main/java/icu/samnyan/aqua/net/UserRegistrar.kt @@ -32,7 +32,7 @@ class UserRegistrar( val cardService: CardService, val validator: AquaUserServices, val emailProps: EmailProperties, - paths: PathProps + final val paths: PathProps ) { val portraitPath = paths.aquaNetPortrait.path() @@ -160,7 +160,10 @@ class UserRegistrar( @API("/me") @Doc("Get the information of the current logged-in user.", "User information") - suspend fun getUser(@RP token: Str) = jwt.auth(token) + suspend fun getUser(@RP token: Str) = jwt.auth(token).apply { + if (!profilePicture.isNullOrBlank()) + profilePicture = (portraitPath / profilePicture!!).toString() + } @API("/setting") @Doc("Validate and set a user setting field.", "Success message") @@ -195,7 +198,7 @@ class UserRegistrar( mapOf("keychip" to new) } - @API("/upload-pfp") + @API("/upload-pfp", consumes = ["multipart/form-data"]) @Doc("Upload a profile picture for the user.", "Success message") suspend fun uploadPfp(@RP token: Str, @RP file: MultipartFile) = jwt.auth(token) { u -> // Processing the image would lead to many open factors for attack