From 2a61acba34c1a790b8b206c7da34144ae9178467 Mon Sep 17 00:00:00 2001 From: lamka02sk Date: Sun, 16 Jun 2024 02:22:53 +0200 Subject: [PATCH] Fixed cache regeneration for changed files --- Cargo.lock | 6 +++--- Cargo.toml | 2 +- src/pipeline/finalize.rs | 6 +++--- src/pipeline/mod.rs | 5 +++-- src/pipeline/thumbnail.rs | 10 +++++----- 5 files changed, 15 insertions(+), 14 deletions(-) mode change 100755 => 100644 Cargo.toml diff --git a/Cargo.lock b/Cargo.lock index 57be0a6..46d8aac 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -958,7 +958,7 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "picturium" -version = "0.1.0" +version = "0.1.1" dependencies = [ "actix-cors", "actix-files", @@ -1337,9 +1337,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.36.0" +version = "1.38.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61285f6515fa018fb2d1e46eb21223fff441ee8db5d0f1435e8ab4f5cdb80931" +checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" dependencies = [ "backtrace", "bytes", diff --git a/Cargo.toml b/Cargo.toml old mode 100755 new mode 100644 index 163b746..f121e94 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "picturium" -version = "0.1.0" +version = "0.1.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/pipeline/finalize.rs b/src/pipeline/finalize.rs index f81afb9..59e873e 100755 --- a/src/pipeline/finalize.rs +++ b/src/pipeline/finalize.rs @@ -134,7 +134,7 @@ fn avif_default_quality(image: &VipsImage) -> i32 { let area = width * height / 1000000.0; // Dynamic AVIF quality based on image area, min. 40, max. 59 - let quality = (8.0 - area) * (59.0 - 40.0) / (8.0 - 0.25) + 40.0; + let quality = (8.0 - area).clamp(0.0, 8.0 - 0.25) * (59.0 - 40.0) / (8.0 - 0.25) + 40.0; debug!("Serving image with quality: {}%, {area}MPix", quality as i32); quality as i32 @@ -148,7 +148,7 @@ fn webp_default_quality(image: &VipsImage) -> i32 { let area = width * height / 1000000.0; // Dynamic WebP quality based on image area, min. 16, max. 78 - let quality = (8.0 - area) * (78.0 - 16.0) / (8.0 - 0.25) + 16.0; + let quality = (8.0 - area).clamp(0.0, 8.0 - 0.25) * (78.0 - 16.0) / (8.0 - 0.25) + 16.0; debug!("Serving image with quality: {}%, {area}MPix", quality as i32); quality as i32 @@ -162,7 +162,7 @@ fn jpg_default_quality(image: &VipsImage) -> i32 { let area = width * height / 1000000.0; // Dynamic JPEG quality based on image area, min. 40, max. 75 - let quality = (8.0 - area) * (75.0 - 40.0) / (8.0 - 0.25) + 40.0; + let quality = (8.0 - area).clamp(0.0, 8.0 - 0.25) * (75.0 - 40.0) / (8.0 - 0.25) + 40.0; debug!("Serving image with quality: {}%, {area}MPix", quality as i32); quality as i32 diff --git a/src/pipeline/mod.rs b/src/pipeline/mod.rs index 2d27fe3..701f42d 100755 --- a/src/pipeline/mod.rs +++ b/src/pipeline/mod.rs @@ -1,9 +1,10 @@ use std::path::PathBuf; + use log::debug; -use crate::cache; +use crate::cache; use crate::parameters::{Rotate, UrlParameters}; -use crate::services::formats::{is_svg, supports_transparency, OutputFormat}; +use crate::services::formats::{is_svg, OutputFormat, supports_transparency}; mod thumbnail; mod rotate; diff --git a/src/pipeline/thumbnail.rs b/src/pipeline/thumbnail.rs index adf5ae3..0489850 100755 --- a/src/pipeline/thumbnail.rs +++ b/src/pipeline/thumbnail.rs @@ -3,10 +3,10 @@ use std::path::{Path, PathBuf}; use std::process::Command; use libvips::{ops, VipsImage}; -use libvips::ops::{Intent, Interesting, ThumbnailOptions}; +use libvips::ops::{Access, Intent, Interesting, ThumbnailOptions}; + use crate::cache; use crate::cache::{get_document_path_from_url_parameters, index}; - use crate::parameters::UrlParameters; use crate::pipeline::{PipelineError, PipelineResult}; use crate::pipeline::resize::get_rasterize_dimensions; @@ -16,7 +16,7 @@ use crate::services::vips::get_error_message; pub(crate) async fn run(working_file: &Path, url_parameters: &UrlParameters<'_>) -> PipelineResult { if !is_thumbnail_format(url_parameters.path) { - return match VipsImage::new_from_file(&working_file.to_string_lossy()) { + return match VipsImage::new_from_file_access(&working_file.to_string_lossy(), Access::Sequential, true) { Ok(image) => Ok(image), Err(error) => return Err(PipelineError(format!("Failed to open image: {}", error))) }; @@ -37,10 +37,10 @@ pub(crate) async fn run(working_file: &Path, url_parameters: &UrlParameters<'_>) fn generate_pdf_thumbnail(working_file: &Path, url_parameters: &UrlParameters<'_>) -> PipelineResult { - let pdf = VipsImage::new_from_file(&working_file.to_string_lossy()).unwrap(); + let pdf = VipsImage::new_from_file_access(&working_file.to_string_lossy(), Access::Sequential, true).unwrap(); let page_parameter = format!("[page={}]", (url_parameters.thumbnail.page - 1).min(pdf.get_n_pages() as u32 - 1)); - let pdf = VipsImage::new_from_file(&(working_file.to_string_lossy() + &page_parameter[..])).unwrap(); + let pdf = VipsImage::new_from_file_access(&(working_file.to_string_lossy() + &page_parameter[..]), Access::Sequential, true).unwrap(); let (width, height) = get_rasterize_dimensions(&pdf, url_parameters); match ops::thumbnail_with_opts(&(working_file.to_string_lossy() + &page_parameter[..]), width, &ThumbnailOptions {