diff --git a/yazi-adaptor/src/image.rs b/yazi-adaptor/src/image.rs index 4f290a73..05ada492 100644 --- a/yazi-adaptor/src/image.rs +++ b/yazi-adaptor/src/image.rs @@ -1,8 +1,7 @@ -use std::{path::Path, sync::Arc}; +use std::path::{Path, PathBuf}; use anyhow::Result; use image::{imageops::FilterType, DynamicImage, ImageFormat}; -use tokio::fs; use yazi_config::PREVIEW; use yazi_shared::Term; @@ -17,32 +16,51 @@ impl Image { }) .unwrap_or((PREVIEW.max_width, PREVIEW.max_height)); - let img = fs::read(path).await?; - let img = tokio::task::spawn_blocking(move || -> Result { - let img = image::load_from_memory(&img)?; + let path = path.to_owned(); + let img = tokio::task::spawn_blocking(move || { + image::io::Reader::open(path)?.with_guessed_format()?.decode() + }) + .await??; + + tokio::task::spawn_blocking(move || { Ok(if img.width() > w || img.height() > h { img.resize(w, h, FilterType::Triangle) } else { img }) - }); - - img.await? + }) + .await? } - 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); + pub async fn precache(path: &Path, cache: PathBuf) -> Result<()> { + let path = path.to_owned(); + let img = tokio::task::spawn_blocking(move || { + image::io::Reader::open(path)?.with_guessed_format()?.decode() + }) + .await??; + tokio::task::spawn_blocking(move || { + let (w, h) = (PREVIEW.max_width, PREVIEW.max_height); Ok(match img.resize(w, h, FilterType::Triangle) { DynamicImage::ImageRgb8(buf) => buf.save_with_format(cache, ImageFormat::Jpeg), DynamicImage::ImageRgba8(buf) => buf.save_with_format(cache, ImageFormat::Jpeg), - buf => buf.to_rgb8().save_with_format(cache, ImageFormat::Jpeg), + buf => buf.into_rgb8().save_with_format(cache, ImageFormat::Jpeg), }?) - }); + }) + .await? + } - result.await? + pub async fn precache_bin(bin: Vec, cache: PathBuf) -> Result<()> { + let img = tokio::task::spawn_blocking(move || image::load_from_memory(&bin)).await??; + + tokio::task::spawn_blocking(move || { + let (w, h) = (PREVIEW.max_width, PREVIEW.max_height); + Ok(match img.resize(w, h, FilterType::Triangle) { + DynamicImage::ImageRgb8(buf) => buf.save_with_format(cache, ImageFormat::Jpeg), + DynamicImage::ImageRgba8(buf) => buf.save_with_format(cache, ImageFormat::Jpeg), + buf => buf.into_rgb8().save_with_format(cache, ImageFormat::Jpeg), + }?) + }) + .await? } } diff --git a/yazi-core/src/external/pdftoppm.rs b/yazi-core/src/external/pdftoppm.rs index 8ea0f383..77f71ba7 100644 --- a/yazi-core/src/external/pdftoppm.rs +++ b/yazi-core/src/external/pdftoppm.rs @@ -1,11 +1,11 @@ -use std::{path::Path, sync::Arc}; +use std::path::Path; use regex::Regex; use tokio::process::Command; use yazi_adaptor::Image; use yazi_shared::PeekError; -pub async fn pdftoppm(src: &Path, dest: impl AsRef, skip: usize) -> Result<(), PeekError> { +pub async fn pdftoppm(src: &Path, dest: &Path, skip: usize) -> Result<(), PeekError> { let output = Command::new("pdftoppm") .args(["-singlefile", "-jpeg", "-jpegopt", "quality=75", "-f"]) .arg((skip + 1).to_string()) @@ -25,5 +25,5 @@ pub async fn pdftoppm(src: &Path, dest: impl AsRef, skip: usize) -> Result return if pages > 0 { Err(PeekError::Exceed(pages - 1)) } else { Err(s.to_string().into()) }; } - Ok(Image::precache(Arc::new(output.stdout), dest).await?) + Ok(Image::precache_bin(output.stdout, dest.to_owned()).await?) } diff --git a/yazi-core/src/tasks/workers/precache.rs b/yazi-core/src/tasks/workers/precache.rs index 204dc8f2..abe6fe71 100644 --- a/yazi-core/src/tasks/workers/precache.rs +++ b/yazi-core/src/tasks/workers/precache.rs @@ -78,9 +78,7 @@ impl Precache { if fs::symlink_metadata(&cache).await.is_ok() { return Ok(self.sch.send(TaskOp::Adv(task.id, 1, 0))?); } - if let Ok(img) = fs::read(&task.target).await { - Image::precache(Arc::new(img), cache).await.ok(); - } + Image::precache(&task.target, cache).await.ok(); self.sch.send(TaskOp::Adv(task.id, 1, 0))?; } PrecacheOp::Video(task) => {