rewrite GET /account and add GET /myimages
This commit is contained in:
parent
3a5fb7bcd0
commit
4ffc45d5f7
|
@ -18,6 +18,8 @@ services:
|
|||
environment:
|
||||
- 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_PORT=$PORT
|
||||
- RUST_LOG=debug
|
||||
|
|
28
src/main.rs
28
src/main.rs
|
@ -41,6 +41,11 @@ fn uploadimage() -> RawHtml<String> {
|
|||
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)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
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")]
|
||||
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");
|
||||
match token {
|
||||
Some(t) => match User::get_by_token(&mut db, t).await {
|
||||
Some(user) => status::Custom(
|
||||
Status::Ok,
|
||||
format!(
|
||||
"Username: {}\nAdmin: {}\nMake Posts: {}\nComment: {}",
|
||||
user.username, user.admin, user.make_posts, user.comment
|
||||
),
|
||||
Ok(Json(GetUser {username: user.username, admin: user.admin, make_posts: user.make_posts, comment: user.comment})),
|
||||
),
|
||||
None => status::Custom(Status::NotFound, "User doesn't exist.".to_string()),
|
||||
None => status::Custom(Status::NotFound, Err("User doesn't exist.")),
|
||||
},
|
||||
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,
|
||||
uploadimage,
|
||||
delete_image,
|
||||
get_images_by_username
|
||||
get_images_by_username,
|
||||
myimages
|
||||
],
|
||||
)
|
||||
.mount("/api", routes![api_perms])
|
||||
|
|
|
@ -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>
|
Loading…
Reference in New Issue