From 911284c8ae09c14f089be786de1716269c7e04c7 Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 23 Oct 2024 12:34:34 +0300 Subject: [PATCH] detect-old-width --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/thumbnail.rs | 34 +++++++++++++++++++++++++--------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9be320f..9f844cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1273,7 +1273,7 @@ dependencies = [ [[package]] name = "discoursio-quoter" -version = "0.0.8" +version = "0.0.9" dependencies = [ "actix", "actix-multipart", diff --git a/Cargo.toml b/Cargo.toml index 1c3b93f..a4b07e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "discoursio-quoter" -version = "0.0.8" +version = "0.0.9" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/thumbnail.rs b/src/thumbnail.rs index b7515b0..4a64332 100644 --- a/src/thumbnail.rs +++ b/src/thumbnail.rs @@ -1,19 +1,35 @@ use actix_web::error::ErrorInternalServerError; use image::{imageops::FilterType, DynamicImage}; -use std::{collections::HashMap, io::Cursor}; +use std::{cmp::max, collections::HashMap, io::Cursor}; -pub const THUMB_WIDTHS: [u32; 6] = [10, 40, 110, 300, 600, 800]; +pub const THUMB_WIDTHS: [u32; 7] = [10, 40, 110, 300, 600, 800, 1400]; /// Парсит запрос на миниатюру, извлекая оригинальное имя файла и требуемую ширину. -/// Пример: "filename_150.ext" -> ("filename.ext", 150) +/// Пример: "filename_150.ext" -> ("filename", 150, "ext") +/// unsafe/1440x/production/image/439efaa0-816f-11ef-b201-439da98539bc.jpg -> ("439efaa0-816f-11ef-b201-439da98539bc", 1440, "jpg") pub fn parse_image_request(path: &str) -> Option<(String, u32, String)> { - if let Some((name_part, ext_part)) = path.rsplit_once('.') { - if let Some((base_name, width_str)) = name_part.rsplit_once('_') { - if let Ok(width) = width_str.parse::() { - return Some((base_name.to_string(), width, ext_part.to_string())); + let mut path_parts = path.rsplit('/').collect::>(); + if let Some(filename_part) = path_parts.pop() { + let mut oldwidth = 0; + if path.starts_with("unsafe") { + if let Some(old_width_str) = path_parts.get(1) { + let mut old_width_str = old_width_str.to_string(); + if old_width_str.ends_with('x') { + old_width_str.pop(); + if let Ok(width) = old_width_str.parse::() { + oldwidth = width; + } + } } } - return Some((name_part.to_string(), 0, ext_part.to_string().to_lowercase())); + if let Some((name_part, ext_part)) = filename_part.rsplit_once('.') { + if let Some((base_name, width_str)) = name_part.rsplit_once('_') { + if let Ok(width) = width_str.parse::() { + return Some((base_name.to_string(), max(width, oldwidth), ext_part.to_string())); + } + } + return Some((name_part.to_string(), 0, ext_part.to_string().to_lowercase())); + } } None } @@ -30,7 +46,7 @@ pub fn find_closest_width(requested_width: u32) -> u32 { pub async fn generate_thumbnails(image: &DynamicImage) -> Result>, actix_web::Error> { let mut thumbnails = HashMap::new(); - for width in THUMB_WIDTHS { + for &width in THUMB_WIDTHS.iter().filter(|&&w| w < image.width()) { let thumbnail = image.resize(width, u32::MAX, FilterType::Lanczos3); // Ресайз изображения по ширине let mut buffer = Vec::new(); thumbnail