refactor(database): Convert DATA_ROOT_DIR to Mutex

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2024-11-09 22:44:44 +11:00
parent 6bc64822df
commit 00b7179743
3 changed files with 16 additions and 8 deletions

View File

@ -2,7 +2,7 @@ use std::{
collections::HashMap,
fs::{self, create_dir_all},
path::PathBuf,
sync::LazyLock,
sync::{LazyLock, Mutex, RwLock},
};
use directories::BaseDirs;
@ -10,6 +10,8 @@ use rustbreak::{deser::Bincode, PathDatabase};
use serde::{Deserialize, Serialize};
use url::Url;
use crate::DB;
#[derive(serde::Serialize, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DatabaseAuth {
@ -42,8 +44,8 @@ pub struct Database {
pub base_url: String,
pub games: DatabaseGames,
}
pub static DATA_ROOT_DIR: LazyLock<PathBuf> =
LazyLock::new(|| BaseDirs::new().unwrap().data_dir().join("drop"));
pub static DATA_ROOT_DIR: LazyLock<Mutex<PathBuf>> =
LazyLock::new(|| Mutex::new(BaseDirs::new().unwrap().data_dir().join("drop")));
pub type DatabaseInterface =
rustbreak::Database<Database, rustbreak::backend::PathBackend, Bincode>;
@ -55,10 +57,11 @@ pub trait DatabaseImpls {
}
impl DatabaseImpls for DatabaseInterface {
fn set_up_database() -> DatabaseInterface {
let db_path = DATA_ROOT_DIR.join("drop.db");
let games_base_dir = DATA_ROOT_DIR.join("games");
let data_root_dir = DATA_ROOT_DIR.lock().unwrap();
let db_path = data_root_dir.join("drop.db");
let games_base_dir = data_root_dir.join("games");
create_dir_all(DATA_ROOT_DIR.clone()).unwrap();
create_dir_all(data_root_dir.clone()).unwrap();
create_dir_all(games_base_dir.clone()).unwrap();
let default = Database {
@ -87,3 +90,8 @@ impl DatabaseImpls for DatabaseInterface {
Url::parse(&handle.base_url).unwrap()
}
}
fn change_root_directory<T: Into<PathBuf>>(new_dir: T) {
let mut lock = DATA_ROOT_DIR.lock().unwrap();
*lock = new_dir.into();
}

View File

@ -151,7 +151,7 @@ impl GameDownloadAgent {
game_id: String,
) -> Result<(), GameDownloadError> {
let mut contexts = Vec::new();
let base_path = DATA_ROOT_DIR.join("games").join(game_id.clone()).clone();
let base_path = DATA_ROOT_DIR.lock().unwrap().join("games").join(game_id.clone()).clone();
create_dir_all(base_path.clone()).unwrap();
info!("Generating contexts");
for (raw_path, chunk) in manifest {

View File

@ -146,7 +146,7 @@ pub fn run() {
.min_inner_size(900.0, 900.0)
.inner_size(1536.0, 864.0)
.decorations(false)
.data_directory(DATA_ROOT_DIR.join(".webview"))
.data_directory(DATA_ROOT_DIR.lock().unwrap().join(".webview"))
.build()
.unwrap();