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
|
|
@ -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;
|
||||
|
||||
mod http2ws;
|
||||
mod users;
|
||||
mod auth;
|
||||
mod types;
|
||||
mod avatars;
|
||||
|
||||
pub fn router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/verify", get(http2ws::verify))
|
||||
.route("/raw", post(http2ws::raw))
|
||||
.route("/sub/raw", post(http2ws::sub_raw))
|
||||
.route("/auth/", get(auth::status))
|
||||
.route("/users/create", post(users::create_user))
|
||||
.route("/user/create", post(users::create_user))
|
||||
.route("/avatar/:uuid", put(avatars::upload_avatar).layer(DefaultBodyLimit::disable()))
|
||||
.route("/avatar/:uuid", delete(avatars::delete_avatar))
|
||||
}
|
||||
|
|
@ -13,11 +13,12 @@ pub(super) async fn create_user(
|
|||
State(state): State<AppState>,
|
||||
Json(json): Json<Userinfo>
|
||||
) -> Response {
|
||||
debug!("Json: {json:?}");
|
||||
match state.config.lock().await.clone().verify_token(&token) {
|
||||
Ok(_) => {},
|
||||
Err(e) => return e,
|
||||
}
|
||||
|
||||
debug!("Creating new user: {json:?}");
|
||||
|
||||
state.user_manager.insert_user(json.uuid, json);
|
||||
(StatusCode::OK, "ok".to_string()).into_response()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue