|
| 1 | +use crate::app_state::AppState; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | +use serde_json::{self, json, Value}; |
| 4 | +use socketioxide::extract::{AckSender, Data, SocketRef, State}; |
| 5 | +use std::fs; |
| 6 | +use std::path::PathBuf; |
| 7 | +use tracing::info; |
| 8 | + |
| 9 | +#[derive(Debug, Serialize, Deserialize)] |
| 10 | +pub struct ThemeListResponseItem { |
| 11 | + pub id: String, |
| 12 | + pub name: String, |
| 13 | + #[serde(rename = "fileName")] |
| 14 | + pub file_name: String, |
| 15 | + #[serde(rename = "themeName")] |
| 16 | + pub theme_name: String, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug, Deserialize)] |
| 20 | +pub struct ThemeGetRequest { |
| 21 | + #[serde(rename = "fileName")] |
| 22 | + pub file_name: String, |
| 23 | + #[serde(rename = "themeName")] |
| 24 | + pub theme_name: String, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Debug, Deserialize)] |
| 28 | +struct ThemeFile { |
| 29 | + themes: Vec<ThemeDefinition>, |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Debug, Deserialize)] |
| 33 | +struct ThemeDefinition { |
| 34 | + name: String, |
| 35 | + mode: String, |
| 36 | + colors: Value, |
| 37 | + highlight: Value, |
| 38 | +} |
| 39 | + |
| 40 | +fn get_themes_dir() -> PathBuf { |
| 41 | + PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 42 | + .parent() |
| 43 | + .map(|p| p.join("themes")) |
| 44 | + .unwrap_or_else(|| PathBuf::from("themes")) |
| 45 | +} |
| 46 | + |
| 47 | +pub async fn handle_theme_list( |
| 48 | + _socket: SocketRef, |
| 49 | + ack: AckSender, |
| 50 | +) { |
| 51 | + info!("theme:list requested"); |
| 52 | + let mut list = Vec::new(); |
| 53 | + let themes_dir = get_themes_dir(); |
| 54 | + let mut seen_files = std::collections::HashSet::new(); |
| 55 | + |
| 56 | + if let Ok(entries) = fs::read_dir(themes_dir) { |
| 57 | + for entry in entries.filter_map(Result::ok) { |
| 58 | + let path = entry.path(); |
| 59 | + if path.extension().and_then(|s| s.to_str()) == Some("json") { |
| 60 | + let file_name = match path.file_name().and_then(|s| s.to_str()) { |
| 61 | + Some(name) => name.to_string(), |
| 62 | + None => continue, |
| 63 | + }; |
| 64 | + if let Ok(content) = fs::read_to_string(&path) { |
| 65 | + if let Ok(theme_file) = serde_json::from_str::<ThemeFile>(&content) { |
| 66 | + seen_files.insert(file_name.clone()); |
| 67 | + for t in theme_file.themes { |
| 68 | + let id = format!("{}:{}", file_name, t.name); |
| 69 | + list.push(ThemeListResponseItem { |
| 70 | + id, |
| 71 | + name: format!("{} ({})", t.name, t.mode), |
| 72 | + file_name: file_name.clone(), |
| 73 | + theme_name: t.name, |
| 74 | + }); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + for embedded_file in crate::config::Themes::iter() { |
| 83 | + let file_name = embedded_file.as_ref().to_string(); |
| 84 | + if !seen_files.contains(&file_name) { |
| 85 | + if let Some(file_data) = crate::config::Themes::get(&file_name) { |
| 86 | + if let Ok(content) = std::str::from_utf8(file_data.data.as_ref()) { |
| 87 | + if let Ok(theme_file) = serde_json::from_str::<ThemeFile>(content) { |
| 88 | + for t in theme_file.themes { |
| 89 | + let id = format!("{}:{}", file_name, t.name); |
| 90 | + list.push(ThemeListResponseItem { |
| 91 | + id, |
| 92 | + name: format!("{} ({})", t.name, t.mode), |
| 93 | + file_name: file_name.clone(), |
| 94 | + theme_name: t.name, |
| 95 | + }); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Sort list by name for nicer presentation |
| 104 | + list.sort_by(|a, b| a.name.cmp(&b.name)); |
| 105 | + |
| 106 | + let _ = ack.send(&json!(list)); |
| 107 | +} |
| 108 | + |
| 109 | +pub async fn handle_theme_get( |
| 110 | + _socket: SocketRef, |
| 111 | + Data(request): Data<ThemeGetRequest>, |
| 112 | + ack: AckSender, |
| 113 | +) { |
| 114 | + info!("theme:get requested: {:?}", request); |
| 115 | + let themes_dir = get_themes_dir(); |
| 116 | + |
| 117 | + let theme_file_path = themes_dir.join(&request.file_name); |
| 118 | + // Basic security check to avoid path traversal |
| 119 | + if theme_file_path.parent() != Some(&themes_dir) { |
| 120 | + let _ = ack.send(&json!({ "success": false, "error": "Invalid theme file path" })); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + let content = match fs::read_to_string(&theme_file_path) { |
| 125 | + Ok(c) => Some(c), |
| 126 | + Err(_) => { |
| 127 | + // Fallback to embedded theme asset |
| 128 | + crate::config::Themes::get(&request.file_name) |
| 129 | + .and_then(|file_data| std::str::from_utf8(file_data.data.as_ref()).ok().map(|s| s.to_string())) |
| 130 | + } |
| 131 | + }; |
| 132 | + |
| 133 | + let content = match content { |
| 134 | + Some(c) => c, |
| 135 | + None => { |
| 136 | + let _ = ack.send(&json!({ "success": false, "error": format!("Theme file not found: {}", request.file_name) })); |
| 137 | + return; |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + let theme_file = match serde_json::from_str::<ThemeFile>(&content) { |
| 142 | + Ok(f) => f, |
| 143 | + Err(e) => { |
| 144 | + let _ = ack.send(&json!({ "success": false, "error": format!("Failed to parse theme: {}", e) })); |
| 145 | + return; |
| 146 | + } |
| 147 | + }; |
| 148 | + |
| 149 | + if let Some(theme_def) = theme_file.themes.into_iter().find(|t| t.name == request.theme_name) { |
| 150 | + let _ = ack.send(&json!({ |
| 151 | + "success": true, |
| 152 | + "theme": { |
| 153 | + "name": theme_def.name, |
| 154 | + "mode": theme_def.mode, |
| 155 | + "colors": theme_def.colors, |
| 156 | + "highlight": theme_def.highlight, |
| 157 | + } |
| 158 | + })); |
| 159 | + } else { |
| 160 | + let _ = ack.send(&json!({ "success": false, "error": "Theme name not found in file" })); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +pub async fn handle_config_get( |
| 165 | + _socket: SocketRef, |
| 166 | + ack: AckSender, |
| 167 | + state: State<AppState>, |
| 168 | +) { |
| 169 | + info!("config:get requested"); |
| 170 | + let autosave = state.config.lock().await.autosave; |
| 171 | + let _ = ack.send(&json!({ "success": true, "autosave": autosave })); |
| 172 | +} |
0 commit comments