mirror of
https://github.com/Grasscutters/Cultivation.git
synced 2025-12-14 08:04:52 +01:00
read and write server config via backend
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use std::fs;
|
use std::{fs, io::{Read, Write}};
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn rename(path: String, new_name: String) {
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,6 +45,8 @@ fn main() {
|
|||||||
file_helpers::dir_is_empty,
|
file_helpers::dir_is_empty,
|
||||||
file_helpers::dir_delete,
|
file_helpers::dir_delete,
|
||||||
file_helpers::copy_file,
|
file_helpers::copy_file,
|
||||||
|
file_helpers::read_file,
|
||||||
|
file_helpers::write_file,
|
||||||
downloader::download_file,
|
downloader::download_file,
|
||||||
downloader::stop_download,
|
downloader::stop_download,
|
||||||
lang::get_lang,
|
lang::get_lang,
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import { fs } from '@tauri-apps/api'
|
import { fs, invoke } from '@tauri-apps/api'
|
||||||
|
|
||||||
export async function toggleEncryption(path: string) {
|
export async function toggleEncryption(path: string) {
|
||||||
let serverConf
|
let serverConf
|
||||||
|
|
||||||
try {
|
try {
|
||||||
serverConf = JSON.parse(await fs.readTextFile(path))
|
serverConf = JSON.parse(await invoke('read_file', {
|
||||||
|
path,
|
||||||
|
}))
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log(`Server config at ${path} not found or invalid`)
|
console.log(`Server config at ${path} not found or invalid`)
|
||||||
return
|
return
|
||||||
@@ -16,7 +18,7 @@ export async function toggleEncryption(path: string) {
|
|||||||
serverConf.server.http.encryption.useInRouting = !enabled
|
serverConf.server.http.encryption.useInRouting = !enabled
|
||||||
|
|
||||||
// Write file
|
// Write file
|
||||||
await fs.writeFile({
|
await invoke('write_file', {
|
||||||
path,
|
path,
|
||||||
contents: JSON.stringify(serverConf, null, 2),
|
contents: JSON.stringify(serverConf, null, 2),
|
||||||
})
|
})
|
||||||
@@ -26,7 +28,9 @@ export async function encryptionEnabled(path: string) {
|
|||||||
let serverConf
|
let serverConf
|
||||||
|
|
||||||
try {
|
try {
|
||||||
serverConf = JSON.parse(await fs.readTextFile(path))
|
serverConf = JSON.parse(await invoke('read_file', {
|
||||||
|
path,
|
||||||
|
}))
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log(`Server config at ${path} not found or invalid`)
|
console.log(`Server config at ${path} not found or invalid`)
|
||||||
return false
|
return false
|
||||||
|
|||||||
Reference in New Issue
Block a user