mirror of
https://github.com/Grasscutters/Cultivation.git
synced 2025-01-09 04:27:53 +08:00
read and write server config via backend
This commit is contained in:
parent
8faaba2849
commit
ffc37cc203
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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,
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user