-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
457 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
use std::io::{Read, Seek, SeekFrom, Write}; | ||
|
||
use tracing::{info, trace}; | ||
|
||
use crate::utils::{constants::get_project_dirs, server::Server}; | ||
|
||
const SERVER_SETTINS_FILE: &str = "server.json"; | ||
const FRONTEND_SETTINS_FILE: &str = "frontend_settings.json"; | ||
|
||
pub fn get_settings_file(file_name: &str) -> Result<std::fs::File, String> { | ||
let project_dirs = get_project_dirs().ok_or("Unable to load project dir")?; | ||
let data_dir = project_dirs.config_dir(); | ||
std::fs::create_dir_all(data_dir).map_err(|e| format!("{e:?}"))?; | ||
let settings_file = std::fs::OpenOptions::new() | ||
.read(true) | ||
.write(true) | ||
.create(true) | ||
.open(data_dir.join(file_name)) | ||
.map_err(|e| format!("Error opening file: {e:?}"))?; | ||
Ok(settings_file) | ||
} | ||
|
||
#[tauri::command] | ||
pub fn save_server( | ||
description: &str, | ||
server_host: &str, | ||
server_port: u16, | ||
username: &str, | ||
) -> Result<(), String> { | ||
info!("Saving server: {server_host}:{server_port}"); | ||
let mut server_file = get_settings_file(SERVER_SETTINS_FILE)?; | ||
|
||
// read the json content using serde and append the new server | ||
let mut server_list = | ||
serde_json::from_reader::<&std::fs::File, Vec<Server>>(&server_file).unwrap_or_default(); | ||
|
||
// check if the server is already in the list | ||
for server in &server_list { | ||
if server.host == server_host && server.port == server_port { | ||
return Err("Server already exists".to_string()); | ||
} | ||
} | ||
|
||
server_list.push(Server { | ||
description: description.to_string(), | ||
host: server_host.to_string(), | ||
port: server_port, | ||
username: username.to_string(), | ||
}); | ||
|
||
trace!("Server list: {:#?}", server_list); | ||
|
||
// write the new json content | ||
server_file | ||
.seek(SeekFrom::Start(0)) | ||
.map_err(|e| format!("{e:?}"))?; | ||
server_file | ||
.write_all( | ||
serde_json::to_string_pretty(&server_list) | ||
.map_err(|e| format!("{e:?}"))? | ||
.as_bytes(), | ||
) | ||
.map_err(|e| format!("{e:?}"))?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tauri::command] | ||
pub fn get_server_list() -> Result<Vec<Server>, String> { | ||
info!("Getting server list"); | ||
let project_dirs = get_project_dirs().ok_or("Unable to load project dir")?; | ||
|
||
let data_dir = project_dirs.config_dir(); | ||
|
||
// create config dir if it doesn't exist | ||
std::fs::create_dir_all(data_dir).map_err(|e| format!("{e:?}"))?; | ||
|
||
// open server.json or create it if it doesn't exist | ||
let server_file = std::fs::OpenOptions::new() | ||
.read(true) | ||
.write(true) | ||
.create(true) | ||
.open(data_dir.join(SERVER_SETTINS_FILE)) | ||
.map_err(|e| format!("Error opening file: {e:?}"))?; | ||
|
||
// read the json content using serde | ||
let server_list = | ||
serde_json::from_reader::<&std::fs::File, Vec<Server>>(&server_file).unwrap_or_default(); | ||
|
||
trace!("Server list: {:#?}", server_list); | ||
|
||
Ok(server_list) | ||
} | ||
|
||
#[derive(serde::Deserialize, serde::Serialize, Debug)] | ||
pub struct LinkPreview { | ||
enabled: bool, | ||
allow_all: bool, | ||
urls: Vec<String>, | ||
} | ||
|
||
#[allow(clippy::needless_pass_by_value)] // LinkPreview needs to be deserialized | ||
#[tauri::command] | ||
pub fn save_frontend_settings(settings_name: &str, data: LinkPreview) -> Result<(), String> { | ||
info!("Saving frontend settings: {settings_name}"); | ||
let mut settings_file = get_settings_file(&format!("{settings_name}_{FRONTEND_SETTINS_FILE}"))?; | ||
|
||
trace!("Settings data: {:#?}", data); | ||
|
||
// write the new json content | ||
settings_file | ||
.seek(SeekFrom::Start(0)) | ||
.map_err(|e| format!("{e:?}"))?; | ||
settings_file | ||
.write_all( | ||
serde_json::to_string_pretty(&data) | ||
.map_err(|e| format!("{e:?}"))? | ||
.as_bytes(), | ||
) | ||
.map_err(|e| format!("{e:?}"))?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tauri::command] | ||
pub fn get_frontend_settings(settings_name: &str) -> Result<String, String> { | ||
info!("Getting frontend settings: {settings_name}"); | ||
let mut settings_file = get_settings_file(&format!("{settings_name}_{FRONTEND_SETTINS_FILE}"))?; | ||
|
||
let mut settings_data = String::new(); | ||
settings_file | ||
.read_to_string(&mut settings_data) | ||
.map_err(|e| format!("{e:?}"))?; | ||
|
||
trace!("Settings data: {:#?}", settings_data); | ||
|
||
Ok(settings_data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use serde_json::json; | ||
use tauri::State; | ||
use tokio::sync::Mutex; | ||
use webbrowser::{Browser, BrowserOptions}; | ||
|
||
use super::helper::OpenGraphCrawler; | ||
|
||
#[tauri::command] | ||
pub fn open_browser(url: &str) -> Result<(), String> { | ||
if let Err(e) = webbrowser::open_browser_with_options( | ||
Browser::Default, | ||
url, | ||
BrowserOptions::new().with_suppress_output(false), | ||
) { | ||
return Err(format!("{e:?}")); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub struct CrawlerState { | ||
pub crawler: Mutex<Option<OpenGraphCrawler>>, | ||
} | ||
|
||
#[tauri::command] | ||
pub async fn get_open_graph_data_from_website( | ||
state: State<'_, CrawlerState>, | ||
url: &str, | ||
) -> Result<String, String> { | ||
// setup crawler if not already done | ||
let result = { | ||
let mut client = state.crawler.lock().await; | ||
if client.is_none() { | ||
*client = OpenGraphCrawler::try_new(); | ||
} | ||
|
||
client | ||
.as_ref() | ||
.ok_or_else(|| "Failed to read website body".to_string())? | ||
.crawl(url) | ||
.await | ||
}; | ||
|
||
let result = json!(result); | ||
|
||
Ok(result.to_string()) | ||
} |
Oops, something went wrong.