diff options
Diffstat (limited to '')
-rw-r--r-- | src/public/admin.rs (renamed from src/admin.rs) | 0 | ||||
-rw-r--r-- | src/public/console.rs (renamed from src/console.rs) | 0 | ||||
-rw-r--r-- | src/public/file.rs | 69 | ||||
-rw-r--r-- | src/public/mod.rs | 47 | ||||
-rw-r--r-- | src/public/pages.rs (renamed from src/api/pages.rs) | 30 |
5 files changed, 125 insertions, 21 deletions
diff --git a/src/admin.rs b/src/public/admin.rs index 1da2f1e..1da2f1e 100644 --- a/src/admin.rs +++ b/src/public/admin.rs diff --git a/src/console.rs b/src/public/console.rs index 16bf4a3..16bf4a3 100644 --- a/src/console.rs +++ b/src/public/console.rs diff --git a/src/public/file.rs b/src/public/file.rs new file mode 100644 index 0000000..b54ef25 --- /dev/null +++ b/src/public/file.rs @@ -0,0 +1,69 @@ +use axum::{ + extract::{Path, Query}, + http::StatusCode, + response::Response, +}; +use serde::Deserialize; + +use crate::types::http::ResponseCode; + +use super::console; + +pub async fn js(Path(path): Path<String>) -> Response { + let path = format!("/js/{}", path); + super::serve(&path).await +} + +pub async fn css(Path(path): Path<String>) -> Response { + let path = format!("/css/{}", path); + super::serve(&path).await +} + +pub async fn fonts(Path(path): Path<String>) -> Response { + let path = format!("/fonts/{}", path); + super::serve(&path).await +} + +pub async fn image(Path(path): Path<String>) -> Response { + let path = format!("/image/{}", path); + super::serve(&path).await +} + +#[derive(Deserialize)] +pub struct AvatarRequest { + user_id: u64, +} + +pub async fn avatar(params: Option<Query<AvatarRequest>>) -> Response { + let Some(params) = params else { + return ResponseCode::BadRequest.text("Missing query paramaters"); + }; + + let custom = format!("/image/custom/avatar/{}.png", params.user_id); + let default = format!("/image/default/{}.png", params.user_id % 25); + + let file = super::serve(&custom).await; + if file.status() != StatusCode::OK { + return super::serve(&default).await; + } + file +} + +#[derive(Deserialize)] +pub struct BannerRequest { + user_id: u64, +} + +pub async fn banner(params: Option<Query<BannerRequest>>) -> Response { + let Some(params) = params else { + return ResponseCode::BadRequest.text("Missing query paramaters"); + }; + + let custom = format!("/image/custom/banner/{}.png", params.user_id); + + let file = super::serve(&custom).await; + if file.status() != StatusCode::OK { + return ResponseCode::NotFound.text("User does not have a custom banner"); + } + file +} diff --git a/src/public/mod.rs b/src/public/mod.rs new file mode 100644 index 0000000..cf8156d --- /dev/null +++ b/src/public/mod.rs @@ -0,0 +1,47 @@ +use axum::{ + body::Body, + http::{Request, StatusCode}, + response::{IntoResponse, Response}, + routing::get, + Router, +}; +use tower::ServiceExt; +use tower_http::services::ServeFile; + +use crate::types::http::ResponseCode; + +pub mod admin; +pub mod console; +pub mod file; +pub mod pages; + +pub fn router() -> Router { + Router::new() + .nest("/", pages::router()) + .route("/js/*path", get(file::js)) + .route("/css/*path", get(file::css)) + .route("/fonts/*path", get(file::fonts)) + .route("/image/*path", get(file::image)) + .route("/image/avatar", get(file::avatar)) + .route("/image/banner", get(file::banner)) +} + +pub async fn serve(path: &str) -> Response { + if !path.chars().any(|c| c == '.') { + return ResponseCode::BadRequest.text("Invalid file path"); + } + + let path = format!("public{path}"); + let file = ServeFile::new(path); + + let Ok(res) = file.oneshot(Request::new(Body::empty())).await else { + tracing::error!("Error while fetching file"); + return ResponseCode::InternalServerError.text("Error while fetching file"); + }; + + if res.status() != StatusCode::OK { + return ResponseCode::NotFound.text("File not found"); + } + + res.into_response() +} diff --git a/src/api/pages.rs b/src/public/pages.rs index 4ed2e49..1614d81 100644 --- a/src/api/pages.rs +++ b/src/public/pages.rs @@ -5,7 +5,7 @@ use axum::{ }; use crate::{ - console, + public::console, types::{ extract::{AuthorizedUser, Log}, http::ResponseCode, @@ -24,32 +24,20 @@ async fn login(user: Option<AuthorizedUser>, _: Log) -> Response { if user.is_some() { Redirect::to("/home").into_response() } else { - ResponseCode::Success.file("/login.html").await + super::serve("/login.html").await } } -async fn home(user: Option<AuthorizedUser>, _: Log) -> Response { - if user.is_none() { - Redirect::to("/login").into_response() - } else { - ResponseCode::Success.file("/home.html").await - } +async fn home(_: Log) -> Response { + super::serve("/home.html").await } -async fn people(user: Option<AuthorizedUser>, _: Log) -> Response { - if user.is_none() { - Redirect::to("/login").into_response() - } else { - ResponseCode::Success.file("/people.html").await - } +async fn people(_: Log) -> Response { + super::serve("/people.html").await } -async fn profile(user: Option<AuthorizedUser>, _: Log) -> Response { - if user.is_none() { - Redirect::to("/login").into_response() - } else { - ResponseCode::Success.file("/profile.html").await - } +async fn profile(_: Log) -> Response { + super::serve("/profile.html").await } async fn console() -> Response { @@ -57,7 +45,7 @@ async fn console() -> Response { } async fn admin() -> Response { - ResponseCode::Success.file("/admin.html").await + super::serve("/admin.html").await } async fn wordpress(_: Log) -> Response { |