use actix_web::error::{ErrorBadRequest, ErrorInternalServerError, ErrorNotFound}; use actix_web::web::ReqData; use actix_web::{get, post, web}; use ovunto_security::CipherMessage; use crate::app::state::{AppState, User}; #[post("/add")] async fn add( state: web::Data, user: ReqData, body: web::Bytes, ) -> actix_web::Result { let body_str = String::from_utf8(body.to_vec()).map_err(|_| ErrorBadRequest("Invalid UTF-8"))?; let layer = CipherMessage::from_base64(body_str).map_err(|err| ErrorBadRequest(format!("{err:?}")))?; let mut users = state .users .lock() .map_err(|err| ErrorInternalServerError(format!("{err:?}")))?; let user = users .get_mut(&user.username) .ok_or(ErrorBadRequest("Unknown user"))?; user.layers.push(layer); Ok("Layer successfully added".to_string()) } #[get("/count")] async fn count(user: ReqData) -> actix_web::Result { Ok(user.layers.len().to_string()) } #[get("/{index}")] async fn get(user: ReqData, path: web::Path<(usize,)>) -> actix_web::Result { let layer = user .layers .get(path.into_inner().0) .ok_or(ErrorNotFound("Layer does not exist"))?; layer .to_base64() .map_err(|err| ErrorInternalServerError(format!("{err:?}"))) } pub fn config(cfg: &mut web::ServiceConfig) { cfg.service(add).service(count).service(get); }