Add /photo
This commit is contained in:
parent
e0a668a581
commit
e903ca7ff9
4 changed files with 90 additions and 8 deletions
|
|
@ -18,6 +18,7 @@ pub enum Command {
|
||||||
Test,
|
Test,
|
||||||
Text(String),
|
Text(String),
|
||||||
Image(RgbaImage),
|
Image(RgbaImage),
|
||||||
|
Photo { image: RgbaImage, title: String },
|
||||||
ChatMessage { username: String, content: String },
|
ChatMessage { username: String, content: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -67,6 +68,7 @@ impl Drawer {
|
||||||
Command::Test => self.on_test()?,
|
Command::Test => self.on_test()?,
|
||||||
Command::Text(text) => self.on_text(text)?,
|
Command::Text(text) => self.on_text(text)?,
|
||||||
Command::Image(image) => self.on_image(image)?,
|
Command::Image(image) => self.on_image(image)?,
|
||||||
|
Command::Photo { image, title } => self.on_photo(image, title)?,
|
||||||
Command::ChatMessage { username, content } => {
|
Command::ChatMessage { username, content } => {
|
||||||
self.on_chat_message(username, content)?
|
self.on_chat_message(username, content)?
|
||||||
}
|
}
|
||||||
|
|
@ -146,6 +148,36 @@ impl Drawer {
|
||||||
Ok(())
|
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<()> {
|
fn on_chat_message(&mut self, username: String, content: String) -> anyhow::Result<()> {
|
||||||
let mut tree = Tree::<Context>::new(WHITE);
|
let mut tree = Tree::<Context>::new(WHITE);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()>
|
||||||
.route("/rip", post(post_rip))
|
.route("/rip", post(post_rip))
|
||||||
.route("/text", post(post_text))
|
.route("/text", post(post_text))
|
||||||
.route("/image", post(post_image).fallback(get_static_file))
|
.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))
|
.route("/chat_message", post(post_chat_message))
|
||||||
.fallback(get(get_static_file))
|
.fallback(get(get_static_file))
|
||||||
.layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB
|
.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;
|
let mut image = None;
|
||||||
|
|
||||||
while let Some(field) = multipart.next_field().await? {
|
while let Some(field) = multipart.next_field().await? {
|
||||||
if let Some(name) = field.name() {
|
if let Some("image") = field.name() {
|
||||||
if name == "image" {
|
|
||||||
let data = field.bytes().await?;
|
let data = field.bytes().await?;
|
||||||
let decoded = image::load_from_memory(&data)?.into_rgba8();
|
let decoded = image::load_from_memory(&data)?.into_rgba8();
|
||||||
image = Some(decoded);
|
image = Some(decoded);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(image) = image {
|
let Some(image) = image else {
|
||||||
|
return Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY));
|
||||||
|
};
|
||||||
|
|
||||||
let _ = server.tx.send(Command::Image(image)).await;
|
let _ = server.tx.send(Command::Image(image)).await;
|
||||||
return Ok(Redirect::to("image").into_response());
|
Ok(Redirect::to("image").into_response())
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(status_code(StatusCode::UNPROCESSABLE_ENTITY))
|
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?);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(image) = image else {
|
||||||
|
return 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)]
|
#[derive(Deserialize)]
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,8 @@
|
||||||
<body>
|
<body>
|
||||||
<h1>Thermal Printer Control</h1>
|
<h1>Thermal Printer Control</h1>
|
||||||
<ul>
|
<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>
|
</ul>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
20
showbits-thermal-printer/static/photo.html
Normal file
20
showbits-thermal-printer/static/photo.html
Normal 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>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue