mirror of
https://github.com/shiroyashik/sculptor.git
synced 2025-12-06 13:01:12 +03:00
Format code
This commit is contained in:
parent
45192ef182
commit
026c6a950e
8 changed files with 145 additions and 86 deletions
105
src/profile.rs
105
src/profile.rs
|
|
@ -1,18 +1,31 @@
|
|||
use anyhow_http::{http_error_ret, response::Result};
|
||||
use axum::{body::Bytes, debug_handler, extract::{Path, State}, Json};
|
||||
use axum::{
|
||||
body::Bytes,
|
||||
debug_handler,
|
||||
extract::{Path, State},
|
||||
Json,
|
||||
};
|
||||
use log::{debug, warn};
|
||||
use serde_json::{json, Value};
|
||||
use tokio::{fs, io::{AsyncReadExt, BufWriter, self}};
|
||||
use tokio::{
|
||||
fs,
|
||||
io::{self, AsyncReadExt, BufWriter},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{auth::Token, utils::{calculate_file_sha256, format_uuid, get_correct_array}, ws::S2CMessage, AppState};
|
||||
use crate::{
|
||||
auth::Token,
|
||||
utils::{calculate_file_sha256, format_uuid, get_correct_array},
|
||||
ws::S2CMessage,
|
||||
AppState,
|
||||
};
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn user_info(
|
||||
Path(uuid): Path<Uuid>,
|
||||
State(state): State<AppState>, // FIXME: Variable doesn't using!
|
||||
) -> Json<Value> {
|
||||
log::info!("Receiving profile information for {}",uuid);
|
||||
log::info!("Receiving profile information for {}", uuid);
|
||||
|
||||
let formatted_uuid = format_uuid(&uuid);
|
||||
|
||||
|
|
@ -40,36 +53,40 @@ pub async fn user_info(
|
|||
if let Some(settings) = state.advanced_users.lock().await.get(&formatted_uuid) {
|
||||
let pride = get_correct_array(settings.get("pride").unwrap());
|
||||
let special = get_correct_array(settings.get("special").unwrap());
|
||||
let badges = user_info_response.get_mut("equippedBadges").and_then(Value::as_object_mut).unwrap();
|
||||
badges.append(json!({
|
||||
"special": special,
|
||||
"pride": pride
|
||||
}).as_object_mut().unwrap())
|
||||
let badges = user_info_response
|
||||
.get_mut("equippedBadges")
|
||||
.and_then(Value::as_object_mut)
|
||||
.unwrap();
|
||||
badges.append(
|
||||
json!({
|
||||
"special": special,
|
||||
"pride": pride
|
||||
})
|
||||
.as_object_mut()
|
||||
.unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
if fs::metadata(&avatar_file).await.is_ok() {
|
||||
if let Some(equipped) = user_info_response.get_mut("equipped").and_then(Value::as_array_mut){
|
||||
match calculate_file_sha256(&avatar_file){
|
||||
Ok(hash) => {
|
||||
equipped.push(json!({
|
||||
"id": "avatar",
|
||||
"owner": &formatted_uuid,
|
||||
"hash": hash
|
||||
}))
|
||||
}
|
||||
if let Some(equipped) = user_info_response
|
||||
.get_mut("equipped")
|
||||
.and_then(Value::as_array_mut)
|
||||
{
|
||||
match calculate_file_sha256(&avatar_file) {
|
||||
Ok(hash) => equipped.push(json!({
|
||||
"id": "avatar",
|
||||
"owner": &formatted_uuid,
|
||||
"hash": hash
|
||||
})),
|
||||
Err(_e) => {}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Json(user_info_response)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn download_avatar(
|
||||
Path(uuid): Path<Uuid>,
|
||||
) -> Result<Vec<u8>> {
|
||||
pub async fn download_avatar(Path(uuid): Path<Uuid>) -> Result<Vec<u8>> {
|
||||
let uuid = format_uuid(&uuid);
|
||||
log::info!("Requesting an avatar: {}", uuid);
|
||||
let mut file = if let Ok(file) = fs::File::open(format!("avatars/{}.moon", uuid)).await {
|
||||
|
|
@ -92,8 +109,7 @@ pub async fn upload_avatar(
|
|||
Token(token): Token,
|
||||
State(state): State<AppState>,
|
||||
body: Bytes,
|
||||
) -> Result<String> {
|
||||
|
||||
) -> Result<String> {
|
||||
let request_data = body;
|
||||
|
||||
let token = match token {
|
||||
|
|
@ -102,39 +118,48 @@ pub async fn upload_avatar(
|
|||
};
|
||||
|
||||
if let Some(user_info) = state.authenticated.get(&token) {
|
||||
log::info!("{} ({}) trying to upload an avatar",user_info.uuid,user_info.username);
|
||||
let avatar_file = format!("avatars/{}.moon",user_info.uuid);
|
||||
log::info!(
|
||||
"{} ({}) trying to upload an avatar",
|
||||
user_info.uuid,
|
||||
user_info.username
|
||||
);
|
||||
let avatar_file = format!("avatars/{}.moon", user_info.uuid);
|
||||
let mut file = BufWriter::new(fs::File::create(&avatar_file).await?);
|
||||
io::copy(&mut request_data.as_ref(), &mut file).await?;
|
||||
}
|
||||
Ok("ok".to_string())
|
||||
}
|
||||
|
||||
pub async fn equip_avatar(
|
||||
Token(token): Token,
|
||||
State(state): State<AppState>,
|
||||
) -> String {
|
||||
pub async fn equip_avatar(Token(token): Token, State(state): State<AppState>) -> String {
|
||||
debug!("[API] S2C : Equip");
|
||||
let uuid = state.authenticated.get(&token.unwrap()).unwrap().uuid;
|
||||
if state.broadcasts.get(&uuid).unwrap().send(S2CMessage::Event(uuid).to_vec()).is_err() {
|
||||
warn!("[WebSocket] Failed to send Event! Maybe there is no one to send") // FIXME: Засунуть в Handler
|
||||
if state
|
||||
.broadcasts
|
||||
.get(&uuid)
|
||||
.unwrap()
|
||||
.send(S2CMessage::Event(uuid).to_vec())
|
||||
.is_err()
|
||||
{
|
||||
warn!("[WebSocket] Failed to send Event! Maybe there is no one to send")
|
||||
// FIXME: Засунуть в Handler
|
||||
};
|
||||
"ok".to_string()
|
||||
}
|
||||
|
||||
pub async fn delete_avatar(
|
||||
Token(token): Token,
|
||||
State(state): State<AppState>,
|
||||
) -> Result<String> {
|
||||
pub async fn delete_avatar(Token(token): Token, State(state): State<AppState>) -> Result<String> {
|
||||
let token = match token {
|
||||
Some(t) => t,
|
||||
None => http_error_ret!(UNAUTHORIZED, "Authentication error!"),
|
||||
};
|
||||
if let Some(user_info) = state.authenticated.get(&token) {
|
||||
log::info!("{} ({}) is trying to delete the avatar",user_info.uuid,user_info.username);
|
||||
let avatar_file = format!("avatars/{}.moon",user_info.uuid);
|
||||
log::info!(
|
||||
"{} ({}) is trying to delete the avatar",
|
||||
user_info.uuid,
|
||||
user_info.username
|
||||
);
|
||||
let avatar_file = format!("avatars/{}.moon", user_info.uuid);
|
||||
fs::remove_file(avatar_file).await?;
|
||||
}
|
||||
// let avatar_file = format!("avatars/{}.moon",user_info.uuid);
|
||||
Ok("ok".to_string())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue