From a51f3fa9b529ddbf629bc80a99798838a4f203ee Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 10 Mar 2024 00:52:49 +0100 Subject: [PATCH] Add /index.html and /image.html --- .vscode/settings.json | 5 + Cargo.lock | 170 ++++++++++++++++++ showbits-thermal-printer/Cargo.toml | 2 + showbits-thermal-printer/src/server.rs | 5 +- showbits-thermal-printer/src/server/static.rs | 28 +++ showbits-thermal-printer/static/image.html | 18 ++ showbits-thermal-printer/static/index.html | 14 ++ 7 files changed, 241 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json create mode 100644 showbits-thermal-printer/src/server/static.rs create mode 100644 showbits-thermal-printer/static/image.html create mode 100644 showbits-thermal-printer/static/index.html diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a353aad --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "[html]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + } +} diff --git a/Cargo.lock b/Cargo.lock index 8245df9..d46b276 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -192,6 +192,15 @@ version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "bytemuck" version = "1.14.3" @@ -297,6 +306,15 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "cpufeatures" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +dependencies = [ + "libc", +] + [[package]] name = "crc32fast" version = "1.4.0" @@ -337,6 +355,26 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "either" version = "1.10.0" @@ -503,6 +541,16 @@ dependencies = [ "pin-utils", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -785,6 +833,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "miniz_oxide" version = "0.7.2" @@ -1116,6 +1174,40 @@ version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3cd14fd5e3b777a7422cca79358c57a8f6e3a703d9ac187448d0daf220c2407f" +[[package]] +name = "rust-embed" +version = "8.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb78f46d0066053d16d4ca7b898e9343bc3530f71c61d5ad84cd404ada068745" +dependencies = [ + "rust-embed-impl", + "rust-embed-utils", + "walkdir", +] + +[[package]] +name = "rust-embed-impl" +version = "8.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b91ac2a3c6c0520a3fb3dd89321177c3c692937c4eb21893378219da10c44fc8" +dependencies = [ + "proc-macro2", + "quote", + "rust-embed-utils", + "syn", + "walkdir", +] + +[[package]] +name = "rust-embed-utils" +version = "8.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86f69089032567ffff4eada41c573fc43ff466c7db7c5688b2e7969584345581" +dependencies = [ + "sha2", + "walkdir", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -1157,6 +1249,15 @@ version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -1222,6 +1323,17 @@ dependencies = [ "serde", ] +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "showbits-assets" version = "0.0.0" @@ -1250,7 +1362,9 @@ dependencies = [ "cosmic-text", "escpos", "image", + "mime_guess", "palette", + "rust-embed", "serde", "showbits-common", "taffy", @@ -1501,6 +1615,21 @@ version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "17f77d76d837a7830fe1d4f12b7b4ba4192c1888001c7164257e4bc6d21d96b4" +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "unicase" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.15" @@ -1561,6 +1690,16 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -1573,6 +1712,37 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/showbits-thermal-printer/Cargo.toml b/showbits-thermal-printer/Cargo.toml index fb0e753..a788903 100644 --- a/showbits-thermal-printer/Cargo.toml +++ b/showbits-thermal-printer/Cargo.toml @@ -10,7 +10,9 @@ clap = { version = "4.5.1", features = ["derive", "deprecated"] } cosmic-text.workspace = true escpos = { version = "0.7.2", features = ["full"] } image.workspace = true +mime_guess = "2.0.4" palette.workspace = true +rust-embed = "8.3.0" serde = { version = "1.0.197", features = ["derive"] } showbits-common.workspace = true taffy.workspace = true diff --git a/showbits-thermal-printer/src/server.rs b/showbits-thermal-printer/src/server.rs index 7e03520..aaa7fa5 100644 --- a/showbits-thermal-printer/src/server.rs +++ b/showbits-thermal-printer/src/server.rs @@ -1,7 +1,9 @@ +mod r#static; + use axum::{ extract::{DefaultBodyLimit, Multipart, State}, http::StatusCode, - routing::post, + routing::{get, post}, Form, Router, }; use serde::Deserialize; @@ -22,6 +24,7 @@ pub async fn run(tx: mpsc::Sender, addr: String) -> anyhow::Result<()> .route("/text", post(post_text)) .route("/image", post(post_image)) .route("/chat_message", post(post_chat_message)) + .fallback(get(r#static::get_static_file)) .layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB .with_state(Server { tx }); diff --git a/showbits-thermal-printer/src/server/static.rs b/showbits-thermal-printer/src/server/static.rs new file mode 100644 index 0000000..5233d6d --- /dev/null +++ b/showbits-thermal-printer/src/server/static.rs @@ -0,0 +1,28 @@ +use axum::{ + http::{header, StatusCode, Uri}, + response::{IntoResponse, Response}, +}; +use rust_embed::RustEmbed; + +#[derive(RustEmbed)] +#[folder = "static"] +struct StaticFiles; + +struct StaticFile(pub String); + +impl IntoResponse for StaticFile { + fn into_response(self) -> Response { + match StaticFiles::get(&self.0) { + None => (StatusCode::NOT_FOUND, "404 Not Found").into_response(), + Some(file) => { + let mime = mime_guess::from_path(self.0).first_or_octet_stream(); + ([(header::CONTENT_TYPE, mime.as_ref())], file.data).into_response() + } + } + } +} + +pub async fn get_static_file(uri: Uri) -> impl IntoResponse { + let path = uri.path().trim_start_matches('/').to_string(); + StaticFile(path) +} diff --git a/showbits-thermal-printer/static/image.html b/showbits-thermal-printer/static/image.html new file mode 100644 index 0000000..6054f4e --- /dev/null +++ b/showbits-thermal-printer/static/image.html @@ -0,0 +1,18 @@ + + + + + + TP: Image + + +

Upload an image

+
+
    +
  1. +
  2. +
  3. Come back to this page
  4. +
+
+ + diff --git a/showbits-thermal-printer/static/index.html b/showbits-thermal-printer/static/index.html new file mode 100644 index 0000000..f3be9de --- /dev/null +++ b/showbits-thermal-printer/static/index.html @@ -0,0 +1,14 @@ + + + + + + TP: Index + + +

Thermal Printer Control

+ + +