release: v3.26.102 相册外部路径重启复授权
This commit is contained in:
1 parent
bac3c32e1c
commit
d604bb39b8
13 files changed
+91
-9
No files matched your search
@@ -191,6 +191,41 @@ fn app_memory_mb() -> u64 {
|
||||
// 把用户**显式选择**的路径加进本次运行的 fs scope(2026-11-26,相册「只引用本地文件」用)。
|
||||
// 为什么单独一条命令:dialog 选中的外部图片不在「资料目录」里,fs 插件默认拒绝读写;
|
||||
// 这里只授权调用方传进来的具体路径,不扩大 $HOME 之类的面。
|
||||
// 用户显式选择过的外部路径:tauri fs scope 是**运行时**的,重启就失效。
|
||||
// 相册的 {__file} 只存路径,若不持久化授权,第二天打开就只剩缩略图缓存、组合/参考图全空。
|
||||
const FILE_GRANTS_FILE: &str = "file-grants.json";
|
||||
|
||||
fn file_grants_path(app: &AppHandle) -> Option<std::path::PathBuf> {
|
||||
app.path().app_config_dir().ok().map(|c| c.join(FILE_GRANTS_FILE))
|
||||
}
|
||||
|
||||
fn persist_file_grants(app: &AppHandle, paths: &[String]) {
|
||||
let Some(p) = file_grants_path(app) else { return };
|
||||
let mut list: Vec<String> = std::fs::read_to_string(&p)
|
||||
.ok()
|
||||
.and_then(|s| serde_json::from_str::<Vec<String>>(&s).ok())
|
||||
.unwrap_or_default();
|
||||
for x in paths {
|
||||
if !x.is_empty() && !list.iter().any(|y| y == x) { list.push(x.clone()) }
|
||||
}
|
||||
if let Some(dir) = p.parent() { let _ = std::fs::create_dir_all(dir); }
|
||||
if let Ok(txt) = serde_json::to_vec(&list) { let _ = std::fs::write(&p, txt); }
|
||||
}
|
||||
|
||||
fn restore_file_grants(app: &AppHandle) {
|
||||
let Some(p) = file_grants_path(app) else { return };
|
||||
let list: Vec<String> = std::fs::read_to_string(&p)
|
||||
.ok()
|
||||
.and_then(|s| serde_json::from_str::<Vec<String>>(&s).ok())
|
||||
.unwrap_or_default();
|
||||
for raw in list {
|
||||
let p = std::path::PathBuf::from(&raw);
|
||||
if !p.is_absolute() || !p.exists() { continue }
|
||||
if p.is_dir() { let _ = app.fs_scope().allow_directory(&p, true); }
|
||||
else { let _ = app.fs_scope().allow_file(&p); }
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn grant_paths(app: AppHandle, paths: Vec<String>) -> Result<Vec<String>, String> {
|
||||
let mut ok: Vec<String> = Vec::new();
|
||||
@@ -208,6 +243,7 @@ fn grant_paths(app: AppHandle, paths: Vec<String>) -> Result<Vec<String>, String
|
||||
ok.push(p.to_string_lossy().to_string());
|
||||
}
|
||||
}
|
||||
if !ok.is_empty() { persist_file_grants(&app, &ok); }
|
||||
Ok(ok)
|
||||
}
|
||||
|
||||
@@ -1322,6 +1358,8 @@ pub fn run() {
|
||||
.build(),
|
||||
)?;
|
||||
}
|
||||
// 重启后恢复用户选择过的外部图片路径(相册 {__file} 依赖;fs scope 不持久)
|
||||
restore_file_grants(app.handle());
|
||||
// 恢复上次窗口大小/位置(主窗口下限与 tauri.conf 的 minWidth/minHeight 对齐)
|
||||
restore_geom(app.handle(), "main", 1024, 640);
|
||||
build_tray(app.handle())?;
|
||||
|
||||
Reference in new issue
Block a user