Working on better error handling.

This commit is contained in:
Samuel Walker 2024-09-27 16:44:37 -06:00
parent 3ccedf668f
commit 7197fabd1e

View File

@ -11,12 +11,9 @@ use crate::modpack::get_versions;
use crate::modpack::VersionEntry; use crate::modpack::VersionEntry;
use crate::sftp; use crate::sftp;
use crate::modpack; use crate::modpack;
use crate::ModpackEntry; use serde::Serialize;
use ssh2::Sftp;
use ssh2::Session; use ssh2::Session;
use chrono; use chrono;
use zip::unstable::write::FileOptionsExt;
use zip::write::FileOptions;
use zip::write::SimpleFileOptions; use zip::write::SimpleFileOptions;
use zip::ZipArchive; use zip::ZipArchive;
use zip::ZipWriter; use zip::ZipWriter;
@ -25,16 +22,22 @@ use zip::ZipWriter;
//static PASSWORD: parking_lot::Mutex<String> = parking_lot::const_mutex(String::new()); //static PASSWORD: parking_lot::Mutex<String> = parking_lot::const_mutex(String::new());
static SESSION: tauri::async_runtime::Mutex<Option<Session>> = tauri::async_runtime::Mutex::const_new(None); static SESSION: tauri::async_runtime::Mutex<Option<Session>> = tauri::async_runtime::Mutex::const_new(None);
pub fn emit(event: &str, payload: impl Serialize + Clone, window: tauri::Window){
if !window.emit(event, payload).is_ok() {
println!("Failed to emit to window!");
}
}
#[tauri::command] #[tauri::command]
pub async fn login(username: String, password: String, window: tauri::Window) { pub async fn login(username: String, password: String, window: tauri::Window) {
let res = sftp::connect(username, password); let res = sftp::connect(username, password);
if(res.is_ok()){ if res.is_ok() {
//*USERNAME.lock() = username; //*USERNAME.lock() = username;
//*PASSWORD.lock() = password; //*PASSWORD.lock() = password;
*SESSION.lock().await = Some(res.unwrap()); *SESSION.lock().await = Some(res.unwrap());
window.emit("Login_Success", {}); emit("Login_Success", {}, window);
}else{ }else{
window.emit("Login_Failed", {}); emit("Login_Failed", {}, window);
} }
} }
@ -42,7 +45,7 @@ pub async fn login(username: String, password: String, window: tauri::Window) {
pub async fn drop_session(){ pub async fn drop_session(){
let ref mut session = *SESSION.lock().await; let ref mut session = *SESSION.lock().await;
if let Some(session) = session { if let Some(session) = session {
session.disconnect(None, "disconnecting", None); session.disconnect(None, "disconnecting", None).unwrap();
} }
*session = None; *session = None;
} }
@ -52,9 +55,9 @@ async fn update_modpacks(modpacks: Vec<modpack::ModpackEntry>) -> Result<(), Str
let reader = Cursor::new(data.as_bytes()); let reader = Cursor::new(data.as_bytes());
let ref mut session = *SESSION.lock().await; let ref mut session = *SESSION.lock().await;
if let Some(session) = session { if let Some(session) = session {
sftp::uplaod(None, session.clone(), PathBuf::from("/ftp/modpacks.json"), reader, format!("modpacks.json"), data.as_bytes().len()).await; sftp::uplaod(None, session.clone(), PathBuf::from("/ftp/modpacks.json"), reader, format!("modpacks.json"), data.as_bytes().len()).await?;
} }
Ok(()) Err(format!("Session doesnt exist?"))
} }
async fn update_versions(id: String, versions: Vec<modpack::VersionEntry>) -> Result<(), String>{ async fn update_versions(id: String, versions: Vec<modpack::VersionEntry>) -> Result<(), String>{
@ -62,17 +65,17 @@ async fn update_versions(id: String, versions: Vec<modpack::VersionEntry>) -> Re
let reader = Cursor::new(data.as_bytes()); let reader = Cursor::new(data.as_bytes());
let ref mut session = *SESSION.lock().await; let ref mut session = *SESSION.lock().await;
if let Some(session) = session { if let Some(session) = session {
sftp::uplaod(None, session.clone(), PathBuf::from(format!("/ftp/{}/versions.json", id)), reader, format!("modpacks.json"), data.as_bytes().len()).await; sftp::uplaod(None, session.clone(), PathBuf::from(format!("/ftp/{}/versions.json", id)), reader, format!("modpacks.json"), data.as_bytes().len()).await?;
} }
Ok(()) Err(format!("Session doesnt exist?"))
} }
#[tauri::command] #[tauri::command]
pub async fn shift_up(id: String){ pub async fn shift_up(id: String, window: tauri::Window){
let mut modpacks = modpack::get_modpacks().await; let mut modpacks = modpack::get_modpacks().await;
let mut index = 0; let mut index = 0;
for pack in modpacks.as_slice() { for pack in modpacks.as_slice() {
if(pack.id == id){ if pack.id == id {
break; break;
} }
index += 1; index += 1;
@ -80,15 +83,18 @@ pub async fn shift_up(id: String){
if index != 0 { if index != 0 {
modpacks.swap(index, index-1); modpacks.swap(index, index-1);
} }
update_modpacks(modpacks).await; let res = update_modpacks(modpacks).await;
if !res.is_ok() {
emit("Error", res.unwrap_err(), window);
}
} }
#[tauri::command] #[tauri::command]
pub async fn shift_down(id: String){ pub async fn shift_down(id: String, window: tauri::Window){
let mut modpacks = modpack::get_modpacks().await; let mut modpacks = modpack::get_modpacks().await;
let mut index = 0; let mut index = 0;
for pack in modpacks.as_slice() { for pack in modpacks.as_slice() {
if(pack.id == id){ if pack.id == id {
break; break;
} }
index += 1; index += 1;
@ -96,23 +102,38 @@ pub async fn shift_down(id: String){
if index != modpacks.len()-1 { if index != modpacks.len()-1 {
modpacks.swap(index, index+1); modpacks.swap(index, index+1);
} }
update_modpacks(modpacks).await; let res = update_modpacks(modpacks).await;
if !res.is_ok() {
emit("Error", res.unwrap_err(), window);
}
} }
#[tauri::command] #[tauri::command]
pub async fn add_pack(id: String, name: String){ pub async fn add_pack(id: String, name: String, window: tauri::Window){
{ {
let ref mut session = *SESSION.lock().await; let ref mut session = *SESSION.lock().await;
if let Some(session) = session{ if let Some(session) = session{
sftp::mkdir(session.clone(), PathBuf::from(format!("/ftp/{}", id))).await; let res = sftp::mkdir(session.clone(), PathBuf::from(format!("/ftp/{}", id))).await;
sftp::mkdir(session.clone(), PathBuf::from(format!("/ftp/{}/Versions", id))).await; if !res.is_ok() {
emit("Error", res.unwrap_err(), window.clone());
}
let res = sftp::mkdir(session.clone(), PathBuf::from(format!("/ftp/{}/Versions", id))).await;
if !res.is_ok() {
emit("Error", res.unwrap_err(), window.clone());
}
} }
} }
let versions: Vec<VersionEntry> = Vec::new(); let versions: Vec<VersionEntry> = Vec::new();
update_versions(id.clone(), versions).await; let res = update_versions(id.clone(), versions).await;
if !res.is_ok() {
emit("Error", res.unwrap_err(), window.clone());
}
let mut modpacks = get_modpacks().await; let mut modpacks = get_modpacks().await;
modpacks.push(modpack::ModpackEntry{id: id, name: name, last_updated: format!("{:?}", chrono::offset::Utc::now())}); modpacks.push(modpack::ModpackEntry{id: id, name: name, last_updated: format!("{:?}", chrono::offset::Utc::now())});
update_modpacks(modpacks).await; let res = update_modpacks(modpacks).await;
if !res.is_ok() {
emit("Error", res.unwrap_err(), window);
}
} }
#[tauri::command] #[tauri::command]