This commit is contained in:
47
src/thumbnail.rs
Normal file
47
src/thumbnail.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
// thumbnail.rs
|
||||
|
||||
use actix_web::error::ErrorInternalServerError;
|
||||
use image::{imageops::FilterType, DynamicImage};
|
||||
use std::{collections::HashMap, io::Cursor};
|
||||
|
||||
pub const ALLOWED_THUMBNAIL_WIDTHS: [u32; 6] = [10, 40, 110, 300, 600, 800];
|
||||
|
||||
/// Парсит запрос на миниатюру, извлекая оригинальное имя файла и требуемую ширину.
|
||||
/// Пример: "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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Выбирает ближайший подходящий размер из предопределённых.
|
||||
pub fn find_closest_width(requested_width: u32) -> u32 {
|
||||
*ALLOWED_THUMBNAIL_WIDTHS
|
||||
.iter()
|
||||
.min_by_key(|&&width| (width as i32 - requested_width as i32).abs())
|
||||
.unwrap_or(&ALLOWED_THUMBNAIL_WIDTHS[0]) // Возвращаем самый маленький размер, если ничего не подошло
|
||||
}
|
||||
|
||||
/// Генерирует миниатюры изображения для заданного набора ширин.
|
||||
pub async fn generate_thumbnails(
|
||||
image: &DynamicImage,
|
||||
widths: &[u32],
|
||||
) -> Result<HashMap<u32, Vec<u8>>, actix_web::Error> {
|
||||
let mut thumbnails = HashMap::new();
|
||||
|
||||
for &width in widths {
|
||||
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)
|
||||
}
|
Reference in New Issue
Block a user