2024-08-31 00:32:37 +00:00
|
|
|
use actix_web::error::ErrorInternalServerError;
|
|
|
|
use image::{imageops::FilterType, DynamicImage};
|
|
|
|
use std::{collections::HashMap, io::Cursor};
|
|
|
|
|
2024-10-22 06:38:30 +00:00
|
|
|
pub const THUMB_WIDTHS: [u32; 6] = [10, 40, 110, 300, 600, 800];
|
2024-08-31 00:32:37 +00:00
|
|
|
|
|
|
|
/// Парсит запрос на миниатюру, извлекая оригинальное имя файла и требуемую ширину.
|
|
|
|
/// Пример: "filename_150.ext" -> ("filename.ext", 150)
|
|
|
|
pub fn parse_thumbnail_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::<u32>() {
|
|
|
|
return Some((base_name.to_string(), width, ext_part.to_string()));
|
|
|
|
}
|
|
|
|
}
|
2024-10-22 11:28:54 +00:00
|
|
|
return Some((name_part.to_string(), 0, ext_part.to_string()))
|
2024-08-31 00:32:37 +00:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Выбирает ближайший подходящий размер из предопределённых.
|
|
|
|
pub fn find_closest_width(requested_width: u32) -> u32 {
|
2024-10-22 06:38:30 +00:00
|
|
|
*THUMB_WIDTHS
|
2024-08-31 00:32:37 +00:00
|
|
|
.iter()
|
|
|
|
.min_by_key(|&&width| (width as i32 - requested_width as i32).abs())
|
2024-10-22 06:38:30 +00:00
|
|
|
.unwrap_or(&THUMB_WIDTHS[0]) // Возвращаем самый маленький размер, если ничего не подошло
|
2024-08-31 00:32:37 +00:00
|
|
|
}
|
|
|
|
|
2024-10-22 11:28:54 +00:00
|
|
|
/// Генерирует миниатюры изображения.
|
2024-10-22 06:38:30 +00:00
|
|
|
pub async fn generate_thumbnails(image: &DynamicImage) -> Result<HashMap<u32, Vec<u8>>, actix_web::Error> {
|
2024-08-31 00:32:37 +00:00
|
|
|
let mut thumbnails = HashMap::new();
|
|
|
|
|
2024-10-22 06:38:30 +00:00
|
|
|
for width in THUMB_WIDTHS {
|
2024-08-31 00:32:37 +00:00
|
|
|
let thumbnail = image.resize(width, u32::MAX, FilterType::Lanczos3); // Ресайз изображения по ширине
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
thumbnail
|
|
|
|
.write_to(&mut Cursor::new(&mut buffer), image::ImageFormat::Jpeg)
|
|
|
|
.map_err(|_| ErrorInternalServerError("Failed to generate thumbnail"))?; // Сохранение изображения в формате JPEG
|
|
|
|
thumbnails.insert(width, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(thumbnails)
|
|
|
|
}
|