Skip to content

Commit

Permalink
Remove python clinginess and pyo3 dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Feb 17, 2024
1 parent c4b6aff commit 14bbd8c
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 299 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ sha2 = "0.10.8"
rand = "0.8.5"
fernet = "0.2.1"
minijinja = { version = "1.0.12", features = ["loader"] }
pyo3 = { version = "0.20.2", features = ["auto-initialize"] }
url = "2.5.0"
itertools = "0.12.1"
openssl = "0.10"
regex = "1.5"
walkdir = "2.3.2"
88 changes: 0 additions & 88 deletions src/python/fileio.py

This file was deleted.

22 changes: 0 additions & 22 deletions src/routes/auth.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::sync::{Arc, Mutex};
use std::time::Instant;

use actix_web::{HttpRequest, HttpResponse, web};
use actix_web::cookie::Cookie;
use actix_web::cookie::time::{Duration, OffsetDateTime};
use actix_web::http::StatusCode;
use itertools::Itertools;
use minijinja;
use serde::Serialize;

Expand Down Expand Up @@ -129,27 +127,7 @@ pub async fn home(config: web::Data<Arc<squire::settings::Config>>,
squire::logger::log_connection(&request);
log::debug!("{}", auth_response.detail);

let start_rust = Instant::now();
let listing_page = squire::content::get_all_stream_content(&config);
let rust_time_taken = start_rust.elapsed();

let start_python = Instant::now();
let default_values = squire::settings::default_file_formats();
// https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.collect_tuple
let _file_format = config.file_formats.iter().collect_tuple();
let file_format = if _file_format.is_none() {
log::debug!("CRITICAL::Failed to extract tuple from {:?}", config.file_formats);
default_values.iter().collect_tuple()
} else {
_file_format
};
let args = (config.video_source.to_string_lossy().to_string(), file_format.unwrap());
let _listing_page = squire::fileio::get_all_stream_content(args);
let python_time_taken = start_python.elapsed();

println!("home_page [py]: {} seconds", python_time_taken.as_secs_f64());
println!("home_page [rs]: {} seconds", rust_time_taken.as_secs_f64());

let template = environment.lock().unwrap();
let listing = template.get_template("listing").unwrap();

Expand Down
58 changes: 6 additions & 52 deletions src/routes/video.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Instant;

use actix_web::{HttpRequest, HttpResponse, web};
use actix_web::http::StatusCode;
use itertools::Itertools;
use minijinja::{context, Environment};
use serde::Deserialize;
use url::form_urlencoded;
Expand Down Expand Up @@ -34,6 +32,9 @@ struct Subtitles {
///
/// * `path` - The input path string to be URL encoded.
///
/// # References:
/// - [RustJobs](https://rustjobs.dev/blog/how-to-url-encode-strings-in-rust/)
///
/// # Returns
///
/// Returns a URL encoded string.
Expand Down Expand Up @@ -133,34 +134,7 @@ pub async fn stream(config: web::Data<Arc<squire::settings::Config>>,
let template = environment.lock().unwrap();
if __target.is_file() {
let landing = template.get_template("landing").unwrap();
let start_rust = Instant::now();
let rust_iter = squire::content::get_iter(&__target, &config.file_formats);
let rust_time_taken = start_rust.elapsed();

let start_python = Instant::now();
let default_values = squire::settings::default_file_formats();
// https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.collect_tuple
let _file_format = config.file_formats.iter().collect_tuple();
let file_format = if _file_format.is_none() {
log::debug!("CRITICAL::Failed to extract tuple from {:?}", config.file_formats);
default_values.iter().collect_tuple()
} else {
_file_format
};
// full path required to read directory
let args = (&__target_str, file_format.unwrap());
let iter = squire::fileio::get_iter(args);
let python_time_taken = start_python.elapsed();

if rust_iter.previous == iter.previous && rust_iter.next == iter.next {
println!("iter [py]: {} seconds", python_time_taken.as_secs_f64());
println!("iter [rs]: {} seconds", rust_time_taken.as_secs_f64());
} else {
println!("iter [rs]::{:?}", &rust_iter);
println!("iter [py]::{:?}", &iter);
}

// https://rustjobs.dev/blog/how-to-url-encode-strings-in-rust/
let render_path = format!("/video?file={}", url_encode(&filepath));
// Rust doesn't allow re-assignment, so might as well create a mutable variable
// Load the default response body and re-construct with subtitles if present
Expand All @@ -183,9 +157,9 @@ pub async fn stream(config: web::Data<Arc<squire::settings::Config>>,
track => sfx_file
)).unwrap();
} else if subtitle.srt.exists() {
log::info!("Converting '{:?}' to '{:?}' for subtitles",
subtitle.srt.file_name(),
subtitle.vtt.file_name());
log::info!("Converting {:?} to {:?} for subtitles",
subtitle.srt.file_name().unwrap(),
subtitle.vtt.file_name().unwrap());
match squire::subtitles::srt_to_vtt(&subtitle.srt) {
Ok(_) => {
log::debug!("Successfully converted srt to vtt file");
Expand All @@ -206,27 +180,7 @@ pub async fn stream(config: web::Data<Arc<squire::settings::Config>>,
.content_type("text/html; charset=utf-8").body(response_body);
} else if __target.is_dir() {
let child_dir = __target.iter().last().unwrap().to_string_lossy().to_string();
let start_rust = Instant::now();
let listing_page = squire::content::get_dir_stream_content(&__target_str, &child_dir, &config.file_formats);
let rust_time_taken = start_rust.elapsed();

let start_python = Instant::now();
let default_values = squire::settings::default_file_formats();
// https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.collect_tuple
let _file_format = config.file_formats.iter().collect_tuple();
let file_format = if _file_format.is_none() {
log::debug!("CRITICAL::Failed to extract tuple from {:?}", config.file_formats);
default_values.iter().collect_tuple()
} else {
_file_format
};
let args = (__target_str, child_dir, file_format.unwrap());
let _listing_page = squire::fileio::get_dir_stream_content(args);
let python_time_taken = start_python.elapsed();

println!("listing_page [py]: {} seconds", python_time_taken.as_secs_f64());
println!("listing_page [rs]: {} seconds", rust_time_taken.as_secs_f64());

let listing = template.get_template("listing").unwrap();
return HttpResponse::build(StatusCode::OK)
.content_type("text/html; charset=utf-8")
Expand Down
2 changes: 1 addition & 1 deletion src/squire/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub fn get_all_stream_content(config: &Config) -> ContentPayload {
/// # Returns
///
/// A `ContentPayload` struct representing the content of the specified directory.
pub fn get_dir_stream_content(parent: &str, subdir: &str, file_formats: &Vec<String>) -> ContentPayload {
pub fn get_dir_stream_content(parent: &str, subdir: &str, file_formats: &[String]) -> ContentPayload {
let mut files = Vec::new();
for entry in fs::read_dir(parent).unwrap().flatten() {
let file_name = entry.file_name().into_string().unwrap();
Expand Down
131 changes: 0 additions & 131 deletions src/squire/fileio.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/squire/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod parser;
pub mod settings;
pub mod startup;
pub mod secure;
pub mod fileio;
pub mod logger;
pub mod ascii_art;
pub mod middleware;
Expand Down
3 changes: 1 addition & 2 deletions src/squire/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ fn default_session_duration() -> i32 {
///
/// Set as public, since this function is re-used in `startup.rs`
pub fn default_file_formats() -> Vec<String> {
// todo: remove the dot (.)
vec![".mp4".to_string(), ".mov".to_string()]
vec!["mp4".to_string(), "mov".to_string()]
}

/// Returns the default number of worker threads (half of logical cores).
Expand Down

0 comments on commit 14bbd8c

Please sign in to comment.