update error messages in GET /images/<image>

This commit is contained in:
SadlyNotSappho 2024-03-22 11:24:11 -07:00
parent 085878f9f8
commit 92671eb0c8
1 changed files with 17 additions and 5 deletions

View File

@ -153,6 +153,7 @@ async fn login(
} }
} }
} }
#[post("/logout")] #[post("/logout")]
async fn logout(cookies: &CookieJar<'_>) -> status::Custom<&'static str> { async fn logout(cookies: &CookieJar<'_>) -> status::Custom<&'static str> {
match cookies.get_private("token") { match cookies.get_private("token") {
@ -323,7 +324,10 @@ async fn toggleperms(
} }
#[get("/images/<image>")] #[get("/images/<image>")]
async fn get_image(image: String, mut db: Connection<Db>) -> Result<NamedFile, Status> { async fn get_image(
image: String,
mut db: Connection<Db>,
) -> Result<NamedFile, status::Custom<&'static str>> {
let mut split = image.split('.').collect::<Vec<&str>>(); let mut split = image.split('.').collect::<Vec<&str>>();
let format = split.pop().unwrap(); let format = split.pop().unwrap();
let image = split.join("."); let image = split.join(".");
@ -351,13 +355,21 @@ async fn get_image(image: String, mut db: Connection<Db>) -> Result<NamedFile, S
Ok(file) Ok(file)
} }
Err(why) => Err(match &why.to_string()[..] { Err(why) => Err(match &why.to_string()[..] {
"Format error decoding Png: Invalid PNG signature." => Status::NotAcceptable, "Format error decoding Png: Invalid PNG signature." => {
_ => Status::InternalServerError, status::Custom(Status::NotAcceptable, "That file isn't an image.")
}
_ => status::Custom(Status::InternalServerError, "Unknown decoding error"),
}), }),
}, },
Err(_) => Err(Status::NotFound), Err(_) => Err(status::Custom(
Status::InternalServerError,
"Unknown decoding error",
)),
}, },
Err(_) => Err(Status::ImATeapot), Err(_) => Err(status::Custom(
Status::InternalServerError,
"File not found",
)),
} }
} }