mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
refactor: import profile
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
use crate::{
|
||||
config::{read_profiles, save_profiles, ProfileItem},
|
||||
events::{emit::ClashInfoPayload, state::ClashInfoState},
|
||||
utils::{clash, import},
|
||||
utils::{app_home_dir, clash, fetch::fetch_profile},
|
||||
};
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use tauri::{api::process::kill_children, AppHandle, State};
|
||||
|
||||
#[tauri::command]
|
||||
@@ -14,14 +17,6 @@ pub fn restart_sidebar(app_handle: AppHandle, clash_info: State<'_, ClashInfoSta
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn import_profile(url: String) -> Result<String, String> {
|
||||
match import::import_profile(&url).await {
|
||||
Ok(_) => Ok(String::from("success")),
|
||||
Err(_) => Err(String::from("error")),
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_clash_info(clash_info: State<'_, ClashInfoState>) -> Option<ClashInfoPayload> {
|
||||
match clash_info.0.lock() {
|
||||
@@ -29,3 +24,44 @@ pub fn get_clash_info(clash_info: State<'_, ClashInfoState>) -> Option<ClashInfo
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Import the Profile from url and
|
||||
/// save to the `profiles.yaml` file
|
||||
#[tauri::command]
|
||||
pub async fn import_profile(url: String) -> Result<String, String> {
|
||||
let result = match fetch_profile(&url).await {
|
||||
Some(r) => r,
|
||||
None => {
|
||||
log::error!("failed to fetch profile from `{}`", url);
|
||||
return Err(format!("failed"));
|
||||
}
|
||||
};
|
||||
|
||||
let path = app_home_dir().join("profiles").join(&result.file);
|
||||
File::create(path)
|
||||
.unwrap()
|
||||
.write(result.data.as_bytes())
|
||||
.unwrap();
|
||||
|
||||
// update profiles.yaml
|
||||
let mut profiles = read_profiles();
|
||||
let mut items = match profiles.items {
|
||||
Some(p) => p,
|
||||
None => vec![],
|
||||
};
|
||||
|
||||
let profile = ProfileItem {
|
||||
name: Some(result.name),
|
||||
file: Some(result.file),
|
||||
mode: Some(format!("rule")),
|
||||
url: Some(url),
|
||||
selected: Some(vec![]), // Todo: parse the selected list
|
||||
extra: Some(result.extra),
|
||||
};
|
||||
|
||||
items.push(profile);
|
||||
profiles.items = Some(items);
|
||||
save_profiles(&profiles);
|
||||
|
||||
Ok(format!("success"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user