0.0.6-onecachekey

This commit is contained in:
2024-10-22 09:38:30 +03:00
parent 9fcce86075
commit 8ff3f018b5
7 changed files with 95 additions and 123 deletions

View File

@@ -2,7 +2,7 @@ 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];
pub const THUMB_WIDTHS: [u32; 6] = [10, 40, 110, 300, 600, 800];
/// Парсит запрос на миниатюру, извлекая оригинальное имя файла и требуемую ширину.
/// Пример: "filename_150.ext" -> ("filename.ext", 150)
@@ -19,20 +19,17 @@ pub fn parse_thumbnail_request(path: &str) -> Option<(String, u32, String)> {
/// Выбирает ближайший подходящий размер из предопределённых.
pub fn find_closest_width(requested_width: u32) -> u32 {
*ALLOWED_THUMBNAIL_WIDTHS
*THUMB_WIDTHS
.iter()
.min_by_key(|&&width| (width as i32 - requested_width as i32).abs())
.unwrap_or(&ALLOWED_THUMBNAIL_WIDTHS[0]) // Возвращаем самый маленький размер, если ничего не подошло
.unwrap_or(&THUMB_WIDTHS[0]) // Возвращаем самый маленький размер, если ничего не подошло
}
/// Генерирует миниатюры изображения для заданного набора ширин.
pub async fn generate_thumbnails(
image: &DynamicImage,
widths: &[u32],
) -> Result<HashMap<u32, Vec<u8>>, actix_web::Error> {
pub async fn generate_thumbnails(image: &DynamicImage) -> Result<HashMap<u32, Vec<u8>>, actix_web::Error> {
let mut thumbnails = HashMap::new();
for &width in widths {
for width in THUMB_WIDTHS {
let thumbnail = image.resize(width, u32::MAX, FilterType::Lanczos3); // Ресайз изображения по ширине
let mut buffer = Vec::new();
thumbnail