瀏覽代碼

Merge remote-tracking branch 'origin/dev' into dev

Vanessa 1 年之前
父節點
當前提交
e42d6abde7
共有 3 個文件被更改,包括 21 次插入8 次删除
  1. 8 1
      kernel/model/asset_content.go
  2. 8 5
      kernel/model/assets_watcher.go
  3. 5 2
      kernel/model/assets_watcher_darwin.go

+ 8 - 1
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{

+ 8 - 5
kernel/model/assets_watcher.go

@@ -74,9 +74,10 @@ func watchAssets() {
 				lastEvent = event
 				timer.Reset(time.Millisecond * 100)
 
-				if lastEvent.Op&fsnotify.Rename == fsnotify.Rename {
-					// 索引资源文件内容
+				if lastEvent.Op&fsnotify.Rename == fsnotify.Rename || lastEvent.Op&fsnotify.Write == fsnotify.Write {
 					IndexAssetContent(lastEvent.Name)
+				} else if lastEvent.Op&fsnotify.Remove == fsnotify.Remove {
+					RemoveIndexAssetContent(lastEvent.Name)
 				}
 			case err, ok := <-assetsWatcher.Errors:
 				if !ok {
@@ -86,15 +87,17 @@ func watchAssets() {
 			case <-timer.C:
 				//logging.LogInfof("assets changed: %s", lastEvent)
 				if lastEvent.Op&fsnotify.Write == fsnotify.Write {
-					// 外部修改已有资源文件后纳入云端同步 https://github.com/siyuan-note/siyuan/issues/4694
 					IncSync()
 				}
 
 				// 重新缓存资源文件,以便使用 /资源 搜索
 				go cache.LoadAssets()
 
-				// 索引资源文件内容
-				IndexAssetContent(lastEvent.Name)
+				if lastEvent.Op&fsnotify.Remove == fsnotify.Remove {
+					RemoveIndexAssetContent(lastEvent.Name)
+				} else {
+					IndexAssetContent(lastEvent.Name)
+				}
 			}
 		}
 	}()

+ 5 - 2
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