From 575b7d7090bb748ec453f5ec8e16ac761b684388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Mon, 7 Aug 2023 13:41:21 +0800 Subject: [PATCH] fix: does not generate the necessary PDF cache when `max_width` is set too large (#28) --- adaptor/src/image.rs | 23 +++++++++++++++++------ core/src/external/pdftoppm.rs | 4 ++-- core/src/tasks/workers/precache.rs | 2 +- cspell.json | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/adaptor/src/image.rs b/adaptor/src/image.rs index 39bdc369..862fed2b 100644 --- a/adaptor/src/image.rs +++ b/adaptor/src/image.rs @@ -1,6 +1,6 @@ -use std::path::{Path, PathBuf}; +use std::{path::{Path, PathBuf}, sync::Arc}; -use anyhow::{Error, Result}; +use anyhow::Result; use config::PREVIEW; use image::{imageops::FilterType, DynamicImage, ImageFormat}; use md5::{Digest, Md5}; @@ -30,20 +30,31 @@ impl Image { img.await? } - pub async fn precache(img: Vec, cache: PathBuf) -> Result<()> { + pub async fn precache(img: Arc>, cache: impl AsRef) -> Result { + let cache = cache.as_ref().to_owned(); let result = tokio::task::spawn_blocking(move || { let img = image::load_from_memory(&img)?; let (w, h) = (PREVIEW.max_width, PREVIEW.max_height); - if img.width() > w || img.height() > h { - img.resize(w, h, FilterType::Triangle).save_with_format(cache, ImageFormat::Jpeg)?; + if img.width() <= w && img.height() <= h { + return Ok(false); } - Ok::<(), Error>(()) + + img.resize(w, h, FilterType::Triangle).save_with_format(cache, ImageFormat::Jpeg)?; + Ok(true) }); result.await? } + #[inline] + pub async fn precache_anyway(img: Arc>, cache: impl AsRef) -> Result<()> { + Ok(match Self::precache(img.clone(), &cache).await { + Ok(true) => (), + _ => fs::write(cache, &*img).await?, + }) + } + #[inline] pub fn cache(path: &Path) -> PathBuf { format!("/tmp/yazi/{:x}", Md5::new_with_prefix(path.to_string_lossy().as_bytes()).finalize()) diff --git a/core/src/external/pdftoppm.rs b/core/src/external/pdftoppm.rs index b9eb9de4..82c23fc3 100644 --- a/core/src/external/pdftoppm.rs +++ b/core/src/external/pdftoppm.rs @@ -4,7 +4,7 @@ use adaptor::Image; use anyhow::{bail, Result}; use tokio::process::Command; -pub async fn pdftoppm(src: &Path, dest: &Path) -> Result<()> { +pub async fn pdftoppm(src: &Path, dest: impl AsRef) -> Result<()> { let output = Command::new("pdftoppm") .args(["-singlefile", "-jpeg", "-jpegopt", "quality=75"]) .arg(src) @@ -15,5 +15,5 @@ pub async fn pdftoppm(src: &Path, dest: &Path) -> Result<()> { if !output.status.success() { bail!("failed to generate PDF thumbnail: {}", String::from_utf8_lossy(&output.stderr)); } - Ok(Image::precache(output.stdout, dest.to_path_buf()).await?) + Ok(Image::precache_anyway(output.stdout.into(), dest).await?) } diff --git a/core/src/tasks/workers/precache.rs b/core/src/tasks/workers/precache.rs index 744ef895..1b5097f7 100644 --- a/core/src/tasks/workers/precache.rs +++ b/core/src/tasks/workers/precache.rs @@ -78,7 +78,7 @@ impl Precache { return Ok(self.sch.send(TaskOp::Adv(task.id, 1, 0))?); } if let Ok(img) = fs::read(&task.target).await { - Image::precache(img, cache).await.ok(); + Image::precache(img.into(), cache).await.ok(); } self.sch.send(TaskOp::Adv(task.id, 1, 0))?; } diff --git a/cspell.json b/cspell.json index a0e3fba6..689c4b3c 100644 --- a/cspell.json +++ b/cspell.json @@ -1 +1 @@ -{"words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild"],"language":"en","version":"0.2","flagWords":[]} +{"language":"en","flagWords":[],"words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt"],"version":"0.2"}