Skip to content

Commit

Permalink
[fs_util, config_util] Create default config file if doesn't exist
Browse files Browse the repository at this point in the history
Other changes:
- [config_util] Manually implement defaults for `UFCRConfig`
  • Loading branch information
m4heshd committed Apr 13, 2024
1 parent 4e300fd commit 5d3d534
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
60 changes: 58 additions & 2 deletions backend/src/config_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
};

// Structs
#[derive(Default, Clone, Serialize, Deserialize)]
#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UFCRConfig {
pub open_in_browser: bool,
Expand Down Expand Up @@ -52,7 +52,52 @@ pub struct UFCRConfig {
pub dl_args: Vec<String>,
}

#[derive(Default, Clone, Serialize, Deserialize)]
impl Default for UFCRConfig {
fn default() -> Self {
UFCRConfig {
open_in_browser: true,
port: 8383,
verbose_logging: false,
api_key: "857a1e5d-e35e-4fdf-805b-a87b6f8364bf".into(),
search_api_key: "e55ccb3db0399eabe2bfc37a0314c346".into(),
region: "dce.ufc".into(),
user: String::new(),
refresh_token: String::new(),
auth_token: String::new(),
search_title_only: false,
show_thumb: true,
show_duration: true,
show_desc: true,
resolution: "720".into(),
merge_ext: "mp4".into(),
vid_quality: "worstvideo".into(),
aud_quality: "bestaudio".into(),
dl_path: String::new(),
number_files: true,
cur_number: 1,
multi_frag: true,
concur_frags: 64,
throttle: false,
dl_rate: "100K".into(),
cus_format: false,
format_id: String::new(),
metadata: false,
use_proxy: false,
proxy_config: ProxyConfig::default(),
dl_args: vec![
"--no-warnings".into(),
"--no-mtime".into(),
"--output-na-placeholder".into(),
"\"\"".into(),
"--no-cache-dir".into(),
"--ignore-config".into(),
"--no-check-certificate".into(),
],
}
}
}

#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProxyConfig {
pub protocol: String,
Expand All @@ -61,6 +106,17 @@ pub struct ProxyConfig {
pub auth: ProxyAuth,
}

impl Default for ProxyConfig {
fn default() -> Self {
ProxyConfig {
protocol: "http".into(),
host: "0.0.0.0".into(),
port: 1111,
auth: ProxyAuth::default(),
}
}
}

#[derive(Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProxyAuth {
Expand Down
28 changes: 24 additions & 4 deletions backend/src/fs_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use futures_util::{Stream, StreamExt};
use rust_embed::RustEmbed;
use tokio::{fs, io::AsyncWriteExt};

use crate::{config_util::get_config, rt_util::QuitUnwrap};
use crate::{
config_util::{get_config, is_debug},
rt_util::QuitUnwrap,
};

// Structs
/// Holds all the static files for UFC Ripper GUI that will be served using axum.
Expand All @@ -16,10 +19,27 @@ use crate::{config_util::get_config, rt_util::QuitUnwrap};
pub struct WebAssets;

/// Reads the config.json file from the disk and returns the content as `String`.
/// Will create the default config file if it doesn't exist.
pub async fn read_config_file_to_string(path: &PathBuf) -> String {
fs::read_to_string(path).await.unwrap_or_quit(
r#"Unable to read "config.json" file. Check if the file exists in "config" directory"#,
)
let read = async {
fs::read_to_string(path).await.unwrap_or_quit(
r#"Unable to read "config.json" file. Check if the file exists in "config" directory"#,
)
};

if path.exists() {
read.await
} else {
if is_debug() {
println!("\"config.json\" file not found. Creating the default config file.\n");
}

write_config_to_file(path)
.await
.unwrap_or_quit("An error occurred while trying to create a new config file");

read.await
}
}

/// Writes the current configuration to config.json file.
Expand Down

0 comments on commit 5d3d534

Please sign in to comment.