Basic steam login and demo upload

This commit is contained in:
Lol3rrr
2024-09-07 02:57:04 +02:00
parent 7c87bd6bcd
commit 324eaf7d3d
11 changed files with 3070 additions and 1 deletions

76
backend/src/lib.rs Normal file
View File

@@ -0,0 +1,76 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct UserSessionData {
pub steam_id: Option<u64>,
}
impl Default for UserSessionData {
fn default() -> Self {
Self { steam_id: None }
}
}
pub struct UserSession {
pub session: tower_sessions::Session,
data: UserSessionData,
}
impl UserSession {
const KEY: &'static str = "user.data";
pub fn data(&self) -> &UserSessionData {
&self.data
}
pub async fn modify_data<F>(&mut self, func: F)
where
F: FnOnce(&mut UserSessionData),
{
let mut entry = &mut self.data;
func(&mut entry);
self.session.insert(Self::KEY, entry).await.unwrap();
}
}
#[async_trait::async_trait]
impl<S> axum::extract::FromRequestParts<S> for UserSession
where
S: Send + Sync,
{
type Rejection = (axum::http::StatusCode, &'static str);
async fn from_request_parts(
req: &mut axum::http::request::Parts,
state: &S,
) -> Result<Self, Self::Rejection> {
let session = tower_sessions::Session::from_request_parts(req, state).await?;
let guest_data: UserSessionData = session.get(Self::KEY).await.unwrap().unwrap_or_default();
Ok(Self {
session,
data: guest_data,
})
}
}
pub async fn get_demo_from_upload(name: &str, mut form: axum::extract::Multipart) -> Option<axum::body::Bytes> {
while let Ok(field) = form.next_field().await {
let field = match field {
Some(f) => f,
None => continue,
};
if field.name().map(|n| n != name).unwrap_or(false) {
continue;
}
if let Ok(data) = field.bytes().await {
return Some(data);
}
}
None
}