reorganisation, cleanup and new nonce protocol

This commit is contained in:
DecDuck
2024-10-12 17:34:47 +11:00
parent ac66b20591
commit e828bca2a5
19 changed files with 429 additions and 60 deletions

62
src-tauri/src/db.rs Normal file
View File

@ -0,0 +1,62 @@
use std::{
fs::{self, create_dir_all},
path::PathBuf,
sync::LazyLock,
};
use directories::BaseDirs;
use rustbreak::{deser::Bincode, PathDatabase};
use serde::Deserialize;
use crate::DB;
#[derive(serde::Serialize, Clone, Deserialize)]
pub struct DatabaseAuth {
pub private: String,
pub cert: String,
pub clientId: String,
}
#[derive(serde::Serialize, Clone, Deserialize)]
pub struct DatabaseApps {
pub apps_base_dir: String,
}
#[derive(serde::Serialize, Clone, Deserialize)]
pub struct Database {
pub auth: Option<DatabaseAuth>,
pub base_url: String,
pub downloads: DatabaseApps,
}
pub type DatabaseInterface =
rustbreak::Database<Database, rustbreak::backend::PathBackend, Bincode>;
pub static DATA_ROOT_DIR: LazyLock<PathBuf> =
LazyLock::new(|| BaseDirs::new().unwrap().data_dir().join("drop"));
pub fn setup() -> DatabaseInterface {
let db_path = DATA_ROOT_DIR.join("drop.db");
let apps_base_dir = DATA_ROOT_DIR.join("apps");
create_dir_all(DATA_ROOT_DIR.clone()).unwrap();
create_dir_all(apps_base_dir.clone()).unwrap();
let default = Database {
auth: None,
base_url: "".to_string(),
downloads: DatabaseApps {
apps_base_dir: apps_base_dir.to_str().unwrap().to_string(),
},
};
let db = match fs::exists(db_path.clone()).unwrap() {
true => PathDatabase::load_from_path(db_path).expect("Database loading failed"),
false => PathDatabase::create_at_path(db_path, default).unwrap(),
};
return db;
}
pub fn is_set_up() -> bool {
return !DB.borrow_data().unwrap().base_url.is_empty();
}