From bd1b8fc6eae1c5348a847324b81c35e0445c47c6 Mon Sep 17 00:00:00 2001 From: thevickypedia Date: Tue, 12 Mar 2024 14:08:59 -0500 Subject: [PATCH] Display confirmation window to public space uploads Update docstrings and bump version to prerelease --- Cargo.toml | 2 +- src/squire/responses.rs | 24 ++++++++++++++++++--- src/squire/startup.rs | 48 ++++++++++++++++++++++++++++++++++------- src/templates/upload.rs | 16 +++++++++++++- 4 files changed, 77 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e52820f..14ecd5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "RuStream" -version = "1.2.0" +version = "1.3.0-a" description = "Self-hosted Streaming Engine, that can render media files via authenticated sessions." license = "MIT" documentation = "https://docs.rs/RuStream" diff --git a/src/squire/responses.rs b/src/squire/responses.rs index bdc9042..a64b03a 100644 --- a/src/squire/responses.rs +++ b/src/squire/responses.rs @@ -2,8 +2,16 @@ use actix_web::http::StatusCode; use actix_web::HttpResponse; use minijinja::Template; -// todo: write docstrings - +/// Frames a response for Not Found [404] into an error page. +/// +/// # Arguments +/// +/// * `error` - Jinja template for the error page. +/// * `description` - Description to be rendered in the UI. +/// +/// # Returns +/// +/// Returns an HTTPResponse with 404 error code formatted as HTML. pub fn not_found(error: Template, description: &String) -> HttpResponse { HttpResponse::build(StatusCode::NOT_FOUND) .content_type("text/html; charset=utf-8") @@ -15,8 +23,18 @@ pub fn not_found(error: Template, description: &String) -> HttpResponse { )).unwrap()) } +/// Frames a response for Forbidden [403] into an error page. +/// +/// # Arguments +/// +/// * `error` - Jinja template for the error page. +/// * `username` - Username whose access is forbidden. +/// +/// # Returns +/// +/// Returns an HTTPResponse with 403 error code formatted as HTML. pub fn restricted(error: Template, username: &String) -> HttpResponse { - HttpResponse::build(StatusCode::UNAUTHORIZED) + HttpResponse::build(StatusCode::FORBIDDEN) .content_type("text/html; charset=utf-8") .body(error.render(minijinja::context!( title => "RESTRICTED SECTION", diff --git a/src/squire/startup.rs b/src/squire/startup.rs index 00c1eaf..65828f1 100644 --- a/src/squire/startup.rs +++ b/src/squire/startup.rs @@ -217,14 +217,29 @@ fn load_env_vars() -> settings::Config { } } +/// Get the current time in a specific format. +/// +/// # Arguments +/// +/// * `utc` - Boolean flag to return the time in UTC timezone. +/// +/// # Returns +/// +/// Returns the current datetime as a `String`. fn get_time(utc: bool) -> String { if utc { - Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true) + Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string() } else { - Local::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true) + Local::now().format("%Y-%m-%dT%H:%M:%SZ").to_string() } } +/// Validates the directory structure to ensure that the secure index is present in media source's root. +/// +/// # Arguments +/// +/// * `config` - Configuration data for the application. +/// * `cargo` - Package specific information loaded in a struct. fn validate_dir_structure(config: &settings::Config, cargo: &Cargo) { let source = &config.media_source.to_string_lossy().to_string(); let mut errors = String::new(); @@ -254,13 +269,22 @@ fn validate_dir_structure(config: &settings::Config, cargo: &Cargo) { } } if errors.is_empty() { - for (username, _) in &config.authorization { + for username in config.authorization.keys() { let secure_path = &config.media_source.join(format!("{}_{}", &username, constant::SECURE_INDEX)); if !secure_path.exists() { - match std::fs::create_dir(&secure_path) { - Ok(_) => println!("[{}\x1b[32m INFO\x1b[0m {}] '{}' has been created", - get_time(config.utc_logging), cargo.crate_name, - &secure_path.to_str().unwrap()), + match std::fs::create_dir(secure_path) { + Ok(_) => { + // keep formatting similar to logging + if config.utc_logging { + println!("[{}\x1b[32m INFO\x1b[0m {}] '{}' has been created", + get_time(config.utc_logging), cargo.crate_name, + &secure_path.to_str().unwrap()) + } else { + println!("[{} INFO {}] '{}' has been created", + get_time(config.utc_logging), cargo.crate_name, + &secure_path.to_str().unwrap()) + } + }, Err(err) => panic!("{}", err) } } @@ -272,6 +296,10 @@ fn validate_dir_structure(config: &settings::Config, cargo: &Cargo) { /// Validates all the required environment variables with the required settings. /// +/// # Arguments +/// +/// * `cargo` - Package specific information loaded in a struct. +/// /// # Returns /// /// Returns the `Config` struct containing the required parameters. @@ -304,12 +332,16 @@ fn validate_vars(cargo: &Cargo) -> settings::Config { if !errors.is_empty() { panic!("{}", errors); } - validate_dir_structure(&config, &cargo); + validate_dir_structure(&config, cargo); config } /// Retrieves the environment variables and parses as the data-type specified in Config struct. /// +/// # Arguments +/// +/// * `cargo` - Package specific information loaded in a struct. +/// /// # Returns /// /// Converts the config struct into an `Arc` and returns it. diff --git a/src/templates/upload.rs b/src/templates/upload.rs index a593e27..7712a76 100644 --- a/src/templates/upload.rs +++ b/src/templates/upload.rs @@ -327,7 +327,7 @@ pub fn get_content() -> String {

Upload Files

PDF, Images, Videos and Subtitles are allowed


- +
@@ -501,6 +501,20 @@ pub fn get_content() -> String { window.history.back(); } + "#.to_string() }