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/util"
30)
31
32var emojisWatcher *fsnotify.Watcher
33
34func WatchEmojis() {
35 if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container || util.ContainerHarmony == util.Container {
36 return
37 }
38
39 go func() {
40 watchEmojis()
41 }()
42}
43
44func watchEmojis() {
45 emojisDir := filepath.Join(util.DataDir, "emojis")
46 if nil != emojisWatcher {
47 emojisWatcher.Close()
48 }
49
50 var err error
51 if emojisWatcher, err = fsnotify.NewWatcher(); err != nil {
52 logging.LogErrorf("add emojis watcher for folder [%s] failed: %s", emojisDir, err)
53 return
54 }
55
56 go func() {
57 defer logging.Recover()
58
59 timer := time.NewTimer(100 * time.Millisecond)
60 <-timer.C // timer should be expired at first
61
62 for {
63 select {
64 case _, ok := <-emojisWatcher.Events:
65 if !ok {
66 return
67 }
68
69 timer.Reset(time.Millisecond * 100)
70 case err, ok := <-emojisWatcher.Errors:
71 if !ok {
72 return
73 }
74 logging.LogErrorf("watch emojis failed: %s", err)
75 case <-timer.C:
76 util.PushReloadEmojiConf()
77 }
78 }
79 }()
80
81 if !gulu.File.IsDir(emojisDir) {
82 os.MkdirAll(emojisDir, 0755)
83 }
84
85 if err = emojisWatcher.Add(emojisDir); err != nil {
86 logging.LogErrorf("add emojis watcher for folder [%s] failed: %s", emojisDir, err)
87 }
88 //logging.LogInfof("added file watcher [%s]", emojisDir)
89}
90
91func CloseWatchEmojis() {
92 if nil != emojisWatcher {
93 emojisWatcher.Close()
94 }
95}