Files
ojrc/cdn/src/robocraft/user_avatar.rs
T

22 lines
1.3 KiB
Rust
Raw Normal View History

use actix_web::{get, post, web::{Data, Path, Bytes}, Responder};
2025-05-24 16:21:45 -04:00
#[get("/customavatar/Live/{name}")]
pub async fn get(cli: Data<crate::cli::CliArgs>, name: Path<String>) -> impl Responder {
2025-05-24 23:56:48 -04:00
let path = std::path::PathBuf::from(&cli.data_robocraft).join("customavatars").join(format!("{}.jpg", *name));
2025-05-24 16:21:45 -04:00
log::debug!("RC asset at {} (exists? {})", path.display(), path.exists());
if path.exists() {
actix_files::NamedFile::open_async(path).await
} else {
log::info!("Not found /customavatar/Live/{} -> {}, using default image", name, path.display());
actix_files::NamedFile::open_async(std::path::PathBuf::from(&cli.assets_robocraft).join(super::DEFAULT_IMAGE)).await
}
2025-05-24 16:21:45 -04:00
}
#[post("/customavatar/Live/{name}")]
pub async fn post(cli: Data<crate::cli::CliArgs>, auth: Data<crate::robocraft::IntercomAuth>, name: Path<String>, body: Bytes, req: actix_web::HttpRequest) -> Result<actix_web::HttpResponse, super::IntercomOpError> {
auth.validate(&req, &name)?;
let path = std::path::PathBuf::from(&cli.data_robocraft).join("customavatars").join(format!("{}.jpg", *name));
log::debug!("Saving customavatar for {} to {}: {}B", name, path.display(), body.len());
std::fs::write(path, &body).map_err(super::IntercomOpError::Io)?;
Ok(actix_web::HttpResponse::NoContent().finish())
}