From 726268b0050ef0a7128bf569b832d39856318b9b Mon Sep 17 00:00:00 2001 From: Sijmen Date: Thu, 27 Apr 2023 04:03:17 +0200 Subject: [PATCH] Infer mimetype from album art buffer --- Cargo.lock | 41 ++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/main.rs | 7 ++++++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7632b8a..c7ae23e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -436,12 +436,29 @@ version = "3.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b1ce199063694f33ffb7dd4e0ee620741495c32833cde5aa08f02a0bf96f0c8" +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + [[package]] name = "cc" version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" +[[package]] +name = "cfb" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d38f2da7a0a2c4ccf0065be06397cc26a81f4e528be095826eee9d4adbb8c60f" +dependencies = [ + "byteorder", + "fnv", + "uuid", +] + [[package]] name = "cfg-if" version = "0.1.10" @@ -663,6 +680,7 @@ dependencies = [ "askama", "askama_tide", "async-std", + "infer 0.13.0", "mpdrs", "serde", "serde_qs 0.12.0", @@ -733,6 +751,12 @@ dependencies = [ "web-sys", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "form_urlencoded" version = "1.1.0" @@ -923,7 +947,7 @@ dependencies = [ "base64 0.13.1", "cookie", "futures-lite", - "infer", + "infer 0.2.3", "pin-project-lite 0.2.9", "rand 0.7.3", "serde", @@ -988,6 +1012,15 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "64e9829a50b42bb782c1df523f78d332fe371b10c661e78b7a3c34b0198e9fac" +[[package]] +name = "infer" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f551f8c3a39f68f986517db0d1759de85881894fdc7db798bd2a9df9cb04b7fc" +dependencies = [ + "cfb", +] + [[package]] name = "instant" version = "0.1.12" @@ -1933,6 +1966,12 @@ dependencies = [ "serde", ] +[[package]] +name = "uuid" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b55a3fef2a1e3b3a00ce878640918820d3c51081576ac657d23af9fc7928fdb" + [[package]] name = "valuable" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 98c49df..22cb8e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ anyhow = "1.0.70" askama = "0.12.0" askama_tide = "0.15.0" async-std = { version = "1.12.0", features = ["attributes"] } +infer = "0.13.0" mpdrs = "0.1.0" serde = { version = "1.0.160", features = ["derive"] } serde_qs = "0.12.0" diff --git a/src/main.rs b/src/main.rs index 30fa3d1..be60c35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,7 +87,7 @@ async fn get_player(_req: tide::Request<()>) -> tide::Result { }; if let Some(song) = song { - let name = song.title.unwrap_or(song.file.clone()).to_string(); + let name = song.title.unwrap_or(song.file); template.name = Some(name); } @@ -150,8 +150,13 @@ async fn post_next(_req: tide::Request<()>) -> tide::Result { async fn get_art(req: tide::Request<()>) -> tide::Result { let query: IndexQuery = req.query()?; let resp = if let Ok(art) = mpd::connect()?.albumart(&query.path) { + let mime = infer::get(&art) + .map(|k| k.mime_type()) + .unwrap_or("application/octet-stream"); + tide::Response::builder(tide::StatusCode::Ok) .body(art) + .content_type(mime) .header("cache-control", "max-age=3600") } else { tide::Response::builder(tide::StatusCode::NotFound)