From 2d61568fadf47adac80f318d939a99d43c82c0b1 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sun, 20 Aug 2023 11:22:48 +0800 Subject: [PATCH] :art: Remove asset content indexes when deleting asset files https://github.com/siyuan-note/siyuan/issues/9010 --- kernel/model/asset_content.go | 9 ++++++++- kernel/model/assets_watcher.go | 10 +++++++--- kernel/model/assets_watcher_darwin.go | 7 +++++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/kernel/model/asset_content.go b/kernel/model/asset_content.go index ebfcbde4c..9ee5f9a08 100644 --- a/kernel/model/asset_content.go +++ b/kernel/model/asset_content.go @@ -286,10 +286,16 @@ func buildAssetContentOrderBy(orderBy int) string { var assetContentSearcher = NewAssetsSearcher() -func IndexAssetContent(absPath string) { +func RemoveIndexAssetContent(absPath string) { defer logging.Recover() assetsDir := util.GetDataAssetsAbsPath() + p := "assets" + filepath.ToSlash(strings.TrimPrefix(absPath, assetsDir)) + sql.DeleteAssetContentsByPathQueue(p) +} + +func IndexAssetContent(absPath string) { + defer logging.Recover() ext := filepath.Ext(absPath) parser := assetContentSearcher.GetParser(ext) @@ -308,6 +314,7 @@ func IndexAssetContent(absPath string) { return } + assetsDir := util.GetDataAssetsAbsPath() p := "assets" + filepath.ToSlash(strings.TrimPrefix(absPath, assetsDir)) assetContents := []*sql.AssetContent{ diff --git a/kernel/model/assets_watcher.go b/kernel/model/assets_watcher.go index 18771a62d..eeb69a5df 100644 --- a/kernel/model/assets_watcher.go +++ b/kernel/model/assets_watcher.go @@ -75,8 +75,9 @@ func watchAssets() { timer.Reset(time.Millisecond * 100) if lastEvent.Op&fsnotify.Rename == fsnotify.Rename { - // 索引资源文件内容 IndexAssetContent(lastEvent.Name) + } else if lastEvent.Op&fsnotify.Remove == fsnotify.Remove { + RemoveIndexAssetContent(lastEvent.Name) } case err, ok := <-assetsWatcher.Errors: if !ok { @@ -93,8 +94,11 @@ func watchAssets() { // 重新缓存资源文件,以便使用 /资源 搜索 go cache.LoadAssets() - // 索引资源文件内容 - IndexAssetContent(lastEvent.Name) + if lastEvent.Op&fsnotify.Remove == fsnotify.Remove { + RemoveIndexAssetContent(lastEvent.Name) + } else { + IndexAssetContent(lastEvent.Name) + } } } }() diff --git a/kernel/model/assets_watcher_darwin.go b/kernel/model/assets_watcher_darwin.go index 8169f3a1f..35d926895 100644 --- a/kernel/model/assets_watcher_darwin.go +++ b/kernel/model/assets_watcher_darwin.go @@ -60,8 +60,11 @@ func watchAssets() { // 重新缓存资源文件,以便使用 /资源 搜索 go cache.LoadAssets() - // 索引资源文件内容 - IndexAssetContent(event.Path) + if watcher.Remove == event.Op { + RemoveIndexAssetContent(event.Path) + } else { + IndexAssetContent(event.Path) + } case err, ok := <-assetsWatcher.Error: if !ok { return