mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-14 08:41:21 +10:00
reorganisation, cleanup and new nonce protocol
This commit is contained in:
62
src-tauri/src/db.rs
Normal file
62
src-tauri/src/db.rs
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user