From ffc37cc2039b15c0731cd8a55097a18da6e4e2af Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Sat, 16 Jul 2022 16:36:05 -0700 Subject: [PATCH] read and write server config via backend --- src-tauri/src/file_helpers.rs | 47 ++++++++++++++++++++++++++++++++++- src-tauri/src/main.rs | 2 ++ src/utils/server.ts | 12 ++++++--- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src-tauri/src/file_helpers.rs b/src-tauri/src/file_helpers.rs index c63cda4..e72570b 100644 --- a/src-tauri/src/file_helpers.rs +++ b/src-tauri/src/file_helpers.rs @@ -1,4 +1,4 @@ -use std::fs; +use std::{fs, io::{Read, Write}}; #[tauri::command] pub fn rename(path: String, new_name: String) { @@ -53,3 +53,48 @@ pub fn copy_file(path: String, new_path: String) -> bool { } } } + +#[tauri::command] +pub fn read_file(path: String) -> String { + let mut file = match fs::File::open(path) { + Ok(file) => file, + Err(e) => { + println!("Failed to open file: {}", e); + return String::new(); + } + }; + + let mut contents = String::new(); + file.read_to_string(&mut contents).unwrap(); + + contents +} + +#[tauri::command] +pub fn write_file(path: String, contents: String) { + // Create file if it exists, otherwise just open and rewrite + let mut file = match fs::File::open(&path) { + Ok(file) => file, + Err(e) => { + println!("Failed to open file: {}", e); + + // attempt to create file. otherwise return + match fs::File::create(&path) { + Ok(file) => file, + Err(e) => { + println!("Failed to create file: {}", e); + return; + } + } + } + }; + + // Write contents to file + match file.write_all(contents.as_bytes()) { + Ok(_) => (), + Err(e) => { + println!("Failed to write to file: {}", e); + return; + } + } +} \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 7fc09be..614756c 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -45,6 +45,8 @@ fn main() { file_helpers::dir_is_empty, file_helpers::dir_delete, file_helpers::copy_file, + file_helpers::read_file, + file_helpers::write_file, downloader::download_file, downloader::stop_download, lang::get_lang, diff --git a/src/utils/server.ts b/src/utils/server.ts index 763872c..f2fc6d5 100644 --- a/src/utils/server.ts +++ b/src/utils/server.ts @@ -1,10 +1,12 @@ -import { fs } from '@tauri-apps/api' +import { fs, invoke } from '@tauri-apps/api' export async function toggleEncryption(path: string) { let serverConf try { - serverConf = JSON.parse(await fs.readTextFile(path)) + serverConf = JSON.parse(await invoke('read_file', { + path, + })) } catch(e) { console.log(`Server config at ${path} not found or invalid`) return @@ -16,7 +18,7 @@ export async function toggleEncryption(path: string) { serverConf.server.http.encryption.useInRouting = !enabled // Write file - await fs.writeFile({ + await invoke('write_file', { path, contents: JSON.stringify(serverConf, null, 2), }) @@ -26,7 +28,9 @@ export async function encryptionEnabled(path: string) { let serverConf try { - serverConf = JSON.parse(await fs.readTextFile(path)) + serverConf = JSON.parse(await invoke('read_file', { + path, + })) } catch(e) { console.log(`Server config at ${path} not found or invalid`) return false