get original filename

This commit is contained in:
Michal Vanko 2024-08-16 23:59:48 +02:00
parent 9b81d70ae2
commit 411b6adff7
2 changed files with 48 additions and 22 deletions

View File

@ -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

View File

@ -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!("<picture></picture>");
result
}
fn get_generated_paths(orig_img_path: &str, &resolutions: Vec<(i32, i32)>) -> Vec<String> {
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<ExportFormat> {
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() {