From 13a39295e725016daa9e830a3cb3fff4d6b8c70f Mon Sep 17 00:00:00 2001 From: rushi3691 Date: Sun, 24 Mar 2024 17:27:30 +0530 Subject: [PATCH] Refactor file path handling and add error handling for file open --- src-tauri/src/creds_window.rs | 17 +++++++++++++++++ src-tauri/src/handler.rs | 14 ++++++++++---- src-tauri/src/main.rs | 27 +++++++++++---------------- 3 files changed, 38 insertions(+), 20 deletions(-) create mode 100644 src-tauri/src/creds_window.rs diff --git a/src-tauri/src/creds_window.rs b/src-tauri/src/creds_window.rs new file mode 100644 index 0000000..6f1e69c --- /dev/null +++ b/src-tauri/src/creds_window.rs @@ -0,0 +1,17 @@ +use tauri::AppHandle; + +pub fn open_window(app_handle: &AppHandle) { + let creds_window = tauri::WindowBuilder::new( + app_handle, + "addcreds", /* the unique window label */ + tauri::WindowUrl::App("index.html".into()), + ) + .title("fortinet-connect") + .center() + .build() + .ok(); + + if let Some(creds_window) = creds_window { + creds_window.show().ok(); + } +} diff --git a/src-tauri/src/handler.rs b/src-tauri/src/handler.rs index bffa94f..cc704df 100644 --- a/src-tauri/src/handler.rs +++ b/src-tauri/src/handler.rs @@ -4,8 +4,6 @@ use tauri::Manager; use crate::{worker::worker, Credentials}; - - #[tauri::command] pub async fn store_credentials( app_handle: tauri::AppHandle, @@ -16,8 +14,16 @@ pub async fn store_credentials( println!("{}", s); // write to file - let binding = app_handle.path_resolver().app_data_dir().unwrap(); - let app_data_dir = binding.to_str().unwrap(); + let app_data_path = app_handle.path_resolver().app_data_dir().unwrap(); + // print if path does not exist + let exists = app_data_path.exists(); + if !exists { + println!("Path does not exist: {:?}", app_data_path); + // create it + std::fs::create_dir_all(&app_data_path).unwrap(); + } + + let app_data_dir = app_data_path.to_str().unwrap(); // println!("App data dir: {:?}", app_data_dir); let file_path = format!("{}/creds.txt", app_data_dir); let mut file = File::create(file_path).unwrap(); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 09e627a..5b51707 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,6 +1,7 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] +mod creds_window; mod fortinet; mod handler; mod setup; @@ -29,9 +30,14 @@ fn main() { .setup(|app| { let state: tauri::State = app.state(); - let path = app.handle().path_resolver().app_data_dir().unwrap(); - println!("Path: {:?}", path); - let file_path = format!("{}/creds.txt", path.to_str().unwrap()); + let app_data_path_buf = app.path_resolver().app_data_dir().unwrap(); + + println!("Path: {:?}", app_data_path_buf); + println!("Logging path: {:?}", app.path_resolver().app_log_dir().unwrap()); + // std::fs::create_dir_all(&app_data_path_buf).unwrap(); + let app_data_path = app_data_path_buf.to_str().unwrap(); + + let file_path = format!("{}/creds.txt", app_data_path); let file = File::open(file_path); match file { Ok(mut file) => { @@ -53,6 +59,7 @@ fn main() { } Err(e) => { println!("Error: {:?}", e); + creds_window::open_window(&app.app_handle()); } } @@ -65,19 +72,7 @@ fn main() { std::process::exit(0); } "add" => { - let creds_window = tauri::WindowBuilder::new( - app_handle, - "addcreds", /* the unique window label */ - tauri::WindowUrl::App("index.html".into()), - ) - .title("fortinet-connect") - .center() - .build() - .ok(); - - if let Some(creds_window) = creds_window { - creds_window.show().ok(); - } + creds_window::open_window(app_handle); } _ => {}