mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
..
This commit is contained in:
parent
36bad9c4ed
commit
dc371b782c
2 changed files with 34 additions and 38 deletions
|
|
@ -15,7 +15,7 @@ image = "^0"
|
||||||
md-5 = "^0"
|
md-5 = "^0"
|
||||||
once_cell = "^1"
|
once_cell = "^1"
|
||||||
ratatui = "^0"
|
ratatui = "^0"
|
||||||
tokio = { version = "^1", features = ["parking_lot"] }
|
tokio = { version = "^1", features = [ "parking_lot" ] }
|
||||||
|
|
||||||
# Logging
|
# Logging
|
||||||
tracing = "^0"
|
tracing = "^0"
|
||||||
|
|
|
||||||
|
|
@ -10,47 +10,43 @@ use tokio::fs;
|
||||||
pub struct Image;
|
pub struct Image;
|
||||||
|
|
||||||
impl Image {
|
impl Image {
|
||||||
pub(super) async fn crop(path: &Path, size: (u16, u16)) -> Result<DynamicImage> {
|
pub(super) async fn crop(path: &Path, size: (u16, u16)) -> Result<DynamicImage> {
|
||||||
let (w, h) = {
|
let (w, h) = {
|
||||||
let r = tty_ratio();
|
let r = tty_ratio();
|
||||||
let (w, h) = ((size.0 as f64 * r.0) as u32, (size.1 as f64 * r.1) as u32);
|
let (w, h) = ((size.0 as f64 * r.0) as u32, (size.1 as f64 * r.1) as u32);
|
||||||
(w.min(PREVIEW.max_width), h.min(PREVIEW.max_height))
|
(w.min(PREVIEW.max_width), h.min(PREVIEW.max_height))
|
||||||
};
|
};
|
||||||
|
|
||||||
let img = fs::read(path).await?;
|
let img = fs::read(path).await?;
|
||||||
let img = tokio::task::spawn_blocking(move || -> Result<DynamicImage> {
|
let img = tokio::task::spawn_blocking(move || -> Result<DynamicImage> {
|
||||||
let img = image::load_from_memory(&img)?;
|
let img = image::load_from_memory(&img)?;
|
||||||
Ok(if img.width() > w || img.height() > h {
|
Ok(if img.width() > w || img.height() > h {
|
||||||
img.resize(w, h, FilterType::Triangle)
|
img.resize(w, h, FilterType::Triangle)
|
||||||
} else {
|
} else {
|
||||||
img
|
img
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
img.await?
|
img.await?
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn precache(img: Vec<u8>, cache: PathBuf) -> Result<()> {
|
pub async fn precache(img: Vec<u8>, cache: PathBuf) -> Result<()> {
|
||||||
let result = tokio::task::spawn_blocking(move || {
|
let result = tokio::task::spawn_blocking(move || {
|
||||||
let img = image::load_from_memory(&img)?;
|
let img = image::load_from_memory(&img)?;
|
||||||
let (w, h) = (PREVIEW.max_width, PREVIEW.max_height);
|
let (w, h) = (PREVIEW.max_width, PREVIEW.max_height);
|
||||||
|
|
||||||
if img.width() > w || img.height() > h {
|
if img.width() > w || img.height() > h {
|
||||||
img.resize(w, h, FilterType::Triangle)
|
img.resize(w, h, FilterType::Triangle).save_with_format(cache, ImageFormat::Jpeg)?;
|
||||||
.save_with_format(cache, ImageFormat::Jpeg)?;
|
}
|
||||||
}
|
Ok::<(), Error>(())
|
||||||
Ok::<(), Error>(())
|
});
|
||||||
});
|
|
||||||
|
|
||||||
result.await?
|
result.await?
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn cache(path: &Path) -> PathBuf {
|
pub fn cache(path: &Path) -> PathBuf {
|
||||||
format!(
|
format!("/tmp/yazi/{:x}", Md5::new_with_prefix(path.to_string_lossy().as_bytes()).finalize())
|
||||||
"/tmp/yazi/{:x}",
|
.into()
|
||||||
Md5::new_with_prefix(path.to_string_lossy().as_bytes()).finalize()
|
}
|
||||||
)
|
|
||||||
.into()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue