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())
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user