下载/主界面优化

This commit is contained in:
2026-07-22 22:26:40 +08:00
parent f7d3c13f35
commit 95a69b0017
16 changed files with 295 additions and 106 deletions
+8
View File
@@ -38,6 +38,10 @@ pub struct Storage {
impl Storage {
pub fn new(data_dir: PathBuf) -> Self {
// 确保数据目录存在(首次启动或目录被删除时自动创建)
if let Err(e) = fs::create_dir_all(&data_dir) {
eprintln!("[download_engine] 创建数据目录失败: {} ({})", data_dir.display(), e);
}
let state_path = data_dir.join("engine_state.json");
let existing = Self::load_raw(&state_path);
let next_id = existing.as_ref().map(|s| s.next_id).unwrap_or(1);
@@ -70,6 +74,10 @@ impl Storage {
state.next_id = self.id_counter.load(Ordering::SeqCst);
match serde_json::to_string_pretty(&state) {
Ok(json) => {
// 兜底:若父目录被外部删除则在写入前重建
if let Some(parent) = self.state_path.parent() {
let _ = fs::create_dir_all(parent);
}
if let Err(e) = fs::write(&self.state_path, json) {
eprintln!("[download_engine] 保存状态失败: {}", e);
}