summaryrefslogtreecommitdiff
path: root/src/public/file.rs
diff options
context:
space:
mode:
authorTyler Murphy <tylermurphy534@gmail.com>2023-02-01 20:34:22 -0500
committerTyler Murphy <tylermurphy534@gmail.com>2023-02-01 20:34:22 -0500
commit2026a8f4579b1db0f6e5e7b11ac33c13969adb6c (patch)
treedaa13419b7227462775e325a4f5f60f2ed33c1da /src/public/file.rs
parentremove b64 imgs (diff)
downloadxssbook-2026a8f4579b1db0f6e5e7b11ac33c13969adb6c.tar.gz
xssbook-2026a8f4579b1db0f6e5e7b11ac33c13969adb6c.tar.bz2
xssbook-2026a8f4579b1db0f6e5e7b11ac33c13969adb6c.zip
static serve refactor
Diffstat (limited to 'src/public/file.rs')
-rw-r--r--src/public/file.rs69
1 files changed, 69 insertions, 0 deletions
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
+}