Add /photo

This commit is contained in:
Joscha 2024-03-10 15:40:41 +01:00
parent e0a668a581
commit e903ca7ff9
4 changed files with 90 additions and 8 deletions

View file

@ -18,6 +18,7 @@ pub enum Command {
Test,
Text(String),
Image(RgbaImage),
Photo { image: RgbaImage, title: String },
ChatMessage { username: String, content: String },
}
@ -67,6 +68,7 @@ impl Drawer {
Command::Test => self.on_test()?,
Command::Text(text) => self.on_text(text)?,
Command::Image(image) => self.on_image(image)?,
Command::Photo { image, title } => self.on_photo(image, title)?,
Command::ChatMessage { username, content } => {
self.on_chat_message(username, content)?
}
@ -146,6 +148,36 @@ impl Drawer {
Ok(())
}
fn on_photo(&mut self, image: RgbaImage, title: String) -> anyhow::Result<()> {
let mut tree = Tree::<Context>::new(WHITE);
let image = Image::new(image)
.with_dither_palette(&[BLACK, WHITE])
.node()
.register(&mut tree)?;
let title = Text::new()
.with_metrics(Text::default_metrics().scale(2.0))
.and_plain(title)
.widget(&mut self.ctx.font_stuff)
.node()
.register(&mut tree)?;
let root = Node::empty()
.with_size_width(percent(1.0))
.with_padding_bottom(length(Self::FEED))
.with_display(Display::Flex)
.with_flex_direction(FlexDirection::Column)
.with_align_items(Some(AlignItems::Center))
.with_gap(length(16.0))
.and_child(image)
.and_child(title)
.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

@ -28,6 +28,7 @@ pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()>
.route("/rip", post(post_rip))
.route("/text", post(post_text))
.route("/image", post(post_image).fallback(get_static_file))
.route("/photo", post(post_photo).fallback(get_static_file))
.route("/chat_message", post(post_chat_message))
.fallback(get(get_static_file))
.layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB
@ -63,21 +64,49 @@ async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow:
let mut image = None;
while let Some(field) = multipart.next_field().await? {
if let Some(name) = field.name() {
if name == "image" {
if let Some("image") = field.name() {
let data = field.bytes().await?;
let decoded = image::load_from_memory(&data)?.into_rgba8();
image = Some(decoded);
}
}
let Some(image) = image else {
return Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY));
};
let _ = server.tx.send(Command::Image(image)).await;
Ok(Redirect::to("image").into_response())
}
async fn post_photo(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> {
let mut image = None;
let mut title = None;
while let Some(field) = multipart.next_field().await? {
match field.name() {
Some("image") => {
let data = field.bytes().await?;
let decoded = image::load_from_memory(&data)?.into_rgba8();
image = Some(decoded);
}
Some("title") => {
title = Some(field.text().await?);
}
_ => {}
}
}
if let Some(image) = image {
let _ = server.tx.send(Command::Image(image)).await;
return Ok(Redirect::to("image").into_response());
}
let Some(image) = image else {
return Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY));
};
Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY))
let Some(title) = title else {
return Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY));
};
let _ = server.tx.send(Command::Photo { image, title }).await;
Ok(Redirect::to("photo").into_response())
}
#[derive(Deserialize)]

View file

@ -8,7 +8,8 @@
<body>
<h1>Thermal Printer Control</h1>
<ul>
<a href="image">Upload an image</a>
<li><a href="image">Upload an image</a></li>
<li><a href="photo">Take a photo</a></li>
</ul>
</body>
</html>

View file

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, height=device-height, initial-scale=1, user-scalable=0"
/>
<title>TP: Photo</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<ol>
<li><input type="file" name="image" /></li>
<li><input type="text" name="title" /></li>
<li><button>Print!</button></li>
</ol>
</form>
</body>
</html>