From ac21b0ef0068f0821d7a665017e3364032ef4e14 Mon Sep 17 00:00:00 2001 From: SadlyNotSappho Date: Tue, 26 Mar 2024 11:59:52 -0700 Subject: [PATCH] fix DELETE /images/ --- src/main.rs | 68 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index 93368d3..e405ff1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,21 +106,29 @@ struct GetUser { username: String, admin: bool, make_posts: bool, - comment: bool + comment: bool, } #[get("/account")] -async fn account(mut db: Connection, cookies: &CookieJar<'_>) -> status::Custom, &'static str>> { +async fn account( + mut db: Connection, + cookies: &CookieJar<'_>, +) -> status::Custom, &'static str>> { let token = cookies.get_private("token"); match token { Some(t) => match User::get_by_token(&mut db, t).await { Some(user) => status::Custom( Status::Ok, - Ok(Json(GetUser {username: user.username, admin: user.admin, make_posts: user.make_posts, comment: user.comment})), + Ok(Json(GetUser { + username: user.username, + admin: user.admin, + make_posts: user.make_posts, + comment: user.comment, + })), ), None => status::Custom(Status::NotFound, Err("User doesn't exist.")), }, - None => status::Custom(Status::Unauthorized, Err("Not logged in")), + None => status::Custom(Status::Unauthorized, Err("Not logged in")), } } @@ -458,29 +466,43 @@ async fn upload( } #[delete("/images/")] -pub async fn delete_image(mut db: Connection, cookies: &CookieJar<'_>, uuid: String) -> String { - let token = cookies.get_private("token"); - match token { - Some(t) => match User::get_by_token(&mut db, t).await { - Some(user) => match Image::is_owned_by(&mut db, &uuid, &user.username).await { +pub async fn delete_image( + mut db: Connection, + cookies: &CookieJar<'_>, + uuid: String, +) -> status::Custom<&'static str> { + match User::login_status(&mut db, cookies).await { + LoginStatus::LoggedIn(user) => { + match Image::is_owned_by(&mut db, &uuid, &user.username).await { Ok(b) => match b { - true => match Image::delete(&mut db, &uuid).await { - Ok(_) => match fs::remove_file(format!("/srv/images/{uuid}.png")) { - Ok(_) => "deleted!".to_string(), - Err(why) => { - eprintln!("{why:?}"); - "Image deleted from database but not filesystem".to_string() - } - }, - Err(_) => "Couldn't delete image".to_string(), + // yeah this is jank but fuck types i don't want to figure that out + true => delimg(&mut db, uuid).await, + false => match user.admin { + true => delimg(&mut db, uuid).await, + false => status::Custom(Status::Unauthorized, "You don't own that image"), }, - false => "You don't own that image".to_string(), }, - Err(_) => "Couldn't get image".to_string(), - }, - None => "Invalid login token".to_string(), + Err(_) => status::Custom(Status::NotFound, "Couldn't get image"), + } + } + LoginStatus::InvalidToken => status::Custom(Status::Unauthorized, "Invalid login token"), + LoginStatus::NotLoggedIn => status::Custom(Status::Unauthorized, "Not logged in"), + } +} + +async fn delimg(db: &mut Connection, uuid: String) -> status::Custom<&'static str> { + match Image::delete(db, &uuid).await { + Ok(_) => match fs::remove_file(format!("/srv/images/{uuid}.png")) { + Ok(_) => status::Custom(Status::Ok, "Image deleted"), + Err(why) => { + eprintln!("{why:?}"); + status::Custom( + Status::ImATeapot, + "Image deleted from database but not filesystem", + ) + } }, - None => "Not logged in".to_string(), + Err(_) => status::Custom(Status::InternalServerError, "Couldn't delete the image"), } }