refactor: move deleted tauri APIs, prepare for next release (#355)

This commit is contained in:
Lucas Fernandes Nogueira
2023-05-13 08:32:30 -07:00
committed by GitHub
parent 937e6a5be6
commit 702b7b36bd
45 changed files with 1412 additions and 31963 deletions
+44 -39
View File
@@ -6,8 +6,9 @@ use aho_corasick::AhoCorasick;
use serde::{Deserialize, Serialize};
use tauri::{
plugin::{Builder, TauriPlugin},
AppHandle, FsScopeEvent, Manager, Runtime,
AppHandle, Manager, Runtime,
};
use tauri_plugin_fs::{FsExt, ScopeEvent as FsScopeEvent};
use std::{
fs::{create_dir_all, File},
@@ -59,45 +60,45 @@ fn fix_pattern(ac: &AhoCorasick, s: &str) -> String {
}
fn save_scopes<R: Runtime>(app: &AppHandle<R>, app_dir: &Path, scope_state_path: &Path) {
let fs_scope = app.fs_scope();
if let Some(fs_scope) = app.try_fs_scope() {
let scope = Scope {
allowed_paths: fs_scope
.allowed_patterns()
.into_iter()
.map(|p| p.to_string())
.collect(),
forbidden_patterns: fs_scope
.forbidden_patterns()
.into_iter()
.map(|p| p.to_string())
.collect(),
};
let scope = Scope {
allowed_paths: fs_scope
.allowed_patterns()
.into_iter()
.map(|p| p.to_string())
.collect(),
forbidden_patterns: fs_scope
.forbidden_patterns()
.into_iter()
.map(|p| p.to_string())
.collect(),
};
let _ = create_dir_all(app_dir)
.and_then(|_| File::create(scope_state_path))
.map_err(Error::Io)
.and_then(|mut f| {
f.write_all(&bincode::serialize(&scope).map_err(Error::from)?)
.map_err(Into::into)
});
let _ = create_dir_all(app_dir)
.and_then(|_| File::create(scope_state_path))
.map_err(Error::Io)
.and_then(|mut f| {
f.write_all(&bincode::serialize(&scope).map_err(Error::from)?)
.map_err(Into::into)
});
}
}
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("persisted-scope")
.setup(|app, _api| {
let fs_scope = app.fs_scope();
#[cfg(feature = "protocol-asset")]
let asset_protocol_scope = app.asset_protocol_scope();
let fs_scope = app.try_fs_scope();
let core_scopes = app.state::<tauri::scope::Scopes>();
let app = app.clone();
let app_dir = app.path().app_data_dir();
if let Ok(app_dir) = app_dir {
let scope_state_path = app_dir.join(SCOPE_STATE_FILENAME);
let _ = fs_scope.forbid_file(&scope_state_path);
#[cfg(feature = "protocol-asset")]
let _ = asset_protocol_scope.forbid_file(&scope_state_path);
if let Some(s) = fs_scope {
let _ = s.forbid_file(&scope_state_path);
}
let _ = core_scopes.forbid_file(&scope_state_path);
// We're trying to fix broken .persisted-scope files seamlessly, so we'll be running this on the values read on the saved file.
// We will still save some semi-broken values because the scope events are quite spammy and we don't want to reduce runtime performance any further.
@@ -111,16 +112,18 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
for allowed in &scope.allowed_paths {
let allowed = fix_pattern(&ac, allowed);
let _ = fs_scope.allow_file(&allowed);
#[cfg(feature = "protocol-asset")]
let _ = asset_protocol_scope.allow_file(&allowed);
if let Some(s) = fs_scope {
let _ = s.allow_file(&allowed);
}
let _ = core_scopes.allow_file(&allowed);
}
for forbidden in &scope.forbidden_patterns {
let forbidden = fix_pattern(&ac, forbidden);
let _ = fs_scope.forbid_file(&forbidden);
#[cfg(feature = "protocol-asset")]
let _ = asset_protocol_scope.forbid_file(&forbidden);
if let Some(s) = fs_scope {
let _ = s.forbid_file(&forbidden);
}
let _ = core_scopes.forbid_file(&forbidden);
}
// Manually save the fixed scopes to disk once.
@@ -128,11 +131,13 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
save_scopes(&app, &app_dir, &scope_state_path);
}
fs_scope.listen(move |event| {
if let FsScopeEvent::PathAllowed(_) = event {
save_scopes(&app, &app_dir, &scope_state_path);
}
});
if let Some(s) = fs_scope {
s.listen(move |event| {
if let FsScopeEvent::PathAllowed(_) = event {
save_scopes(&app, &app_dir, &scope_state_path);
}
});
}
}
Ok(())
})