|
| 1 | +//! Icon retrieval and caching for file types. |
| 2 | +
|
| 3 | +use base64::Engine; |
| 4 | +use file_icon_provider::get_file_icon; |
| 5 | +use image::{DynamicImage, ImageFormat, imageops::FilterType}; |
| 6 | +use std::collections::HashMap; |
| 7 | +use std::io::Cursor; |
| 8 | +use std::path::Path; |
| 9 | +use std::sync::RwLock; |
| 10 | + |
| 11 | +/// Icon size in pixels (32x32 for retina display) |
| 12 | +const ICON_SIZE: u32 = 32; |
| 13 | + |
| 14 | +/// Cache for generated icons (icon_id -> base64 WebP data URL) |
| 15 | +static ICON_CACHE: RwLock<Option<HashMap<String, String>>> = RwLock::new(None); |
| 16 | + |
| 17 | +/// Initializes the icon cache if not already done. |
| 18 | +fn ensure_cache() { |
| 19 | + let cache = ICON_CACHE.read().unwrap(); |
| 20 | + if cache.is_some() { |
| 21 | + return; |
| 22 | + } |
| 23 | + drop(cache); |
| 24 | + let mut cache = ICON_CACHE.write().unwrap(); |
| 25 | + if cache.is_none() { |
| 26 | + *cache = Some(HashMap::new()); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/// Gets cached icon data URL for the given icon ID, if available. |
| 31 | +fn get_cached_icon(icon_id: &str) -> Option<String> { |
| 32 | + ensure_cache(); |
| 33 | + let cache = ICON_CACHE.read().unwrap(); |
| 34 | + cache.as_ref()?.get(icon_id).cloned() |
| 35 | +} |
| 36 | + |
| 37 | +/// Caches an icon data URL. |
| 38 | +fn cache_icon(icon_id: String, data_url: String) { |
| 39 | + ensure_cache(); |
| 40 | + let mut cache = ICON_CACHE.write().unwrap(); |
| 41 | + if let Some(ref mut map) = *cache { |
| 42 | + map.insert(icon_id, data_url); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// Converts an image to a base64 WebP data URL. |
| 47 | +fn image_to_data_url(img: &DynamicImage) -> Option<String> { |
| 48 | + // Resize to 32x32 |
| 49 | + let resized = img.resize_exact(ICON_SIZE, ICON_SIZE, FilterType::Lanczos3); |
| 50 | + |
| 51 | + // Encode as WebP |
| 52 | + let mut buffer = Cursor::new(Vec::new()); |
| 53 | + resized.write_to(&mut buffer, ImageFormat::WebP).ok()?; |
| 54 | + |
| 55 | + // Convert to base64 data URL |
| 56 | + let base64 = base64::engine::general_purpose::STANDARD.encode(buffer.into_inner()); |
| 57 | + Some(format!("data:image/webp;base64,{}", base64)) |
| 58 | +} |
| 59 | + |
| 60 | +/// Fetches icon for a specific file path. |
| 61 | +fn fetch_icon_for_path(path: &Path) -> Option<String> { |
| 62 | + // Get icon from OS (size is u16) |
| 63 | + let icon = get_file_icon(path, ICON_SIZE as u16).ok()?; |
| 64 | + |
| 65 | + // file_icon_provider returns Icon with width, height, and RGBA pixels |
| 66 | + let img = image::RgbaImage::from_raw(icon.width, icon.height, icon.pixels)?; |
| 67 | + let dynamic_img = DynamicImage::ImageRgba8(img); |
| 68 | + |
| 69 | + image_to_data_url(&dynamic_img) |
| 70 | +} |
| 71 | + |
| 72 | +/// Generates icon ID based on file properties. |
| 73 | +/// This is called during list_directory. |
| 74 | +pub fn generate_icon_id(is_dir: bool, is_symlink: bool, extension: Option<&str>) -> String { |
| 75 | + if is_symlink { |
| 76 | + return "symlink".to_string(); |
| 77 | + } |
| 78 | + if is_dir { |
| 79 | + return "dir".to_string(); |
| 80 | + } |
| 81 | + match extension { |
| 82 | + Some(ext) => format!("ext:{}", ext.to_lowercase()), |
| 83 | + None => "file".to_string(), |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/// Gets the sample file path to use for fetching an icon by ID. |
| 88 | +/// For extension-based icons, we create a temp file with that extension. |
| 89 | +fn get_sample_path_for_icon_id(icon_id: &str) -> Option<std::path::PathBuf> { |
| 90 | + if icon_id == "dir" { |
| 91 | + // Use home directory as sample directory |
| 92 | + return dirs::home_dir(); |
| 93 | + } |
| 94 | + if icon_id == "symlink" { |
| 95 | + // Symlinks use a generic file icon |
| 96 | + return Some(std::path::PathBuf::from("/tmp")); |
| 97 | + } |
| 98 | + if icon_id == "file" { |
| 99 | + // Generic file with no extension |
| 100 | + return Some(std::path::PathBuf::from("/tmp/file")); |
| 101 | + } |
| 102 | + if let Some(ext) = icon_id.strip_prefix("ext:") { |
| 103 | + // Create a fake path with the extension to get the right icon |
| 104 | + return Some(std::path::PathBuf::from(format!("/tmp/sample.{}", ext))); |
| 105 | + } |
| 106 | + None |
| 107 | +} |
| 108 | + |
| 109 | +/// Fetches icons for the given icon IDs that are not already cached. |
| 110 | +/// Returns a map of icon_id -> data URL. |
| 111 | +pub fn get_icons(icon_ids: Vec<String>) -> HashMap<String, String> { |
| 112 | + let mut result = HashMap::new(); |
| 113 | + |
| 114 | + for icon_id in icon_ids { |
| 115 | + // Check cache first |
| 116 | + if let Some(cached) = get_cached_icon(&icon_id) { |
| 117 | + result.insert(icon_id, cached); |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + // Not cached, fetch it |
| 122 | + if let Some(sample_path) = get_sample_path_for_icon_id(&icon_id) |
| 123 | + && let Some(data_url) = fetch_icon_for_path(&sample_path) |
| 124 | + { |
| 125 | + cache_icon(icon_id.clone(), data_url.clone()); |
| 126 | + result.insert(icon_id, data_url); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + result |
| 131 | +} |
| 132 | + |
| 133 | +#[cfg(test)] |
| 134 | +mod tests { |
| 135 | + use super::*; |
| 136 | + |
| 137 | + #[test] |
| 138 | + fn test_generate_icon_id_directory() { |
| 139 | + assert_eq!(generate_icon_id(true, false, None), "dir"); |
| 140 | + } |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn test_generate_icon_id_symlink() { |
| 144 | + assert_eq!(generate_icon_id(false, true, Some("txt")), "symlink"); |
| 145 | + } |
| 146 | + |
| 147 | + #[test] |
| 148 | + fn test_generate_icon_id_extension() { |
| 149 | + assert_eq!(generate_icon_id(false, false, Some("PDF")), "ext:pdf"); |
| 150 | + assert_eq!(generate_icon_id(false, false, Some("jpg")), "ext:jpg"); |
| 151 | + } |
| 152 | + |
| 153 | + #[test] |
| 154 | + fn test_generate_icon_id_no_extension() { |
| 155 | + assert_eq!(generate_icon_id(false, false, None), "file"); |
| 156 | + } |
| 157 | +} |
0 commit comments