fix: does not generate the necessary PDF cache when max_width is set too large (#28)

This commit is contained in:
三咲雅 · Misaki Masa 2023-08-07 13:41:21 +08:00 committed by GitHub
parent e52547de10
commit 575b7d7090
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 10 deletions

View file

@ -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<u8>, cache: PathBuf) -> Result<()> {
pub async fn precache(img: Arc<Vec<u8>>, cache: impl AsRef<Path>) -> Result<bool> {
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<Vec<u8>>, cache: impl AsRef<Path>) -> 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())

View file

@ -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<Path>) -> 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?)
}

View file

@ -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))?;
}

View file

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