diff --git a/justfile b/justfile index e1852cb..500c042 100644 --- a/justfile +++ b/justfile @@ -52,6 +52,7 @@ clean: # SSG ssg: - wget --no-convert-links -r -p -E -P dist --no-host-directories 127.0.0.1:{{port}} + find generated_images/ -name "*_og*" -exec cp --parents {} dist/ \; # Preview server preview: diff --git a/src/picture_generator/image_src_generator.rs b/src/picture_generator/image_src_generator.rs new file mode 100644 index 0000000..f289da7 --- /dev/null +++ b/src/picture_generator/image_src_generator.rs @@ -0,0 +1,63 @@ +use std::{path::Path, sync::Arc}; + +use anyhow::Context; +use image::ImageReader; +use tracing::debug; + +use super::{ + image_generator::generate_images, + picture_markup_generator::{get_export_formats, get_generated_file_name, get_image_path}, +}; + +pub fn generate_image_with_src( + orig_img_path: &str, + width: u32, + height: u32, + suffix: &str, + generate_image: bool, +) -> Result { + let path_to_generated = get_generated_file_name(orig_img_path); + let file_stem = path_to_generated.file_stem().unwrap().to_str().unwrap(); + let path_to_generated = path_to_generated.with_file_name(format!("{file_stem}{suffix}")); + + let disk_img_path = + Path::new("static/").join(orig_img_path.strip_prefix("/").unwrap_or(orig_img_path)); + let resolutions = [(width, height, 1.)]; + + let exported_formats = get_export_formats(orig_img_path); + let exported_format = *exported_formats.first().unwrap(); + + let path_to_generated_arc = Arc::new(path_to_generated); + let path_to_generated_clone = Arc::clone(&path_to_generated_arc); + + if generate_image { + rayon::spawn(move || { + let orig_img = ImageReader::open(&disk_img_path) + .with_context(|| format!("Failed to read instrs from {:?}", &disk_img_path)) + .unwrap() + .decode() + .unwrap(); + let path_to_generated = path_to_generated_clone.as_ref(); + + let result = generate_images( + &orig_img, + path_to_generated, + &resolutions, + &[exported_format], + ) + .with_context(|| "Failed to generate images".to_string()); + if let Err(e) = result { + tracing::error!("Error: {}", e); + } + }); + } + let path_to_generated = Arc::clone(&path_to_generated_arc); + + let image_path = get_image_path( + &path_to_generated, + resolutions.first().expect("Should this error ever happen?"), + &exported_format, + ); + + Ok(image_path) +} diff --git a/src/picture_generator/mod.rs b/src/picture_generator/mod.rs index 6b06bf2..f8109a9 100644 --- a/src/picture_generator/mod.rs +++ b/src/picture_generator/mod.rs @@ -23,5 +23,6 @@ It should be used from the templates as well pub mod export_format; pub mod image_generator; +pub mod image_src_generator; pub mod picture_markup_generator; pub mod resolutions; diff --git a/src/picture_generator/picture_markup_generator.rs b/src/picture_generator/picture_markup_generator.rs index 7ce8a44..1501d26 100644 --- a/src/picture_generator/picture_markup_generator.rs +++ b/src/picture_generator/picture_markup_generator.rs @@ -119,7 +119,7 @@ pub fn generate_picture_markup( Ok(result) } -fn get_image_path(path: &Path, resolution: &(u32, u32, f32), format: &ExportFormat) -> String { +pub fn get_image_path(path: &Path, resolution: &(u32, u32, f32), format: &ExportFormat) -> String { let path_name = path.to_str().expect("Image has to have a valid path"); let (width, height, _) = resolution; let extension = format.get_extension(); @@ -197,7 +197,7 @@ fn strip_prefixes(path: &Path) -> &Path { parent_path } -fn get_generated_file_name(orig_img_path: &str) -> PathBuf { +pub fn get_generated_file_name(orig_img_path: &str) -> PathBuf { let path = Path::new(&orig_img_path); // let parent = path // .parent() @@ -247,7 +247,7 @@ fn generate_srcset(path: &Path, format: &ExportFormat, resolutions: &[(u32, u32, .join(", ") } -fn get_export_formats(orig_img_path: &str) -> Vec { +pub fn get_export_formats(orig_img_path: &str) -> Vec { let path = Path::new(&orig_img_path) .extension() .and_then(|ext| ext.to_str()); diff --git a/templates/blog_post.html b/templates/blog_post.html index 6b405aa..c91cfa3 100644 --- a/templates/blog_post.html +++ b/templates/blog_post.html @@ -6,7 +6,8 @@ {% match thumbnail %} {% when Some with (img) %} - +{% let src = crate::picture_generator::image_src_generator::generate_image_with_src(img, 1200, 630, "_og", true).unwrap_or("thumbnail not found".to_string())|safe %} + {% when None %} {% endmatch %}