rewrite GET /account and add GET /myimages

This commit is contained in:
SadlyNotSappho 2024-03-26 11:43:57 -07:00
parent 3a5fb7bcd0
commit 4ffc45d5f7
3 changed files with 50 additions and 8 deletions

View File

@ -18,6 +18,8 @@ services:
environment: environment:
- ROCKET_DATABASES={fossil_postgres={url="postgres://postgres:$POSTGRES_PASSWORD@db/$POSTGRES_DB", max_connections=10, connect_timeout=10}} - ROCKET_DATABASES={fossil_postgres={url="postgres://postgres:$POSTGRES_PASSWORD@db/$POSTGRES_DB", max_connections=10, connect_timeout=10}}
- ROCKET_LIMITS={file=1GiB}
- ROCKET_ADDRESS=0.0.0.0 - ROCKET_ADDRESS=0.0.0.0
- ROCKET_PORT=$PORT - ROCKET_PORT=$PORT
- RUST_LOG=debug - RUST_LOG=debug

View File

@ -41,6 +41,11 @@ fn uploadimage() -> RawHtml<String> {
RawHtml(fs::read_to_string("/srv/web/uploadimage.html").unwrap()) RawHtml(fs::read_to_string("/srv/web/uploadimage.html").unwrap())
} }
#[get("/myimages")]
fn myimages() -> RawHtml<String> {
RawHtml(fs::read_to_string("/srv/web/myimages.html").unwrap())
}
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(crate = "rocket::serde")] #[serde(crate = "rocket::serde")]
struct LoginInfo { struct LoginInfo {
@ -95,21 +100,27 @@ async fn createuser(
} }
} }
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
struct GetUser {
username: String,
admin: bool,
make_posts: bool,
comment: bool
}
#[get("/account")] #[get("/account")]
async fn account(mut db: Connection<Db>, cookies: &CookieJar<'_>) -> status::Custom<String> { async fn account(mut db: Connection<Db>, cookies: &CookieJar<'_>) -> status::Custom<Result<Json<GetUser>, &'static str>> {
let token = cookies.get_private("token"); let token = cookies.get_private("token");
match token { match token {
Some(t) => match User::get_by_token(&mut db, t).await { Some(t) => match User::get_by_token(&mut db, t).await {
Some(user) => status::Custom( Some(user) => status::Custom(
Status::Ok, Status::Ok,
format!( Ok(Json(GetUser {username: user.username, admin: user.admin, make_posts: user.make_posts, comment: user.comment})),
"Username: {}\nAdmin: {}\nMake Posts: {}\nComment: {}",
user.username, user.admin, user.make_posts, user.comment
), ),
), None => status::Custom(Status::NotFound, Err("User doesn't exist.")),
None => status::Custom(Status::NotFound, "User doesn't exist.".to_string()),
}, },
None => status::Custom(Status::Unauthorized, "Not logged in".to_string()), None => status::Custom(Status::Unauthorized, Err("Not logged in")),
} }
} }
@ -629,7 +640,8 @@ async fn main() {
upload, upload,
uploadimage, uploadimage,
delete_image, delete_image,
get_images_by_username get_images_by_username,
myimages
], ],
) )
.mount("/api", routes![api_perms]) .mount("/api", routes![api_perms])

28
web/myimages.html Normal file
View File

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Images</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<div id='images' style='display: none'></div>
<button id='showImages-button' onclick="getImages()">Get Images</button>
</body>
<script defer>
async function getImages(user) {
// get current username
let username = JSON.parse(await (await fetch("/account")).text()).username;
// get images for username
let images = JSON.parse(await (await fetch(`/images/by-user/${username}`)).text())
let imagestrings = [];
for(const img of images) {
imagestrings.push(`${img}<br><img src="/images/${img}.png" alt="${img}" style="max-width: 100%">`)
}
document.getElementById('images').innerHTML = imagestrings.join('<br><br>');
document.getElementById('images').style.display = '';
}
</script>
</html>