Add /image endpoint

This commit is contained in:
Joscha 2024-03-09 22:47:10 +01:00
parent ef5f0e3af4
commit 74b0d25dfc
4 changed files with 80 additions and 3 deletions

View file

@ -5,7 +5,7 @@ edition.workspace = true
[dependencies]
anyhow.workspace = true
axum = "0.7.4"
axum = { version = "0.7.4", features = ["multipart"] }
clap = { version = "4.5.1", features = ["derive", "deprecated"] }
cosmic-text.workspace = true
escpos = { version = "0.7.2", features = ["full"] }

View file

@ -1,6 +1,7 @@
use image::RgbaImage;
use showbits_common::{
color::{BLACK, WHITE},
widgets::{Block, FontStuff, HasFontStuff, Text},
widgets::{Block, FontStuff, HasFontStuff, Image, Text},
Node, Tree, WidgetExt,
};
use taffy::{
@ -16,6 +17,7 @@ pub enum Command {
Rip,
Test,
Text(String),
Image(RgbaImage),
ChatMessage { username: String, content: String },
}
@ -62,6 +64,7 @@ impl Drawer {
Command::Rip => self.printer.rip()?,
Command::Test => self.on_test()?,
Command::Text(text) => self.on_text(text)?,
Command::Image(image) => self.on_image(image)?,
Command::ChatMessage { username, content } => {
self.on_chat_message(username, content)?
}
@ -120,6 +123,24 @@ impl Drawer {
Ok(())
}
fn on_image(&mut self, image: RgbaImage) -> anyhow::Result<()> {
let mut tree = Tree::<Context>::new(WHITE);
let image = Image::new(image)
.with_dither_palette(&[BLACK, WHITE])
.node()
.register(&mut tree)?;
let root = Node::empty()
.with_size_width(percent(1.0))
.with_padding_bottom(length(32.0))
.and_child(image)
.register(&mut tree)?;
self.printer.print_tree(&mut tree, &mut self.ctx, root)?;
Ok(())
}
fn on_chat_message(&mut self, username: String, content: String) -> anyhow::Result<()> {
let mut tree = Tree::<Context>::new(WHITE);

View file

@ -1,4 +1,9 @@
use axum::{extract::State, routing::post, Form, Router};
use axum::{
extract::{Multipart, State},
http::StatusCode,
routing::post,
Form, Router,
};
use serde::Deserialize;
use tokio::{net::TcpListener, sync::mpsc};
@ -15,6 +20,7 @@ pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()>
.route("/test", post(post_test))
.route("/rip", post(post_rip))
.route("/text", post(post_text))
.route("/image", post(post_image))
.route("/chat_message", post(post_chat_message))
.with_state(Server { tx });
@ -44,6 +50,37 @@ async fn post_text(server: State<Server>, request: Form<PostTextForm>) {
let _ = server.tx.send(Command::Text(request.0.text)).await;
}
async fn post_image(server: State<Server>, mut multipart: Multipart) -> Result<(), StatusCode> {
let mut image = None;
while let Some(field) = multipart
.next_field()
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
{
let name = field.name().ok_or(StatusCode::INTERNAL_SERVER_ERROR)?;
if name == "image" {
let data = field
.bytes()
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let decoded = image::load_from_memory(&data)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.into_rgba8();
image = Some(decoded);
}
}
if let Some(image) = image {
let _ = server.tx.send(Command::Image(image)).await;
Ok(())
} else {
Err(StatusCode::INTERNAL_SERVER_ERROR)
}
}
#[derive(Deserialize)]
struct PostChatMessageForm {
username: String,