Skip to content

Commit

Permalink
[config_util, fs_util] Build downloads directory path if not set
Browse files Browse the repository at this point in the history
  • Loading branch information
m4heshd committed May 2, 2024
1 parent 4ab374b commit cac3417
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ axum = "0.7.5"
axum-embed = "0.1.0"
bytes = "1.6.0"
colored = "2.1.0"
dirs = "5.0.1"
form_urlencoded = "1.2.1"
futures-util = "0.3.30"
once_cell = "1.19.0"
Expand Down
22 changes: 17 additions & 5 deletions backend/src/config_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};

use crate::{
app_util::get_app_root_dir,
fs_util::{read_config_file_to_string, write_config_to_file},
fs_util::{build_downloads_dir_path, read_config_file_to_string, write_config_to_file},
net_util::LoginSession,
rt_util::QuitUnwrap,
};
Expand Down Expand Up @@ -150,9 +150,14 @@ pub async fn load_config() {
pub async fn get_config_from_file() -> UFCRConfig {
let conf_file = read_config_file_to_string(&CONFIG_PATH).await;

serde_json::from_str(&conf_file).unwrap_or_quit(
let mut config: UFCRConfig = serde_json::from_str(&conf_file).unwrap_or_quit(
r#"Invalid configuration format. Please reset your "config.json" file or check the configuration"#,
)
);

config.dl_path = build_downloads_dir_path(config.dl_path)
.unwrap_or_quit("Failed to build the path for user's downloads directory");

config
}

/// Returns the current configuration.
Expand Down Expand Up @@ -205,7 +210,7 @@ pub async fn inc_file_number() {

#[cfg(test)]
mod tests {
use crate::rt_util::set_custom_panic;
use crate::{fs_util::build_downloads_dir_path, rt_util::set_custom_panic};

use super::{get_config, is_debug, load_config, UFCRConfig};

Expand All @@ -218,7 +223,14 @@ mod tests {
#[tokio::test]
async fn unit_get_config() {
load_config().await;
assert_eq!(get_config().as_ref(), &UFCRConfig::default());

let config = get_config();
let default_config = UFCRConfig {
dl_path: build_downloads_dir_path(config.dl_path.clone()).unwrap(),
..UFCRConfig::default()
};

assert_eq!(config.as_ref(), &default_config);
}

#[test]
Expand Down
26 changes: 26 additions & 0 deletions backend/src/fs_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ use std::path::PathBuf;

use anyhow::Context;
use bytes::Bytes;
use dirs::home_dir;
use futures_util::{Stream, StreamExt};
use path_absolutize::Absolutize;
use rust_embed::RustEmbed;
use tokio::{fs, io::AsyncWriteExt};

use crate::{
app_util::is_container,
config_util::{get_config, is_debug},
rt_util::QuitUnwrap,
};
Expand Down Expand Up @@ -108,3 +111,26 @@ pub fn open_downloads_dir() -> anyhow::Result<()> {

Ok(())
}

/// Generates the path to downloads directory depending on the source path and the OS
/// and returns it as a String.
pub fn build_downloads_dir_path(org_dl_path: String) -> anyhow::Result<String> {
if is_container() {
Ok("/downloads".to_string())
} else if org_dl_path.is_empty() {
let home_opt = home_dir();

if let Some(home) = home_opt {
Ok(home
.join("Downloads")
.absolutize()?
.to_str()
.context("Failed to convert absolute path to a string")?
.to_string())
} else {
Ok("~/Downloads".to_string())
}
} else {
Ok(org_dl_path)
}
}

0 comments on commit cac3417

Please sign in to comment.