Skip to content

Commit

Permalink
Update docstrings and bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Feb 14, 2024
1 parent c4807cf commit f98c8a1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
8 changes: 4 additions & 4 deletions src/routes/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arc<squire::settings::Config>>, request: HttpRequest) -> HttpResponse {
let verified = routes::authenticator::verify_login(&request, &config);
Expand Down Expand Up @@ -67,7 +67,7 @@ pub async fn login(config: web::Data<Arc<squire::settings::Config>>, 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<Arc<squire::settings::Config>>,
request: HttpRequest) -> HttpResponse {
Expand Down Expand Up @@ -115,7 +115,7 @@ pub async fn logout(config: web::Data<Arc<squire::settings::Config>>,
/// # 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<Arc<squire::settings::Config>>,
request: HttpRequest) -> HttpResponse {
Expand Down Expand Up @@ -151,7 +151,7 @@ pub async fn home(config: web::Data<Arc<squire::settings::Config>>,
///
/// # 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") {
Expand Down
12 changes: 6 additions & 6 deletions src/routes/authenticator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,19 @@ pub fn verify_token(request: &HttpRequest, config: &Data<Arc<squire::settings::C
if current_time - timestamp > 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()
};
}
}
}
25 changes: 25 additions & 0 deletions src/routes/images.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::PathBuf;
use std::string::ToString;

use actix_web::{HttpRequest, HttpResponse, web};
use lazy_static::lazy_static;

Expand All @@ -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<String>) -> 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)
})
Expand Down
7 changes: 7 additions & 0 deletions src/squire/ascii_art.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// References:
// https://www.asciiart.eu/
// https://asciiart.cc/

/// ASCII art of a horse
pub static HORSE: &str = r"
# #
%%% ## ##
Expand Down Expand Up @@ -43,6 +48,7 @@ pub static HORSE: &str = r"
";


/// ASCII art of a dog
pub static DOG: &str = r###"
__.
.-".' .--. _..._
Expand Down Expand Up @@ -80,6 +86,7 @@ pub static DOG: &str = r###"
'..__L.:-'
"###;

/// ASCII art of a dolphin
pub static DOLPHIN: &str = r###"
_______
.,add88YYYYY88ba,
Expand Down

0 comments on commit f98c8a1

Please sign in to comment.