A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
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
17//go:build !darwin
18
19package model
20
21import (
22 "os"
23 "path/filepath"
24 "time"
25
26 "github.com/88250/gulu"
27 "github.com/fsnotify/fsnotify"
28 "github.com/siyuan-note/logging"
29 "github.com/siyuan-note/siyuan/kernel/cache"
30 "github.com/siyuan-note/siyuan/kernel/util"
31)
32
33var assetsWatcher *fsnotify.Watcher
34
35func WatchAssets() {
36 if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container || util.ContainerHarmony == util.Container {
37 return
38 }
39
40 go func() {
41 watchAssets()
42 }()
43}
44
45func watchAssets() {
46 assetsDir := filepath.Join(util.DataDir, "assets")
47 if nil != assetsWatcher {
48 assetsWatcher.Close()
49 }
50
51 var err error
52 if assetsWatcher, err = fsnotify.NewWatcher(); err != nil {
53 logging.LogErrorf("add assets watcher for folder [%s] failed: %s", assetsDir, err)
54 return
55 }
56
57 go func() {
58 defer logging.Recover()
59
60 var (
61 timer *time.Timer
62 lastEvent fsnotify.Event
63 )
64 timer = time.NewTimer(100 * time.Millisecond)
65 <-timer.C // timer should be expired at first
66
67 for {
68 select {
69 case event, ok := <-assetsWatcher.Events:
70 if !ok {
71 return
72 }
73
74 lastEvent = event
75 timer.Reset(time.Millisecond * 100)
76
77 if lastEvent.Op&fsnotify.Rename == fsnotify.Rename || lastEvent.Op&fsnotify.Write == fsnotify.Write {
78 HandleAssetsChangeEvent(lastEvent.Name)
79 } else if lastEvent.Op&fsnotify.Remove == fsnotify.Remove {
80 HandleAssetsRemoveEvent(lastEvent.Name)
81 }
82 case err, ok := <-assetsWatcher.Errors:
83 if !ok {
84 return
85 }
86 logging.LogErrorf("watch assets failed: %s", err)
87 case <-timer.C:
88 //logging.LogInfof("assets changed: %s", lastEvent)
89 if lastEvent.Op&fsnotify.Write == fsnotify.Write {
90 IncSync()
91 }
92
93 // 重新缓存资源文件,以便使用 /资源 搜索
94 go cache.LoadAssets()
95
96 if lastEvent.Op&fsnotify.Remove == fsnotify.Remove {
97 HandleAssetsRemoveEvent(lastEvent.Name)
98 } else {
99 HandleAssetsChangeEvent(lastEvent.Name)
100 }
101 }
102 }
103 }()
104
105 if !gulu.File.IsDir(assetsDir) {
106 os.MkdirAll(assetsDir, 0755)
107 }
108
109 if err = assetsWatcher.Add(assetsDir); err != nil {
110 logging.LogErrorf("add assets watcher for folder [%s] failed: %s", assetsDir, err)
111 }
112 //logging.LogInfof("added file watcher [%s]", assetsDir)
113}
114
115func CloseWatchAssets() {
116 if nil != assetsWatcher {
117 assetsWatcher.Close()
118 }
119}