A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
at lambda-fork/main 95 lines 2.3 kB view raw
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 "path/filepath" 23 "time" 24 25 "github.com/radovskyb/watcher" 26 "github.com/siyuan-note/logging" 27 "github.com/siyuan-note/siyuan/kernel/cache" 28 "github.com/siyuan-note/siyuan/kernel/util" 29) 30 31var assetsWatcher *watcher.Watcher 32 33func WatchAssets() { 34 go func() { 35 watchAssets() 36 }() 37} 38 39func watchAssets() { 40 if nil != assetsWatcher { 41 assetsWatcher.Close() 42 } 43 assetsWatcher = watcher.New() 44 45 assetsDir := filepath.Join(util.DataDir, "assets") 46 47 go func() { 48 for { 49 select { 50 case event, ok := <-assetsWatcher.Event: 51 if !ok { 52 return 53 } 54 55 //logging.LogInfof("assets changed: %s", event) 56 if watcher.Write == event.Op { 57 IncSync() 58 } 59 60 // 重新缓存资源文件,以便使用 /资源 搜索 61 go cache.LoadAssets() 62 63 if watcher.Remove == event.Op { 64 HandleAssetsRemoveEvent(event.Path) 65 } else { 66 HandleAssetsChangeEvent(event.Path) 67 } 68 case err, ok := <-assetsWatcher.Error: 69 if !ok { 70 return 71 } 72 logging.LogErrorf("watch assets failed: %s", err) 73 case <-assetsWatcher.Closed: 74 return 75 } 76 } 77 }() 78 79 if err := assetsWatcher.Add(assetsDir); err != nil { 80 logging.LogErrorf("add assets watcher for folder [%s] failed: %s", assetsDir, err) 81 return 82 } 83 84 //logging.LogInfof("added file watcher [%s]", assetsDir) 85 if err := assetsWatcher.Start(10 * time.Second); err != nil { 86 logging.LogErrorf("start assets watcher for folder [%s] failed: %s", assetsDir, err) 87 return 88 } 89} 90 91func CloseWatchAssets() { 92 if nil != assetsWatcher { 93 assetsWatcher.Close() 94 } 95}