An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Invalidate thumbnails when image assets change https://github.com/siyuan-note/siyuan/issues/19424
Danielae3808743773
diff --git a/kernel/model/assets.go b/kernel/model/assets.goindex 1f0429096a..110aa039c9 100644--- a/kernel/model/assets.go+++ b/kernel/model/assets.go@@ -235,14 +235,18 @@ func HandleAssetsChangeEvent(assetAbsPath string) {}func removeAssetThumbnail(assetAbsPath string) {- if util.IsCompressibleAssetImage(assetAbsPath) {- p := filepath.ToSlash(assetAbsPath)- _, after, found := strings.Cut(p, "assets/")- if !found {- return- }- thumbnailPath := filepath.Join(util.TempDir, "thumbnails", "assets", after)- os.RemoveAll(thumbnailPath)+ relativePath, err := filepath.Rel(util.DataDir, assetAbsPath)+ if err != nil {+ return+ }+ // 按数据目录内的资源路径匹配缩略图缓存,保留资源子目录和文件名大小写。+ assetPath, _, ok := AssetPathFromDataRelativePath(filepath.ToSlash(relativePath))+ if !ok || !util.IsCompressibleAssetImage(assetPath) {+ return+ }+ thumbnailPath := filepath.Join(util.TempDir, "thumbnails", assetPath)+ if err = os.Remove(thumbnailPath); err != nil && !os.IsNotExist(err) {+ logging.LogErrorf("remove asset thumbnail [%s] failed: %s", thumbnailPath, err)}}diff --git a/kernel/model/assets_test.go b/kernel/model/assets_test.goindex 2f442b9409..1498ff4b5d 100644--- a/kernel/model/assets_test.go+++ b/kernel/model/assets_test.go@@ -39,6 +39,51 @@ import ("github.com/siyuan-note/siyuan/kernel/util")+func TestRemoveAssetThumbnail(t *testing.T) {+ originalDataDir, originalTempDir := util.DataDir, util.TempDir+ t.Cleanup(func() { util.DataDir, util.TempDir = originalDataDir, originalTempDir })+ root := filepath.Join(t.TempDir(), "assets", "workspace")+ util.DataDir = filepath.Join(root, "data")+ util.TempDir = filepath.Join(root, "temp")+ for _, test := range []struct {+ name string+ source string+ thumbnail string+ remove bool+ }{+ {"global", "assets/photo.png", "assets/photo.png", true},+ {"uppercase", "assets/Photo.JPG", "assets/Photo.JPG", true},+ {"nested", "assets/nested/assets/photo.jpeg", "assets/nested/assets/photo.jpeg", true},+ {"notebook", "20260101120000-abc1234/assets/photo.png", "assets/photo.png", true},+ {"document", "20260101120000-abc1234/20260101120001-def5678/assets/photo.png", "assets/photo.png", true},+ {"unsupported", "assets/photo.txt", "assets/photo.txt", false},+ {"outside", "../assets/photo.png", "assets/photo.png", false},+ {"nonasset", "storage/assets/photo.png", "assets/photo.png", false},+ {"similar directory", "otherassets/photo.png", "assets/photo.png", false},+ } {+ t.Run(test.name, func(t *testing.T) {+ thumbnailPath := filepath.Join(util.TempDir, "thumbnails", filepath.FromSlash(test.thumbnail))+ if err := os.MkdirAll(filepath.Dir(thumbnailPath), 0755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(thumbnailPath, []byte("cached image"), 0644); err != nil {+ t.Fatal(err)+ }+ assetAbsPath := filepath.Join(util.DataDir, filepath.FromSlash(test.source))+ removeAssetThumbnail(assetAbsPath)+ _, err := os.Stat(thumbnailPath)+ if test.remove {+ if !os.IsNotExist(err) {+ t.Fatalf("thumbnail should be removed: %v", err)+ }+ removeAssetThumbnail(assetAbsPath)+ } else if err != nil {+ t.Fatalf("unrelated thumbnail should be preserved: %v", err)+ }+ })+ }+}+func TestGenerateImageDoesNotRequireDocument(t *testing.T) {var source bytes.Bufferif err := png.Encode(&source, image.NewRGBA(image.Rect(0, 0, 2, 2))); err != nil {