mirror of
https://github.com/shiroyashik/sculptor.git
synced 2025-12-06 13:01:12 +03:00
Added avatar deletion and uploading ( v1 api )
This commit is contained in:
parent
64d1111522
commit
e8bf52dfd1
6 changed files with 65 additions and 29 deletions
|
|
@ -155,7 +155,7 @@ pub async fn delete_avatar(Token(token): Token, State(state): State<AppState>) -
|
||||||
Ok("ok".to_string())
|
Ok("ok".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_event(broadcasts: &Arc<DashMap<Uuid, Sender<Vec<u8>>>>, uuid: &Uuid) {
|
pub fn send_event(broadcasts: &Arc<DashMap<Uuid, Sender<Vec<u8>>>>, uuid: &Uuid) {
|
||||||
if let Some(broadcast) = broadcasts.get(&uuid) {
|
if let Some(broadcast) = broadcasts.get(&uuid) {
|
||||||
if broadcast.send(S2CMessage::Event(*uuid).to_vec()).is_err() {
|
if broadcast.send(S2CMessage::Event(*uuid).to_vec()).is_err() {
|
||||||
debug!("[WebSocket] Failed to send Event! There is no one to send. UUID: {uuid}")
|
debug!("[WebSocket] Failed to send Event! There is no one to send. UUID: {uuid}")
|
||||||
|
|
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
use axum::{extract::State, http::StatusCode, response::{IntoResponse, Response}};
|
|
||||||
|
|
||||||
use crate::{auth::Token, AppState};
|
|
||||||
|
|
||||||
pub async fn status(
|
|
||||||
Token(token): Token,
|
|
||||||
State(state): State<AppState>
|
|
||||||
) -> Response {
|
|
||||||
match token {
|
|
||||||
Some(token) => {
|
|
||||||
if state.user_manager.is_authenticated(&token) {
|
|
||||||
(StatusCode::OK, "ok".to_string()).into_response()
|
|
||||||
} else {
|
|
||||||
|
|
||||||
(StatusCode::UNAUTHORIZED, "unauthorized".to_string()).into_response()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
(StatusCode::BAD_REQUEST, "bad request".to_string()).into_response()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
56
src/api/v1/avatars.rs
Normal file
56
src/api/v1/avatars.rs
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
use axum::{body::Bytes, extract::{Path, State}, http::StatusCode, response::{IntoResponse, Response}};
|
||||||
|
use tokio::{fs, io::{self, BufWriter}};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::{api::figura::profile::send_event, auth::Token, AppState};
|
||||||
|
|
||||||
|
pub async fn upload_avatar(
|
||||||
|
Path(uuid): Path<Uuid>,
|
||||||
|
Token(token): Token,
|
||||||
|
State(state): State<AppState>,
|
||||||
|
body: Bytes,
|
||||||
|
) -> Response {
|
||||||
|
let request_data = body;
|
||||||
|
|
||||||
|
match state.config.lock().await.clone().verify_token(&token) {
|
||||||
|
Ok(_) => {},
|
||||||
|
Err(err) => return err,
|
||||||
|
};
|
||||||
|
|
||||||
|
tracing::info!(
|
||||||
|
"trying to upload the avatar for {}",
|
||||||
|
uuid,
|
||||||
|
);
|
||||||
|
|
||||||
|
let avatar_file = format!("avatars/{}.moon", &uuid);
|
||||||
|
let mut file = BufWriter::new(fs::File::create(&avatar_file).await.unwrap());
|
||||||
|
io::copy(&mut request_data.as_ref(), &mut file).await.unwrap();
|
||||||
|
send_event(&state.broadcasts, &uuid);
|
||||||
|
|
||||||
|
(StatusCode::OK, "ok".to_string()).into_response()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_avatar(
|
||||||
|
Path(uuid): Path<Uuid>,
|
||||||
|
Token(token): Token,
|
||||||
|
State(state): State<AppState>
|
||||||
|
) -> Response {
|
||||||
|
match state.config.lock().await.clone().verify_token(&token) {
|
||||||
|
Ok(_) => {},
|
||||||
|
Err(err) => return err,
|
||||||
|
};
|
||||||
|
|
||||||
|
tracing::info!(
|
||||||
|
"trying to delete the avatar for {}",
|
||||||
|
uuid,
|
||||||
|
);
|
||||||
|
|
||||||
|
let avatar_file = format!("avatars/{}.moon", &uuid);
|
||||||
|
match fs::remove_file(avatar_file).await {
|
||||||
|
Ok(_) => {},
|
||||||
|
Err(_) => return (StatusCode::NOT_FOUND, "avatar doesn't exist".to_string()).into_response()
|
||||||
|
};
|
||||||
|
send_event(&state.broadcasts, &uuid);
|
||||||
|
|
||||||
|
(StatusCode::OK, "ok".to_string()).into_response()
|
||||||
|
}
|
||||||
|
|
@ -1,16 +1,17 @@
|
||||||
use axum::{routing::{get, post}, Router};
|
use axum::{extract::DefaultBodyLimit, routing::{delete, get, post, put}, Router};
|
||||||
use crate::AppState;
|
use crate::AppState;
|
||||||
|
|
||||||
mod http2ws;
|
mod http2ws;
|
||||||
mod users;
|
mod users;
|
||||||
mod auth;
|
|
||||||
mod types;
|
mod types;
|
||||||
|
mod avatars;
|
||||||
|
|
||||||
pub fn router() -> Router<AppState> {
|
pub fn router() -> Router<AppState> {
|
||||||
Router::new()
|
Router::new()
|
||||||
.route("/verify", get(http2ws::verify))
|
.route("/verify", get(http2ws::verify))
|
||||||
.route("/raw", post(http2ws::raw))
|
.route("/raw", post(http2ws::raw))
|
||||||
.route("/sub/raw", post(http2ws::sub_raw))
|
.route("/sub/raw", post(http2ws::sub_raw))
|
||||||
.route("/auth/", get(auth::status))
|
.route("/user/create", post(users::create_user))
|
||||||
.route("/users/create", post(users::create_user))
|
.route("/avatar/:uuid", put(avatars::upload_avatar).layer(DefaultBodyLimit::disable()))
|
||||||
|
.route("/avatar/:uuid", delete(avatars::delete_avatar))
|
||||||
}
|
}
|
||||||
|
|
@ -13,12 +13,13 @@ pub(super) async fn create_user(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Json(json): Json<Userinfo>
|
Json(json): Json<Userinfo>
|
||||||
) -> Response {
|
) -> Response {
|
||||||
debug!("Json: {json:?}");
|
|
||||||
match state.config.lock().await.clone().verify_token(&token) {
|
match state.config.lock().await.clone().verify_token(&token) {
|
||||||
Ok(_) => {},
|
Ok(_) => {},
|
||||||
Err(e) => return e,
|
Err(e) => return e,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
debug!("Creating new user: {json:?}");
|
||||||
|
|
||||||
state.user_manager.insert_user(json.uuid, json);
|
state.user_manager.insert_user(json.uuid, json);
|
||||||
(StatusCode::OK, "ok".to_string()).into_response()
|
(StatusCode::OK, "ok".to_string()).into_response()
|
||||||
}
|
}
|
||||||
|
|
@ -137,7 +137,7 @@ impl UManager {
|
||||||
) -> Option<dashmap::mapref::one::Ref<'_, Uuid, Userinfo>> {
|
) -> Option<dashmap::mapref::one::Ref<'_, Uuid, Userinfo>> {
|
||||||
self.registered.get(uuid)
|
self.registered.get(uuid)
|
||||||
}
|
}
|
||||||
pub fn is_authenticated(&self, token: &String) -> bool {
|
pub fn _is_authenticated(&self, token: &String) -> bool {
|
||||||
self.authenticated.contains_key(token)
|
self.authenticated.contains_key(token)
|
||||||
}
|
}
|
||||||
pub fn _is_registered(&self, uuid: &Uuid) -> bool {
|
pub fn _is_registered(&self, uuid: &Uuid) -> bool {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue