|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use image::ImageFormat; |
| 4 | +use ohkami::{ |
| 5 | + IntoResponse, Ohkami, Query, Route, |
| 6 | + claw::status::Created, |
| 7 | + openapi::{self, Schema}, |
| 8 | + serde::Deserialize, |
| 9 | +}; |
| 10 | +use tee_morphosis::tee::{Tee, hsl::ddnet_color_to_hsl, parts::TeePart, skin::TEE_SKIN_LAYOUT}; |
| 11 | +use tokio::{fs, task::spawn_blocking}; |
| 12 | +use tracing::info; |
| 13 | + |
| 14 | +use crate::{ |
| 15 | + app::{logger::LogRequest, png::Png}, |
| 16 | + error::Error, |
| 17 | +}; |
| 18 | + |
| 19 | +pub fn skin_router() -> Ohkami { |
| 20 | + Ohkami::new((LogRequest, openapi::Tag("skin"), "/".GET(skin_handler))) |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Debug, Deserialize, Schema)] |
| 24 | +pub struct SkinQuery<'req> { |
| 25 | + pub body: Option<u32>, |
| 26 | + pub feet: Option<u32>, |
| 27 | + pub name: Option<&'req str>, |
| 28 | +} |
| 29 | + |
| 30 | +async fn skin_handler(Query(query): Query<SkinQuery<'_>>) -> Result<impl IntoResponse, Error> { |
| 31 | + let path = PathBuf::from("./.ref") |
| 32 | + .join(query.name.ok_or(Error::QueryNameNotFound)?) |
| 33 | + .with_extension("png"); |
| 34 | + info!(path=%path.display()); |
| 35 | + |
| 36 | + let uv = fs::read(path.clone()).await.map_err(Error::Io)?; |
| 37 | + |
| 38 | + let tee = spawn_blocking(move || { |
| 39 | + Tee::new(uv.into(), ImageFormat::Png).map(|mut tee| { |
| 40 | + if let Some(value) = query.body { |
| 41 | + tee.apply_hsv_to_parts( |
| 42 | + ddnet_color_to_hsl(value), |
| 43 | + &[TeePart::Body, TeePart::BodyShadow], |
| 44 | + ); |
| 45 | + } |
| 46 | + if let Some(value) = query.feet { |
| 47 | + tee.apply_hsv_to_parts( |
| 48 | + ddnet_color_to_hsl(value), |
| 49 | + &[TeePart::Feet, TeePart::FeetShadow], |
| 50 | + ); |
| 51 | + } |
| 52 | + tee.compose_default(TEE_SKIN_LAYOUT) |
| 53 | + }) |
| 54 | + }) |
| 55 | + .await???; |
| 56 | + Ok(Created(Png(tee.into()))) |
| 57 | +} |
0 commit comments