This commit is contained in:
@@ -5,33 +5,45 @@ use std::{cmp::max, collections::HashMap, io::Cursor};
|
||||
pub const THUMB_WIDTHS: [u32; 7] = [10, 40, 110, 300, 600, 800, 1400];
|
||||
|
||||
/// Парсит запрос на миниатюру, извлекая оригинальное имя файла и требуемую ширину.
|
||||
/// Пример: "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)> {
|
||||
/// Пример: "filename_150.ext" -> ("filename.ext", 150, "ext")
|
||||
/// unsafe/1440x/production/image/439efaa0-816f-11ef-b201-439da98539bc.jpg -> ("439efaa0-816f-11ef-b201-439da98539bc.jpg", 1440, "jpg")
|
||||
/// unsafe/production/image/5627e002-0c53-11ee-9565-0242ac110006.png -> ("5627e002-0c53-11ee-9565-0242ac110006.png", 0, "png")
|
||||
pub fn parse_image_request(path: &str) -> (String, u32, String) {
|
||||
let mut path_parts = path.rsplit('/').collect::<Vec<&str>>();
|
||||
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 filename_part = path_parts.pop().unwrap_or("");
|
||||
let mut width = 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::<u32>() {
|
||||
oldwidth = width;
|
||||
}
|
||||
if let Ok(w) = old_width_str.parse::<u32>() {
|
||||
width = w;
|
||||
}
|
||||
}
|
||||
}
|
||||
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::<u32>() {
|
||||
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
|
||||
|
||||
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(w) = width_str.parse::<u32>() {
|
||||
return (
|
||||
format!("{}.{}", base_name, ext_part),
|
||||
max(w, width),
|
||||
ext_part.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
(
|
||||
format!("{}.{}", name_part, ext_part),
|
||||
width,
|
||||
ext_part.to_string().to_lowercase(),
|
||||
)
|
||||
} else {
|
||||
// Если расширение отсутствует, возвращаем имя файла как есть, ширину 0 и пустую строку для расширения
|
||||
(filename_part.to_string(), width, "".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// Выбирает ближайший подходящий размер из предопределённых.
|
||||
|
Reference in New Issue
Block a user