From 44a58ff590dce7c9c6d0d0a1f7f41fc3d45b5f4b Mon Sep 17 00:00:00 2001 From: selikhov Date: Sun, 4 Oct 2026 10:57:26 +0300 Subject: [PATCH] Show downloaded avatars and icons: decode cached files by content The image cache stores downloads without an extension (avatars/, icons/), but the UI loaded them with Image::load_from_path, which picks the decoder from the extension. Every avatar and server icon failed with "The image format could not be determined" and was drawn empty. Read the bytes and let Image::load_from_data sniff the format, which also covers the SVG icons TeamSpeak 6 servers keep. Co-Authored-By: Claude Opus 5.5 --- crates/voelin-ui/src/images.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/voelin-ui/src/images.rs b/crates/voelin-ui/src/images.rs index fed489c..c986b61 100644 --- a/crates/voelin-ui/src/images.rs +++ b/crates/voelin-ui/src/images.rs @@ -108,7 +108,11 @@ pub fn file(path: &std::path::Path) -> Image { CACHE .with(|c| { c.borrow_mut().get_or_load(&key, || { - let image = Image::load_from_path(path).ok()?; + // By content, not by name: the cache's files have no extension + // (`avatars/`, `icons/`), and servers keep PNG, JPEG + // and SVG icons alike. + let bytes = std::fs::read(path).ok()?; + let image = Image::load_from_data(&bytes, None).ok()?; let size = image.size(); Some((image, size.width as usize * size.height as usize * 4)) }) @@ -191,6 +195,26 @@ mod tests { assert_eq!(picture("test:junk", b"not a picture").size().width, 0); } + /// Cached avatars and icons are named without an extension. + #[test] + fn cached_files_decode_by_content() { + let dir = std::env::temp_dir().join(format!("voelin-images-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let mut png = Vec::new(); + let mut encoder = png::Encoder::new(&mut png, 3, 2); + encoder.set_color(png::ColorType::Rgba); + encoder.set_depth(png::BitDepth::Eight); + encoder.write_header().unwrap().write_image_data(&[90; 24]).unwrap(); + let svg = br#" +"#; + for (name, bytes, width) in [("287478770", &png[..], 3), ("413901487", &svg[..], 16)] { + let path = dir.join(name); + std::fs::write(&path, bytes).unwrap(); + assert_eq!(file(&path).size().width, width, "{name}"); + } + std::fs::remove_dir_all(dir).unwrap(); + } + #[test] fn emoji_images_decode() { let image = emoji("1f600");