assets_watcher.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //go:build !darwin
  17. package model
  18. import (
  19. "os"
  20. "path/filepath"
  21. "time"
  22. "github.com/88250/gulu"
  23. "github.com/fsnotify/fsnotify"
  24. "github.com/siyuan-note/logging"
  25. "github.com/siyuan-note/siyuan/kernel/cache"
  26. "github.com/siyuan-note/siyuan/kernel/util"
  27. )
  28. var assetsWatcher *fsnotify.Watcher
  29. func WatchAssets() {
  30. if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container {
  31. return
  32. }
  33. go func() {
  34. watchAssets()
  35. }()
  36. }
  37. func watchAssets() {
  38. assetsDir := filepath.Join(util.DataDir, "assets")
  39. if nil != assetsWatcher {
  40. assetsWatcher.Close()
  41. }
  42. var err error
  43. if assetsWatcher, err = fsnotify.NewWatcher(); nil != err {
  44. logging.LogErrorf("add assets watcher for folder [%s] failed: %s", assetsDir, err)
  45. return
  46. }
  47. go func() {
  48. defer logging.Recover()
  49. var (
  50. timer *time.Timer
  51. lastEvent fsnotify.Event
  52. )
  53. timer = time.NewTimer(100 * time.Millisecond)
  54. <-timer.C // timer should be expired at first
  55. for {
  56. select {
  57. case event, ok := <-assetsWatcher.Events:
  58. if !ok {
  59. return
  60. }
  61. lastEvent = event
  62. timer.Reset(time.Millisecond * 100)
  63. if lastEvent.Op&fsnotify.Rename == fsnotify.Rename || lastEvent.Op&fsnotify.Write == fsnotify.Write {
  64. IndexAssetContent(lastEvent.Name)
  65. } else if lastEvent.Op&fsnotify.Remove == fsnotify.Remove {
  66. RemoveIndexAssetContent(lastEvent.Name)
  67. }
  68. case err, ok := <-assetsWatcher.Errors:
  69. if !ok {
  70. return
  71. }
  72. logging.LogErrorf("watch assets failed: %s", err)
  73. case <-timer.C:
  74. //logging.LogInfof("assets changed: %s", lastEvent)
  75. if lastEvent.Op&fsnotify.Write == fsnotify.Write {
  76. IncSync()
  77. }
  78. // 重新缓存资源文件,以便使用 /资源 搜索
  79. go cache.LoadAssets()
  80. if lastEvent.Op&fsnotify.Remove == fsnotify.Remove {
  81. RemoveIndexAssetContent(lastEvent.Name)
  82. } else {
  83. IndexAssetContent(lastEvent.Name)
  84. }
  85. }
  86. }
  87. }()
  88. if !gulu.File.IsDir(assetsDir) {
  89. os.MkdirAll(assetsDir, 0755)
  90. }
  91. if err = assetsWatcher.Add(assetsDir); err != nil {
  92. logging.LogErrorf("add assets watcher for folder [%s] failed: %s", assetsDir, err)
  93. }
  94. //logging.LogInfof("added file watcher [%s]", assetsDir)
  95. }
  96. func CloseWatchAssets() {
  97. if nil != assetsWatcher {
  98. assetsWatcher.Close()
  99. }
  100. }