From a735b3234f2da36ef9c85581b94414a5acdbd16f 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: Fri, 12 Jan 2024 01:35:57 +0800 Subject: [PATCH] fix: prevent recursive caching - don't cache files in the cache directory (#504) --- yazi-plugin/preset/plugins/image.lua | 14 ++++++++------ yazi-plugin/preset/plugins/pdf.lua | 13 ++++++++----- yazi-plugin/preset/plugins/video.lua | 13 ++++++++----- yazi-plugin/src/utils/cache.rs | 22 +++++++++++++++------- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/yazi-plugin/preset/plugins/image.lua b/yazi-plugin/preset/plugins/image.lua index c787acd8..2ebfcb33 100644 --- a/yazi-plugin/preset/plugins/image.lua +++ b/yazi-plugin/preset/plugins/image.lua @@ -1,18 +1,20 @@ local M = {} -function M:cache() return ya.cache_file(self.file.url .. tostring(self.file.cha.modified)) end - function M:peek() - local cache = self:cache() - ya.image_show(fs.symlink_metadata(cache) and cache or self.file.url, self.area) + local url = ya.file_cache(self) + if not url or not fs.symlink_metadata(url) then + url = self.file.url + end + + ya.image_show(url, self.area) ya.preview_widgets(self, {}) end function M:seek() end function M:preload() - local cache = self:cache() - if fs.symlink_metadata(cache) then + local cache = ya.file_cache(self) + if not cache or fs.symlink_metadata(cache) then return 1 end diff --git a/yazi-plugin/preset/plugins/pdf.lua b/yazi-plugin/preset/plugins/pdf.lua index c2ad4362..62fea107 100644 --- a/yazi-plugin/preset/plugins/pdf.lua +++ b/yazi-plugin/preset/plugins/pdf.lua @@ -1,10 +1,13 @@ local M = {} -function M:cache() return ya.cache_file(self.file.url .. self.skip .. tostring(self.file.cha.modified)) end - function M:peek() + local cache = ya.file_cache(self) + if not cache then + return + end + if self:preload() == 1 then - ya.image_show(self:cache(), self.area) + ya.image_show(cache, self.area) ya.preview_widgets(self, {}) end end @@ -18,8 +21,8 @@ function M:seek(units) end function M:preload() - local cache = self:cache() - if fs.symlink_metadata(cache) then + local cache = ya.file_cache(self) + if not cache or fs.symlink_metadata(cache) then return 1 end diff --git a/yazi-plugin/preset/plugins/video.lua b/yazi-plugin/preset/plugins/video.lua index 0a5e61f8..04695a13 100644 --- a/yazi-plugin/preset/plugins/video.lua +++ b/yazi-plugin/preset/plugins/video.lua @@ -1,10 +1,13 @@ local M = {} -function M:cache() return ya.cache_file(self.file.url .. self.skip .. tostring(self.file.cha.modified)) end - function M:peek() + local cache = ya.file_cache(self) + if not cache then + return + end + if self:preload() == 1 then - ya.image_show(self:cache(), self.area) + ya.image_show(cache, self.area) ya.preview_widgets(self, {}) end end @@ -26,8 +29,8 @@ function M:preload() return 2 end - local cache = self:cache() - if fs.symlink_metadata(cache) then + local cache = ya.file_cache(self) + if not cache or fs.symlink_metadata(cache) then return 1 end diff --git a/yazi-plugin/src/utils/cache.rs b/yazi-plugin/src/utils/cache.rs index 85c962c6..94aa4aac 100644 --- a/yazi-plugin/src/utils/cache.rs +++ b/yazi-plugin/src/utils/cache.rs @@ -3,17 +3,25 @@ use mlua::{Lua, Table}; use yazi_config::PREVIEW; use super::Utils; -use crate::bindings::{Cast, Url}; +use crate::bindings::{Cast, FileRef, Url}; impl Utils { pub(super) fn cache(lua: &Lua, ya: &Table) -> mlua::Result<()> { ya.set( - "cache_file", - lua.create_function(|lua, data: mlua::String| { - Url::cast( - lua, - PREVIEW.cache_dir.join(format!("{:x}", Md5::new_with_prefix(data).finalize())), - ) + "file_cache", + lua.create_function(|lua, t: Table| { + let file: FileRef = t.get("file")?; + if file.url.parent() == Some(&PREVIEW.cache_dir) { + return Ok(None); + } + + let hex = { + let mut digest = Md5::new_with_prefix(file.url.as_os_str().as_encoded_bytes()); + digest.update(&format!("//{:?}//{}", file.cha.modified, t.get("skip").unwrap_or(0))); + format!("{:x}", digest.finalize()) + }; + + Some(Url::cast(lua, PREVIEW.cache_dir.join(hex))).transpose() })?, )?;