Skip to content

Commit

Permalink
Use app_local_data_dir() for database (#757)
Browse files Browse the repository at this point in the history
* Use app_local_data_dir() for database
* Move get_app_storage_dir() into config
  • Loading branch information
uklotzde authored Mar 18, 2024
1 parent ceb0438 commit de9e36b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
8 changes: 0 additions & 8 deletions src-tauri/src/libs/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ impl TimeLogger {
}
}

/**
* Get the app configuration/storage directory
*/
pub fn get_app_storage_dir() -> PathBuf {
let path = dirs::home_dir().expect("Get home dir");
path.join(".museeks")
}

/**
* Check if a directory or a file is visible or not
*/
Expand Down
5 changes: 1 addition & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ use tauri_plugin_log::{Target, TargetKind};
*/
#[tokio::main]
async fn main() {
// Is there any way to instantiate that in the plugin directly?
let db = plugins::database::setup().await.ok().unwrap();

tauri::Builder::default()
// Logging must be setup first, otherwise the logs won't be captured
// while setting up the other plugins.
Expand All @@ -36,7 +33,7 @@ async fn main() {
.plugin(plugins::app_menu::init())
.plugin(plugins::config::init())
.plugin(plugins::cover::init())
.plugin(plugins::database::init(db))
.plugin(plugins::database::init())
.plugin(plugins::debug::init())
.plugin(plugins::default_view::init())
.plugin(plugins::shell_extension::init())
Expand Down
11 changes: 9 additions & 2 deletions src-tauri/src/plugins/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use tauri::plugin::{Builder, TauriPlugin};
use tauri::{Manager, Runtime, State};
use ts_rs::TS;

use crate::libs::utils::get_app_storage_dir;

#[derive(Serialize, Deserialize, Debug, Clone, TS)]
#[ts(export, export_to = "../src/generated/typings/Repeat.ts")]
pub enum Repeat {
Expand Down Expand Up @@ -136,6 +134,15 @@ pub fn set_config(config_manager: State<ConfigManager>, config: Config) {
config_manager.update(config);
}

/**
* Get the app configuration/storage directory
*/
// TODO: Replace with PathResolver::app_config_dir().
fn get_app_storage_dir() -> PathBuf {
let path = dirs::home_dir().expect("Get home dir");
path.join(".museeks")
}

pub fn init<R: Runtime>() -> TauriPlugin<R> {
let conf_path = get_app_storage_dir();
let manager = HomeConfig::with_file(conf_path.join("config.toml"));
Expand Down
29 changes: 21 additions & 8 deletions src-tauri/src/plugins/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ use std::path::PathBuf;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tauri::plugin::{Builder, TauriPlugin};
use tauri::{Manager, Runtime, State};
use tauri::{AppHandle, Manager, Runtime, State};
use ts_rs::TS;
use uuid::Uuid;

use crate::libs::error::{AnyResult, MuseeksError};
use crate::libs::events::IPCEvent;
use crate::libs::utils::{get_app_storage_dir, scan_dirs, TimeLogger};
use crate::libs::utils::{scan_dirs, TimeLogger};

const INSERTION_BATCH: usize = 200;

Expand Down Expand Up @@ -537,9 +537,12 @@ async fn reset(db: State<'_, DB>) -> AnyResult<()> {
* Database setup
* Doc: https://github.com/khonsulabs/bonsaidb/blob/main/examples/basic-local/examples/basic-local-multidb.rs
*/
pub async fn setup() -> AnyResult<DB> {
let conf_path = get_app_storage_dir();
let storage_configuration = StorageConfiguration::new(conf_path.join("main.bonsaidb"))
async fn setup<R: Runtime>(app_handle: &AppHandle<R>) -> AnyResult<DB> {
let storage_path = app_handle
.path()
.app_local_data_dir()
.map_err(anyhow::Error::from)?;
let storage_configuration = StorageConfiguration::new(storage_path.join("main.bonsaidb"))
.with_schema::<Track>()?
.with_schema::<Playlist>()?;

Expand All @@ -556,7 +559,7 @@ pub async fn setup() -> AnyResult<DB> {
/**
* Database plugin, exposing commands and state
*/
pub fn init<R: Runtime>(db: DB) -> TauriPlugin<R> {
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::<R>::new("database")
.invoke_handler(tauri::generate_handler![
get_all_tracks,
Expand All @@ -573,8 +576,18 @@ pub fn init<R: Runtime>(db: DB) -> TauriPlugin<R> {
reset,
import_tracks_to_library,
])
.setup(|app_handle, _api| {
app_handle.manage(db);
.setup(move |app_handle, _api| {
let app_handle = app_handle.clone();
tauri::async_runtime::spawn(async move {
let db = match setup(&app_handle).await {
Ok(db) => db,
Err(err) => {
error!("Failed to setup database: {:?}", err);
return;
}
};
app_handle.manage(db);
});
Ok(())
})
.build()
Expand Down

0 comments on commit de9e36b

Please sign in to comment.