fix(settings): 下载目录授权不再覆盖 data-dir.txt(数据库目录显示/打开错位)
This commit is contained in:
1 parent
ada8916fd6
commit
857acd30d3
3 files changed
+11
-7
No files matched your search
@@ -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<String, String> {
|
||||
fn grant_dir(app: AppHandle, dir: String, record: Option<bool>) -> Result<String, String> {
|
||||
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<String, String> {
|
||||
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())
|
||||
}
|
||||
|
||||
+1
-1
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user