feat: user avatars

This commit is contained in:
Radical 2025-05-23 20:33:58 +02:00
parent d6364a0dc0
commit 97072d54d1
4 changed files with 65 additions and 13 deletions

View file

@ -1,4 +1,5 @@
use actix_web::{get, patch, web, HttpRequest, HttpResponse};
use actix_multipart::form::{json::Json as MpJson, tempfile::TempFile, MultipartForm};
use serde::Deserialize;
use crate::{error::Error, structs::Me, api::v1::auth::check_access_token, utils::get_auth_header, Data};
@ -18,7 +19,7 @@ pub async fn res(req: HttpRequest, data: web::Data<Data>) -> Result<HttpResponse
Ok(HttpResponse::Ok().json(me))
}
#[derive(Deserialize)]
#[derive(Debug, Deserialize)]
struct NewInfo {
username: Option<String>,
display_name: Option<String>,
@ -26,8 +27,15 @@ struct NewInfo {
email: Option<String>,
}
#[derive(Debug, MultipartForm)]
struct UploadForm {
#[multipart(limit = "100MB")]
avatar: Option<TempFile>,
json: Option<MpJson<NewInfo>>,
}
#[patch("/me")]
pub async fn update(req: HttpRequest, new_info: web::Json<NewInfo>, data: web::Data<Data>) -> Result<HttpResponse, Error> {
pub async fn update(req: HttpRequest, MultipartForm(form): MultipartForm<UploadForm>, data: web::Data<Data>) -> Result<HttpResponse, Error> {
let headers = req.headers();
let auth_header = get_auth_header(headers)?;
@ -36,22 +44,32 @@ pub async fn update(req: HttpRequest, new_info: web::Json<NewInfo>, data: web::D
let uuid = check_access_token(auth_header, &mut conn).await?;
let me = Me::get(&mut conn, uuid).await?;
let mut me = Me::get(&mut conn, uuid).await?;
if let Some(username) = &new_info.username {
todo!();
if let Some(avatar) = form.avatar {
let bytes = tokio::fs::read(avatar.file).await?;
let byte_slice: &[u8] = &bytes;
me.set_avatar(&data.bunny_cdn, &mut conn, data.config.bunny.cdn_url.clone(), byte_slice.into()).await?;
}
if let Some(display_name) = &new_info.display_name {
todo!();
}
if let Some(new_info) = form.json {
if let Some(username) = &new_info.username {
todo!();
}
if let Some(password) = &new_info.password {
todo!();
}
if let Some(display_name) = &new_info.display_name {
todo!();
}
if let Some(email) = &new_info.email {
todo!();
if let Some(password) = &new_info.password {
todo!();
}
if let Some(email) = &new_info.email {
todo!();
}
}
Ok(HttpResponse::Ok().finish())

View file

@ -9,6 +9,7 @@ pub fn web() -> Scope {
web::scope("/users")
.service(res)
.service(me::res)
.service(me::update)
.service(uuid::res)
}