From 411b6adff7667822ba5f841d5c78d67a95ac7eea Mon Sep 17 00:00:00 2001 From: Michal Vanko Date: Fri, 16 Aug 2024 23:59:48 +0200 Subject: [PATCH] get original filename --- axum_server/justfile | 5 +- .../picture_markup_generator.rs | 65 +++++++++++++------ 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/axum_server/justfile b/axum_server/justfile index 281234c..987aed1 100644 --- a/axum_server/justfile +++ b/axum_server/justfile @@ -25,10 +25,13 @@ dev: just tailwind just decap_server -# Run dev server in watch mode +# Run tests test: cargo test +test_watch: + cargo watch -x test + # Run server in production mode prod: cargo run --release diff --git a/axum_server/src/picture_generator/picture_markup_generator.rs b/axum_server/src/picture_generator/picture_markup_generator.rs index 5d1d653..8a0b474 100644 --- a/axum_server/src/picture_generator/picture_markup_generator.rs +++ b/axum_server/src/picture_generator/picture_markup_generator.rs @@ -1,7 +1,6 @@ -use std::path::Path; +use std::path::{Path, PathBuf}; - -pub const PIXEL_DENSITIES: [f64; 5] = [1., 1.5, 2., 3., 4.]; +pub const PIXEL_DENSITIES: [f32; 5] = [1., 1.5, 2., 3., 4.]; #[derive(Debug, PartialEq)] pub enum ExportFormat { @@ -12,34 +11,53 @@ pub enum ExportFormat { } pub fn generate_picture_markup(orig_img_path: &str, width: u32, height: u32) -> String { - let exported_formats = get_export_formats(&orig_img_path); + let exported_formats = get_export_formats(orig_img_path); + let path_to_generated = get_generated_file_name(orig_img_path); + let resolutions = vec![ + (300, 200, 1.), + (450, 300, 1.5), + (600, 400, 2.), + (900, 600, 3.), + (1200, 800, 4.), + ]; for export_format in exported_formats { // TODO get original img resolution and determine how many exports are going to be needed - // let orig_img_resolution = + // let orig_img_resolution = // let resolutions = get_resolutions(width, height); - - let resolutions = vec![(300, 200), (450, 300), (600, 400), (900, 600), (1200, 800)]; - let generated_paths = get_generated_paths(orig_img_path, resolutions); - } let result = format!(""); result } -fn get_generated_paths(orig_img_path: &str, &resolutions: Vec<(i32, i32)>) -> Vec { - +fn get_generated_file_name(orig_img_path: &str) -> PathBuf { + let path = Path::new(&orig_img_path); + let parent = path + .parent() + .expect("There should be a parent route to an image") + .strip_prefix(".") + .unwrap(); + let file_name = path + .file_stem() + .expect("There should be a name for every img"); + let result = Path::new("./generated_images/") + .join(parent) + .join(file_name); + result } #[test] -fn test_get_generated_paths { +fn test_get_generated_paths() { let orig_img_path = "./images/uploads/img_name.jpg"; - let resolutions = vec![(300, 200), (450, 300), (600, 400), (900, 600), (1200, 800)]; - assert_eq!(get_generated_paths(orig_img_path,&resolutions), vec![]) + assert_eq!( + get_generated_file_name(orig_img_path) + .to_str() + .unwrap_or(""), + "./generated_images/images/uploads/img_name" + ); } - // TODO get original img resolution and determine how many exports are going to be needed fn get_resolutions(orig_width: i32, orig_height: i32, width: i32, height: i32) -> Vec<(i32, i32)> { todo!("get original img resolution and determine how many exports are going to be needed") @@ -50,18 +68,23 @@ fn generate_srcset(new_path: &str, width: u32, height: u32) -> &str { } fn get_export_formats(orig_img_path: &str) -> Vec { - let path = Path::new(&orig_img_path).extension().and_then(|ext| ext.to_str()); + let path = Path::new(&orig_img_path) + .extension() + .and_then(|ext| ext.to_str()); match path { - Some("jpg" | "jpeg") => vec![ExportFormat::AVIF, ExportFormat::JPG] - Some("png") => vec![ExportFormat::AVIF, ExportFormat::PNG] - Some(_) | None => vec![] + Some("jpg" | "jpeg") => vec![ExportFormat::AVIF, ExportFormat::JPG], + Some("png") => vec![ExportFormat::AVIF, ExportFormat::PNG], + Some(_) | None => vec![], } } #[test] -fn test_get_export_formats { - assert_eq!(get_export_formats("./images/uploads/img_name.jpg"), vec![ExportFormat::AVIF, ExportFormat::JPG]) +fn test_get_export_formats() { + assert_eq!( + get_export_formats("./images/uploads/img_name.jpg"), + vec![ExportFormat::AVIF, ExportFormat::JPG] + ) } #[test] fn test_generate_srcset() {