Basic steam login and demo upload
This commit is contained in:
76
backend/src/lib.rs
Normal file
76
backend/src/lib.rs
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user