From 553f34a1111bd86d14ac1835158a9a116c59126d Mon Sep 17 00:00:00 2001 From: chmanie Date: Fri, 2 Jun 2023 20:05:31 +0200 Subject: [PATCH] Use proper config directory --- Cargo.lock | 28 ++++++++++++++++++++++++++++ crabidy-core/src/lib.rs | 1 + crabidy-server/Cargo.toml | 1 + crabidy-server/src/main.rs | 23 +++++++++++++++++++---- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bcc6d28..cb372d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -377,6 +377,7 @@ dependencies = [ "anyhow", "async-trait", "crabidy-core", + "dirs", "flume", "futures", "gstreamer", @@ -461,6 +462,15 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dirs" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" +dependencies = [ + "dirs-sys", +] + [[package]] name = "dirs-next" version = "2.0.0" @@ -471,6 +481,18 @@ dependencies = [ "dirs-sys-next", ] +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + [[package]] name = "dirs-sys-next" version = "0.1.2" @@ -1453,6 +1475,12 @@ version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "option-operations" version = "0.5.0" diff --git a/crabidy-core/src/lib.rs b/crabidy-core/src/lib.rs index 4d86e98..83162d5 100644 --- a/crabidy-core/src/lib.rs +++ b/crabidy-core/src/lib.rs @@ -17,6 +17,7 @@ pub trait ProviderClient: std::fmt::Debug + Send + Sync { #[derive(Clone, Debug, Hash)] pub enum ProviderError { + Config(String), UnknownUser, CouldNotLogin, FetchError, diff --git a/crabidy-server/Cargo.toml b/crabidy-server/Cargo.toml index f39e97c..ef12777 100644 --- a/crabidy-server/Cargo.toml +++ b/crabidy-server/Cargo.toml @@ -20,3 +20,4 @@ tonic = "0.9.2" async-trait = "0.1.68" futures = "0.3.28" tokio-stream = { version = "0.1.14", features = ["sync"] } +dirs = "5.0.1" diff --git a/crabidy-server/src/main.rs b/crabidy-server/src/main.rs index 6df4feb..5e2b2b5 100644 --- a/crabidy-server/src/main.rs +++ b/crabidy-server/src/main.rs @@ -19,6 +19,7 @@ use tokio_stream::StreamExt; use std::{ fs, + path::PathBuf, pin::Pin, sync::{Arc, Mutex}, }; @@ -149,10 +150,24 @@ impl ProviderOrchestrator { #[async_trait] impl ProviderClient for ProviderOrchestrator { async fn init(_s: &str) -> Result { - let raw_toml_settings = fs::read_to_string("/tmp/tidaldy.toml").unwrap_or("".to_owned()); + let config_dir = dirs::config_dir() + .map(|d| d.join("crabidy")) + .unwrap_or(PathBuf::from("/tmp")); + let dir_exists = tokio::fs::try_exists(&config_dir) + .await + .map_err(|e| ProviderError::Config(e.to_string()))?; + if !dir_exists { + tokio::fs::create_dir(&config_dir) + .await + .map_err(|e| ProviderError::Config(e.to_string()))?; + } + let config_file = config_dir.join("tidaly.toml"); + let raw_toml_settings = fs::read_to_string(&config_file).unwrap_or("".to_owned()); let tidal_client = Arc::new(tidaldy::Client::init(&raw_toml_settings).await.unwrap()); let new_toml_config = tidal_client.settings(); - fs::write("/tmp/tidaldy.toml", new_toml_config).unwrap(); + tokio::fs::write(&config_file, new_toml_config) + .await + .map_err(|e| ProviderError::Config(e.to_string())); let (provider_tx, provider_rx) = flume::bounded(100); Ok(Self { provider_rx, @@ -291,12 +306,12 @@ impl Playback { duration: self .play .duration() - .and_then(|t| Some(t.mseconds() as u32)) + .map(|t| t.mseconds() as u32) .unwrap_or(0), position: self .play .position() - .and_then(|t| Some(t.mseconds() as u32)) + .map(|t| t.mseconds() as u32) .unwrap_or(0), }; let play_state = match *self.state.lock().unwrap() {