detect-old-width
All checks were successful
deploy / deploy (push) Successful in 4m15s

This commit is contained in:
Untone 2024-10-23 12:34:34 +03:00
parent 9051bc2648
commit 911284c8ae
3 changed files with 27 additions and 11 deletions

2
Cargo.lock generated
View File

@ -1273,7 +1273,7 @@ dependencies = [
[[package]]
name = "discoursio-quoter"
version = "0.0.8"
version = "0.0.9"
dependencies = [
"actix",
"actix-multipart",

View File

@ -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

View File

@ -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::<u32>() {
return Some((base_name.to_string(), width, ext_part.to_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 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;
}
}
}
}
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::<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
}
@ -30,7 +46,7 @@ pub fn find_closest_width(requested_width: u32) -> u32 {
pub async fn generate_thumbnails(image: &DynamicImage) -> Result<HashMap<u32, Vec<u8>>, 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