From f98c8a1ef73392d54425409776c4d6f6c52ee2b5 Mon Sep 17 00:00:00 2001 From: thevickypedia Date: Tue, 13 Feb 2024 19:42:46 -0600 Subject: [PATCH] Update docstrings and bump version --- Cargo.toml | 2 +- src/routes/auth.rs | 8 ++++---- src/routes/authenticator.rs | 12 ++++++------ src/routes/images.rs | 25 +++++++++++++++++++++++++ src/squire/ascii_art.rs | 7 +++++++ 5 files changed, 43 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c72432b..754f0c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ # https://doc.rust-lang.org/cargo/getting-started/first-steps.html#first-steps-with-cargo [package] name = "RuStream" -version = "0.0.1" +version = "0.0.2" description = "An API written in Rust, to stream videos using Actix framework, via authenticated sessions" license = "MIT" documentation = "https://docs.rs/RuStream" diff --git a/src/routes/auth.rs b/src/routes/auth.rs index 097bed6..b2412a6 100644 --- a/src/routes/auth.rs +++ b/src/routes/auth.rs @@ -28,7 +28,7 @@ pub struct DetailError { /// # Arguments /// /// * `config` - Configuration data for the application. -/// * `request` - HTTP request object. +/// * `request`: Actix HttpRequest containing information about the incoming request. #[post("/login")] pub async fn login(config: web::Data>, request: HttpRequest) -> HttpResponse { let verified = routes::authenticator::verify_login(&request, &config); @@ -67,7 +67,7 @@ pub async fn login(config: web::Data>, request: Ht /// # Arguments /// /// * `config` - Configuration data for the application. -/// * `request` - HTTP request object. +/// * `request`: Actix HttpRequest containing information about the incoming request. #[get("/logout")] pub async fn logout(config: web::Data>, request: HttpRequest) -> HttpResponse { @@ -115,7 +115,7 @@ pub async fn logout(config: web::Data>, /// # Arguments /// /// * `config` - Configuration data for the application. -/// * `request` - HTTP request object. +/// * `request`: Actix HttpRequest containing information about the incoming request. #[get("/home")] pub async fn home(config: web::Data>, request: HttpRequest) -> HttpResponse { @@ -151,7 +151,7 @@ pub async fn home(config: web::Data>, /// /// # Arguments /// -/// * `request` - HTTP request object. +/// * `request`: Actix HttpRequest containing information about the incoming request. #[get("/error")] pub async fn error(request: HttpRequest) -> HttpResponse { if let Some(detail) = request.cookie("detail") { diff --git a/src/routes/authenticator.rs b/src/routes/authenticator.rs index 564a347..a88a5a5 100644 --- a/src/routes/authenticator.rs +++ b/src/routes/authenticator.rs @@ -163,19 +163,19 @@ pub fn verify_token(request: &HttpRequest, config: &Data config.session_duration as i64 { return AuthToken { ok: false, detail: "Session Expired".to_string(), username }; } - return AuthToken { + AuthToken { ok: true, detail: format!("Session valid for {}s", timestamp + config.session_duration as i64 - current_time), username - }; + } } else { - return AuthToken { + AuthToken { ok: false, detail: "Invalid session token".to_string(), username: "NA".to_string() - }; + } } } else { - return AuthToken { + AuthToken { ok: false, detail: "Session information not found".to_string(), username: "NA".to_string() - }; + } } } diff --git a/src/routes/images.rs b/src/routes/images.rs index 190adfd..df325df 100644 --- a/src/routes/images.rs +++ b/src/routes/images.rs @@ -1,5 +1,6 @@ use std::path::PathBuf; use std::string::ToString; + use actix_web::{HttpRequest, HttpResponse, web}; use lazy_static::lazy_static; @@ -9,25 +10,49 @@ lazy_static! { static ref IMAGES: PathBuf = PathBuf::new().join(env!("CARGO_MANIFEST_DIR")).join("src").join("images"); } +/// An Actix web handler for serving images based on the requested filename. +/// +/// # Parameters +/// +/// * `request` - Actix HttpRequest containing information about the incoming request. +/// * `filename` - Extracted from the request path, the name of the requested image file. +/// +/// # Returns +/// +/// - `HttpResponse`: Responds with the requested image content if found, or raises a 404. #[get("/images/{filename:.*}")] pub async fn image_endpoint(request: HttpRequest, filename: web::Path) -> HttpResponse { + // Log the incoming connection for monitoring purposes squire::logger::log_connection(&request); log::debug!("Image requested: {}", &filename); + + // Define allowed image file types let allowed_types = ["jpeg", "jpg", "png", "gif"]; + + // Extract the file extension from the requested filename let extension = filename.split('.').last().unwrap_or("NA"); + + // Determine the image file type and format let filetype = if allowed_types.contains(&extension) { format!("image/{}", &extension) } else { + // Return a BadRequest response if the file type is not allowed return HttpResponse::BadRequest().json(routes::auth::DetailError { detail: format!("'{}' is not an allowed filetype", &extension) }); }; + + // Construct the full file path for the requested image let filepath = IMAGES.join(filename.to_string()); log::debug!("Image file lookup: {}", &filepath.to_string_lossy()); + + // Attempt to read the image content from the file match web::block(|| std::fs::read(filepath)).await { + // Respond with the image content if successful Ok(image_content) => HttpResponse::Ok() .content_type(filetype) .body(image_content.unwrap()), + // Return a NotFound response if the file is not found Err(_) => HttpResponse::NotFound().json(routes::auth::DetailError { detail: format!("'{}' was not found", &filename) }) diff --git a/src/squire/ascii_art.rs b/src/squire/ascii_art.rs index af889f0..0de27b4 100644 --- a/src/squire/ascii_art.rs +++ b/src/squire/ascii_art.rs @@ -1,3 +1,8 @@ +// References: +// https://www.asciiart.eu/ +// https://asciiart.cc/ + +/// ASCII art of a horse pub static HORSE: &str = r" # # %%% ## ## @@ -43,6 +48,7 @@ pub static HORSE: &str = r" "; +/// ASCII art of a dog pub static DOG: &str = r###" __. .-".' .--. _..._ @@ -80,6 +86,7 @@ pub static DOG: &str = r###" '..__L.:-' "###; +/// ASCII art of a dolphin pub static DOLPHIN: &str = r###" _______ .,add88YYYYY88ba,