🎨 /资源 支持搜索未索引的文件 https://github.com/siyuan-note/siyuan/issues/5416

This commit is contained in:
Liang Ding 2022-07-15 09:25:02 +08:00
parent 6e02c3e930
commit 4a64f5592f
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
7 changed files with 96 additions and 33 deletions

View file

@ -37,6 +37,7 @@ import (
"github.com/gabriel-vasile/mimetype"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/search"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
@ -161,25 +162,32 @@ func NetImg2LocalAssets(rootID string) (err error) {
return
}
type Asset struct {
HName string `json:"hName"`
Name string `json:"name"`
Path string `json:"path"`
}
func SearchAssetsByName(keyword string) (ret []*cache.Asset) {
ret = []*cache.Asset{}
func SearchAssetsByName(keyword string) (ret []*Asset) {
ret = []*Asset{}
sqlAssets := sql.QueryAssetsByName(keyword)
for _, sqlAsset := range sqlAssets {
hName := util.RemoveID(sqlAsset.Name)
_, hName = search.MarkText(hName, keyword, 64, Conf.Search.CaseSensitive)
asset := &Asset{
HName: hName,
Name: sqlAsset.Name,
Path: sqlAsset.Path,
count := 0
cache.Assets.Range(func(k, v interface{}) bool {
asset := v.(*cache.Asset)
if !strings.Contains(strings.ToLower(asset.HName), strings.ToLower(keyword)) {
return true
}
ret = append(ret, asset)
}
_, hName := search.MarkText(asset.HName, keyword, 64, Conf.Search.CaseSensitive)
ret = append(ret, &cache.Asset{
HName: hName,
Path: asset.Path,
Updated: asset.Updated,
})
count++
if Conf.Search.Limit <= count {
return false
}
return true
})
sort.Slice(ret, func(i, j int) bool {
return ret[i].Updated > ret[j].Updated
})
return
}

View file

@ -23,6 +23,7 @@ import (
"time"
"github.com/fsnotify/fsnotify"
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -78,6 +79,9 @@ func watchAssets() {
// 外部修改已有资源文件后纳入云端同步 https://github.com/siyuan-note/siyuan/issues/4694
IncSync()
}
// 重新缓存资源文件,以便使用 /资源 搜索
cache.LoadAssets()
}
}
}()

View file

@ -23,6 +23,7 @@ import (
"time"
"github.com/radovskyb/watcher"
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -54,6 +55,9 @@ func watchAssets() {
if watcher.Write == event.Op {
IncSync()
}
// 重新缓存资源文件,以便使用 /资源 搜索
cache.LoadAssets()
case err, ok := <-assetsWatcher.Error:
if !ok {
return