Skip to content

Commit

Permalink
fix:modifyAlistConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
VirtualHotBar committed Jun 2, 2024
1 parent bdc0ad1 commit 8ff3649
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src-tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ use serde_json::{Map, Value};
use std::fs::File;
use std::io::{self, Write};
use tauri_build::Attributes;
use tokio::fs::read_to_string; // 此处使用futures_util
//use tokio::fs::read_to_string; // 此处使用futures_util
#[tokio::main]
pub async fn download_with_progress<F>(
url: &str,
Expand Down
29 changes: 28 additions & 1 deletion src-tauri/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::{Path, PathBuf};
use std::{fs, path::{Path, PathBuf}};

use tauri::Manager as _;

Expand Down Expand Up @@ -27,3 +27,30 @@ pub fn fs_make_dir(app: tauri::AppHandle<Runtime>, path: &str) -> anyhow_tauri::
std::fs::create_dir_all(path).map_err(anyhow::Error::from)?;
Ok(())
}



use serde_json::{to_string_pretty, Value};

#[tauri::command]
pub fn read_json_file(path: Option<&str>) -> Result<Value, String> {
let content_result = fs::read_to_string(PathBuf::from(path.unwrap()));
match content_result {
Ok(content) => match serde_json::from_str(&content) {
Ok(config) => Ok(config),
Err(json_error) => Err(format!("Failed to parse JSON from file: {}", json_error)),
},
Err(io_error) => Err(format!("Failed to read file: {}", io_error)),
}
}

#[tauri::command]
pub async fn write_json_file(config_data: Value, path: Option<&str>) -> Result<(), String> {
let pretty_config = to_string_pretty(&config_data)
.map_err(|json_error| format!("Failed to serialize JSON: {}", json_error))?;

fs::write(PathBuf::from(path.unwrap()), pretty_config)
.map_err(|io_error| format!("Failed to write file: {}", io_error))?;

Ok(())
}
6 changes: 4 additions & 2 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::path::PathBuf;
use std::{env, fs::File, ops::Deref, path::Path, sync::RwLock};

use config::Config;
use fs::{fs_exist_dir, fs_make_dir};
use fs::{fs_exist_dir, fs_make_dir,read_json_file,write_json_file};
use locale::Locale;
use tray::Tray;

Expand Down Expand Up @@ -194,7 +194,9 @@ pub fn init() -> anyhow::Result<()> {
get_temp_dir,
fs_exist_dir,
fs_make_dir,
restart_self
restart_self,
read_json_file,
write_json_file
])
.setup(|app| {
if let Some(file) = File::open(app.app_config_file()).ok() {
Expand Down
6 changes: 1 addition & 5 deletions src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#[cfg(target_os = "windows")]
use tauri::{Manager, Runtime};

#[cfg(target_os = "windows")]
use std::error::Error;
#[cfg(target_os = "windows")]
use std::fs;
use std::{
io::{self, Write},
path::PathBuf,
};
use std::io::{self, Write};

pub fn get_available_ports(count: usize) -> Vec<u16> {
use std::net::TcpListener;
Expand Down
8 changes: 4 additions & 4 deletions src/utils/alist/alist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ async function setAlistPass(pass:string){
}

async function modifyAlistConfig(rewriteData:any=alistInfo.alistConfig){
// const path = alistDataDir()+'config.json'
// const oldAlistConfig =await invoke('read_config_file',{path}) as object
// const newAlistConfig = {...oldAlistConfig, ...rewriteData}
// await invoke('write_config_file',{configData:newAlistConfig,path:path})
const path = alistDataDir()+'config.json'
const oldAlistConfig =await invoke('read_json_file',{path}) as object
const newAlistConfig = {...oldAlistConfig, ...rewriteData}
await invoke('write_json_file',{configData:newAlistConfig,path:path})
}

async function addAlistInRclone(){
Expand Down

0 comments on commit 8ff3649

Please sign in to comment.