Skip to content

Commit f411d41

Browse files
committed
refactor: simplify packaged webui resolution flow
1 parent f2359d6 commit f411d41

1 file changed

Lines changed: 80 additions & 45 deletions

File tree

src-tauri/src/main.rs

Lines changed: 80 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -311,46 +311,13 @@ impl BackendState {
311311
resolve_resource_path(app, "webui/index.html")
312312
.and_then(|index_path| index_path.parent().map(Path::to_path_buf))
313313
});
314-
let fallback_webui_dir = packaged_fallback_webui_dir(root_dir.as_deref());
315-
let webui_dir = match embedded_webui_dir {
316-
Some(candidate) if candidate.join("index.html").is_file() => candidate,
317-
Some(candidate) => {
318-
append_desktop_log(&format!(
319-
"packaged webui index is missing at {}, trying fallback data/dist",
320-
candidate.join("index.html").display()
321-
));
322-
if let Some(fallback) = fallback_webui_dir {
323-
append_desktop_log(&format!(
324-
"using fallback webui directory: {}",
325-
fallback.display()
326-
));
327-
fallback
328-
} else {
329-
return Err(format!(
330-
"Packaged WebUI is unavailable. Missing embedded index at {} and fallback data/dist. Please reinstall AstrBot or download the matching dist.zip to data/dist.",
331-
candidate.join("index.html").display()
332-
));
333-
}
334-
}
335-
None => {
336-
if let Some(fallback) = fallback_webui_dir {
337-
append_desktop_log(&format!(
338-
"embedded webui directory not found, using fallback webui directory: {}",
339-
fallback.display()
340-
));
341-
fallback
342-
} else {
343-
return Err(
344-
"Packaged WebUI directory is missing and fallback data/dist is unavailable. Please reinstall AstrBot or download the matching dist.zip to data/dist."
345-
.to_string(),
346-
);
347-
}
348-
}
349-
};
314+
let webui_dir = resolve_packaged_webui_dir(embedded_webui_dir, root_dir.as_deref())?;
350315

351-
let mut args = vec![launch_script_path.to_string_lossy().to_string()];
352-
args.push("--webui-dir".to_string());
353-
args.push(webui_dir.to_string_lossy().to_string());
316+
let args = vec![
317+
launch_script_path.to_string_lossy().to_string(),
318+
"--webui-dir".to_string(),
319+
webui_dir.to_string_lossy().to_string(),
320+
];
354321

355322
let plan = LaunchPlan {
356323
cmd: python_path.to_string_lossy().to_string(),
@@ -2211,15 +2178,83 @@ fn default_packaged_root_dir() -> Option<PathBuf> {
22112178
dirs::home_dir().map(|home| home.join(".astrbot"))
22122179
}
22132180

2181+
fn packaged_fallback_webui_probe_dir(root_dir: Option<&Path>) -> Option<PathBuf> {
2182+
match root_dir {
2183+
Some(root) => Some(root.join("data").join("dist")),
2184+
None => default_packaged_root_dir().map(|root| root.join("data").join("dist")),
2185+
}
2186+
}
2187+
22142188
fn packaged_fallback_webui_dir(root_dir: Option<&Path>) -> Option<PathBuf> {
2215-
let root = root_dir
2216-
.map(Path::to_path_buf)
2217-
.or_else(default_packaged_root_dir)?;
2218-
let candidate = root.join("data").join("dist");
2189+
let candidate = packaged_fallback_webui_probe_dir(root_dir)?;
22192190
if candidate.join("index.html").is_file() {
2220-
return Some(candidate);
2191+
Some(candidate)
2192+
} else {
2193+
None
2194+
}
2195+
}
2196+
2197+
fn resolve_packaged_webui_dir(
2198+
embedded_webui_dir: Option<PathBuf>,
2199+
root_dir: Option<&Path>,
2200+
) -> Result<PathBuf, String> {
2201+
let fallback_webui_dir = packaged_fallback_webui_dir(root_dir);
2202+
let fallback_index_path = packaged_fallback_webui_probe_dir(root_dir)
2203+
.map(|path| path.join("index.html").display().to_string());
2204+
2205+
match embedded_webui_dir {
2206+
Some(candidate) => {
2207+
let embedded_index = candidate.join("index.html");
2208+
if embedded_index.is_file() {
2209+
return Ok(candidate);
2210+
}
2211+
2212+
append_desktop_log(&format!(
2213+
"packaged webui index is missing at {}, trying fallback data/dist",
2214+
embedded_index.display()
2215+
));
2216+
2217+
if let Some(fallback) = fallback_webui_dir {
2218+
append_desktop_log(&format!(
2219+
"using fallback webui directory: {}",
2220+
fallback.display()
2221+
));
2222+
return Ok(fallback);
2223+
}
2224+
2225+
let fallback_index = fallback_index_path.unwrap_or_else(|| "<unresolved>".to_string());
2226+
append_desktop_log(&format!(
2227+
"packaged webui resolution failed: embedded index missing at {}, fallback index missing at {}",
2228+
embedded_index.display(),
2229+
fallback_index
2230+
));
2231+
2232+
Err(format!(
2233+
"Packaged WebUI is unavailable. Missing embedded index at {} and fallback data/dist. Please reinstall AstrBot or download the matching dist.zip to data/dist.",
2234+
embedded_index.display()
2235+
))
2236+
}
2237+
None => {
2238+
if let Some(fallback) = fallback_webui_dir {
2239+
append_desktop_log(&format!(
2240+
"embedded webui directory not found, using fallback webui directory: {}",
2241+
fallback.display()
2242+
));
2243+
return Ok(fallback);
2244+
}
2245+
2246+
let fallback_index = fallback_index_path.unwrap_or_else(|| "<unresolved>".to_string());
2247+
append_desktop_log(&format!(
2248+
"packaged webui resolution failed: embedded webui directory is missing, fallback index missing at {}",
2249+
fallback_index
2250+
));
2251+
2252+
Err(
2253+
"Packaged WebUI directory is missing and fallback data/dist is unavailable. Please reinstall AstrBot or download the matching dist.zip to data/dist."
2254+
.to_string(),
2255+
)
2256+
}
22212257
}
2222-
None
22232258
}
22242259

22252260
fn resolve_backend_timeout_ms(packaged_mode: bool) -> Option<Duration> {

0 commit comments

Comments
 (0)