@setoelkahfi / svara / commits / 4bd1324

fix: added way to add file scopes dynamically

can still access files regardless of user permissions (not secure)

mellbacon committed Jun 24, 2023 at 19:34 UTC 4bd1324b413d8ca60a68ef5f97048d4b3f43aef8
2 files changed +13 -2
src-tauri/src/main.rs
+8 -1
@@ -6,6 +6,8 @@
6 use std::{env, fs};
7 use std::process::Command;
8
9 +use tauri::{Manager};
10 +
11 #[tauri::command]
12 fn open_in_explorer(path: &str) {
13 // FOR OTHER OS REFER - https://doc.rust-lang.org/std/env/consts/constant.OS.html
@@ -19,6 +21,11 @@ fn open_in_explorer(path: &str) {
21 }
22 }
23
24 +#[tauri::command]
25 +fn attempt_file_access(app_handle: tauri::AppHandle, p: &str) {
26 + app_handle.fs_scope().allow_directory(p, true).unwrap();
27 +}
28 +
29 #[tauri::command]
30 fn delete_file(path: &str, perm: bool, is_file: bool) {
31 if perm {
@@ -36,7 +43,7 @@ fn delete_file(path: &str, perm: bool, is_file: bool) {
43
44 fn main() {
45 tauri::Builder::default()
39 - .invoke_handler(tauri::generate_handler![open_in_explorer, delete_file])
46 + .invoke_handler(tauri::generate_handler![open_in_explorer, delete_file, attempt_file_access])
47 .plugin(tauri_plugin_fs_watch::init())
48 .run(tauri::generate_context!())
49 .expect("error while running tauri application");
src/lib/File.ts
+5 -1
@@ -1,4 +1,4 @@
1 -import { dialog, fs, path, invoke } from "@tauri-apps/api";
1 +import { dialog, fs, path, invoke, window } from "@tauri-apps/api";
2 import { get, writable } from 'svelte/store';
3 import { tabs, addEditorTab } from "./EditorTabList.svelte";
4 import { filetree } from "./FileTree.svelte";
@@ -16,6 +16,10 @@ export async function openFolder() {
16 let directory = await dialog.open({directory: true}) as string;
17 if (!directory) return;
18
19 + // if file path is not in the configured scope already, add it
20 + // TODO: should configure this so it doesnt access restricted paths based on user permissions
21 + await invoke("attempt_file_access", {app_handle: window, p: directory});
22 +
23 const directoryName = await updateTree(directory);
24 workspaceName.set(directoryName);
25