Infer mimetype from album art buffer

This commit is contained in:
Sijmen 2023-04-27 04:03:17 +02:00
parent b2ff3b60c8
commit 726268b005
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
3 changed files with 47 additions and 2 deletions

41
Cargo.lock generated
View File

@ -436,12 +436,29 @@ version = "3.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b1ce199063694f33ffb7dd4e0ee620741495c32833cde5aa08f02a0bf96f0c8" checksum = "9b1ce199063694f33ffb7dd4e0ee620741495c32833cde5aa08f02a0bf96f0c8"
[[package]]
name = "byteorder"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.79" version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" 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]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "0.1.10" version = "0.1.10"
@ -663,6 +680,7 @@ dependencies = [
"askama", "askama",
"askama_tide", "askama_tide",
"async-std", "async-std",
"infer 0.13.0",
"mpdrs", "mpdrs",
"serde", "serde",
"serde_qs 0.12.0", "serde_qs 0.12.0",
@ -733,6 +751,12 @@ dependencies = [
"web-sys", "web-sys",
] ]
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]] [[package]]
name = "form_urlencoded" name = "form_urlencoded"
version = "1.1.0" version = "1.1.0"
@ -923,7 +947,7 @@ dependencies = [
"base64 0.13.1", "base64 0.13.1",
"cookie", "cookie",
"futures-lite", "futures-lite",
"infer", "infer 0.2.3",
"pin-project-lite 0.2.9", "pin-project-lite 0.2.9",
"rand 0.7.3", "rand 0.7.3",
"serde", "serde",
@ -988,6 +1012,15 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64e9829a50b42bb782c1df523f78d332fe371b10c661e78b7a3c34b0198e9fac" checksum = "64e9829a50b42bb782c1df523f78d332fe371b10c661e78b7a3c34b0198e9fac"
[[package]]
name = "infer"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f551f8c3a39f68f986517db0d1759de85881894fdc7db798bd2a9df9cb04b7fc"
dependencies = [
"cfb",
]
[[package]] [[package]]
name = "instant" name = "instant"
version = "0.1.12" version = "0.1.12"
@ -1933,6 +1966,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "uuid"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b55a3fef2a1e3b3a00ce878640918820d3c51081576ac657d23af9fc7928fdb"
[[package]] [[package]]
name = "valuable" name = "valuable"
version = "0.1.0" version = "0.1.0"

View File

@ -10,6 +10,7 @@ anyhow = "1.0.70"
askama = "0.12.0" askama = "0.12.0"
askama_tide = "0.15.0" askama_tide = "0.15.0"
async-std = { version = "1.12.0", features = ["attributes"] } async-std = { version = "1.12.0", features = ["attributes"] }
infer = "0.13.0"
mpdrs = "0.1.0" mpdrs = "0.1.0"
serde = { version = "1.0.160", features = ["derive"] } serde = { version = "1.0.160", features = ["derive"] }
serde_qs = "0.12.0" serde_qs = "0.12.0"

View File

@ -87,7 +87,7 @@ async fn get_player(_req: tide::Request<()>) -> tide::Result {
}; };
if let Some(song) = song { 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); 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 { async fn get_art(req: tide::Request<()>) -> tide::Result {
let query: IndexQuery = req.query()?; let query: IndexQuery = req.query()?;
let resp = if let Ok(art) = mpd::connect()?.albumart(&query.path) { 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) tide::Response::builder(tide::StatusCode::Ok)
.body(art) .body(art)
.content_type(mime)
.header("cache-control", "max-age=3600") .header("cache-control", "max-age=3600")
} else { } else {
tide::Response::builder(tide::StatusCode::NotFound) tide::Response::builder(tide::StatusCode::NotFound)