From 857acd30d32864005be3845c26055c877bfaa6ef Mon Sep 17 00:00:00 2001 From: qiqi Date: Tue, 17 Nov 2026 08:57:37 +0800 Subject: [PATCH] =?UTF-8?q?fix(settings):=20=E4=B8=8B=E8=BD=BD=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E6=8E=88=E6=9D=83=E4=B8=8D=E5=86=8D=E8=A6=86=E7=9B=96?= =?UTF-8?q?=20data-dir.txt=EF=BC=88=E6=95=B0=E6=8D=AE=E5=BA=93=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E6=98=BE=E7=A4=BA/=E6=89=93=E5=BC=80=E9=94=99?= =?UTF-8?q?=E4=BD=8D=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/lib.rs | 13 ++++++++----- src/storage-file.js | 2 +- src/ui-settings-sections.js | 3 ++- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index d68cff2..574029e 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -127,7 +127,7 @@ fn build_tray(app: &AppHandle) -> tauri::Result<()> { // 为什么需要它:tauri-plugin-fs 的路径白名单是**运行时**的,重启就没了;而资料目录是用户 // 在设置里选的、存在应用配置里 ⇒ 启动时由这里重新授权一次,之后 fs 插件的读写才允许。 #[tauri::command] -fn grant_dir(app: AppHandle, dir: String) -> Result { +fn grant_dir(app: AppHandle, dir: String, record: Option) -> Result { let p = std::path::PathBuf::from(&dir); if !p.is_absolute() { return Err("资料目录必须是绝对路径".into()); @@ -136,10 +136,13 @@ fn grant_dir(app: AppHandle, dir: String) -> Result { app.fs_scope() .allow_directory(&p, true) .map_err(|e| e.to_string())?; - // 记到 app config:这样「打开资料目录」按钮不需要前端传路径,也不给前端任意开目录的能力 - if let Ok(cfg) = app.path().app_config_dir() { - let _ = std::fs::create_dir_all(&cfg); - let _ = std::fs::write(cfg.join("data-dir.txt"), p.to_string_lossy().as_bytes()); + // 只有「资料目录」才记进配置(record 默认 true);下载目录等传 record=false, + // 否则会把 data-dir.txt 覆盖成别的目录,导致「打开」跳到错误位置。 + if record.unwrap_or(true) { + if let Ok(cfg) = app.path().app_config_dir() { + let _ = std::fs::create_dir_all(&cfg); + let _ = std::fs::write(cfg.join("data-dir.txt"), p.to_string_lossy().as_bytes()); + } } Ok(p.to_string_lossy().to_string()) } diff --git a/src/storage-file.js b/src/storage-file.js index 1c45f9c..aa0108b 100644 --- a/src/storage-file.js +++ b/src/storage-file.js @@ -621,7 +621,7 @@ try { const rawDl = global.localStorage.getItem('iwb.downloadDir') const dl = rawDl ? JSON.parse(rawDl) : '' - if (dl) { try { await core().invoke('grant_dir', { dir: String(dl) }) } catch (e) { /* 忽略 */ } } + if (dl) { try { await core().invoke('grant_dir', { dir: String(dl), record: false }) } catch (e) { /* 忽略 */ } } } catch (e) { /* 下载目录指针不存在或格式异常 */ } return true } diff --git a/src/ui-settings-sections.js b/src/ui-settings-sections.js index d1dd192..f17972b 100644 --- a/src/ui-settings-sections.js +++ b/src/ui-settings-sections.js @@ -100,7 +100,8 @@ } catch (e) { setNote('切换失败:' + String((e && e.message) || e)) } } const openData = async () => { - try { const p = await D.openDataDir(); if (p) setDataDir(p) } catch (e) { setNote('打开失败:' + String((e && e.message) || e)) } + // 只负责打开;界面上的路径始终以 F.dir()(真实资料目录)为准,避免被返回值覆盖 + try { await D.openDataDir() } catch (e) { setNote('打开失败:' + String((e && e.message) || e)) } } const migrateData = async () => { try {