mirror of
https://github.com/FranP-code/ChatGPT.git
synced 2025-10-13 00:13:25 +00:00
82 lines
2.3 KiB
Rust
82 lines
2.3 KiB
Rust
use anyhow::Result;
|
|
use std::{
|
|
fs::{self, File},
|
|
path::{Path, PathBuf},
|
|
process::Command,
|
|
};
|
|
use tauri::Manager;
|
|
// use tauri::utils::config::Config;
|
|
|
|
pub fn chat_root() -> PathBuf {
|
|
tauri::api::path::home_dir().unwrap().join(".chatgpt")
|
|
}
|
|
|
|
// pub fn get_tauri_conf() -> Option<Config> {
|
|
// let config_file = include_str!("../tauri.conf.json");
|
|
// let config: Config =
|
|
// serde_json::from_str(config_file).expect("failed to parse tauri.conf.json");
|
|
// Some(config)
|
|
// }
|
|
|
|
pub fn exists(path: &Path) -> bool {
|
|
Path::new(path).exists()
|
|
}
|
|
|
|
pub fn create_file(path: &Path) -> Result<File> {
|
|
if let Some(p) = path.parent() {
|
|
fs::create_dir_all(p)?
|
|
}
|
|
File::create(path).map_err(Into::into)
|
|
}
|
|
|
|
pub fn script_path() -> PathBuf {
|
|
let script_file = chat_root().join("main.js");
|
|
if !exists(&script_file) {
|
|
create_file(&script_file).unwrap();
|
|
fs::write(&script_file, format!("// *** ChatGPT User Script ***\n// @github: https://github.com/lencx/ChatGPT \n// @path: {}\n\nconsole.log('🤩 Hello ChatGPT!!!');", &script_file.to_string_lossy())).unwrap();
|
|
}
|
|
|
|
script_file
|
|
}
|
|
|
|
pub fn user_script() -> String {
|
|
let user_script_content = fs::read_to_string(script_path()).unwrap_or_else(|_| "".to_string());
|
|
format!(
|
|
"window.addEventListener('DOMContentLoaded', function() {{\n{}\n}})",
|
|
user_script_content
|
|
)
|
|
}
|
|
|
|
pub fn open_file(path: PathBuf) {
|
|
#[cfg(target_os = "macos")]
|
|
Command::new("open").arg("-R").arg(path).spawn().unwrap();
|
|
|
|
#[cfg(target_os = "windows")]
|
|
Command::new("explorer")
|
|
.arg("/select,")
|
|
.arg(path)
|
|
.spawn()
|
|
.unwrap();
|
|
|
|
// https://askubuntu.com/a/31071
|
|
#[cfg(target_os = "linux")]
|
|
Command::new("xdg-open").arg(path).spawn().unwrap();
|
|
}
|
|
|
|
pub fn clear_conf(app: &tauri::AppHandle) {
|
|
let root = chat_root();
|
|
let app2 = app.clone();
|
|
let msg = format!("Path: {}\nAre you sure to clear all ChatGPT configurations? Please backup in advance if necessary!", root.to_string_lossy());
|
|
tauri::api::dialog::ask(
|
|
app.get_window("core").as_ref(),
|
|
"Clear Config",
|
|
msg,
|
|
move |is_ok| {
|
|
if is_ok {
|
|
fs::remove_dir_all(root).unwrap();
|
|
tauri::api::process::restart(&app2.env());
|
|
}
|
|
},
|
|
);
|
|
}
|