mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-20 16:40:13 +01:00
⚡ 优化清理未引用资源内存占用 https://github.com/siyuan-note/siyuan/issues/5200
This commit is contained in:
parent
f8b1bedfe5
commit
8589a53ee4
2 changed files with 43 additions and 23 deletions
|
|
@ -446,21 +446,32 @@ func UnusedAssets() (ret []string) {
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
luteEngine := NewLute()
|
||||||
for _, notebook := range notebooks {
|
for _, notebook := range notebooks {
|
||||||
notebookAbsPath := filepath.Join(util.DataDir, notebook.ID)
|
notebookAbsPath := filepath.Join(util.DataDir, notebook.ID)
|
||||||
trees := loadTrees(notebookAbsPath)
|
|
||||||
dests := map[string]bool{}
|
dests := map[string]bool{}
|
||||||
for _, tree := range trees {
|
pages := pagedPaths(notebookAbsPath, 20)
|
||||||
for _, d := range assetsLinkDestsInTree(tree) {
|
for _, paths := range pages {
|
||||||
dests[d] = true
|
var trees []*parse.Tree
|
||||||
}
|
for _, localPath := range paths {
|
||||||
|
tree, loadTreeErr := loadTree(localPath, luteEngine)
|
||||||
if titleImgPath := treenode.GetDocTitleImgPath(tree.Root); "" != titleImgPath {
|
if nil != loadTreeErr {
|
||||||
// 题头图计入
|
|
||||||
if !sql.IsAssetLinkDest([]byte(titleImgPath)) {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
dests[titleImgPath] = true
|
trees = append(trees, tree)
|
||||||
|
}
|
||||||
|
for _, tree := range trees {
|
||||||
|
for _, d := range assetsLinkDestsInTree(tree) {
|
||||||
|
dests[d] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if titleImgPath := treenode.GetDocTitleImgPath(tree.Root); "" != titleImgPath {
|
||||||
|
// 题头图计入
|
||||||
|
if !sql.IsAssetLinkDest([]byte(titleImgPath)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
dests[titleImgPath] = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/88250/lute"
|
||||||
"github.com/88250/lute/parse"
|
"github.com/88250/lute/parse"
|
||||||
"github.com/88250/protyle"
|
"github.com/88250/protyle"
|
||||||
"github.com/siyuan-note/filelock"
|
"github.com/siyuan-note/filelock"
|
||||||
|
|
@ -30,8 +31,9 @@ import (
|
||||||
"github.com/siyuan-note/siyuan/kernel/util"
|
"github.com/siyuan-note/siyuan/kernel/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadTrees(localPath string) (ret []*parse.Tree) {
|
func pagedPaths(localPath string, pageSize int) (ret map[int][]string) {
|
||||||
luteEngine := NewLute()
|
ret = map[int][]string{}
|
||||||
|
page := 1
|
||||||
filepath.Walk(localPath, func(path string, info fs.FileInfo, err error) error {
|
filepath.Walk(localPath, func(path string, info fs.FileInfo, err error) error {
|
||||||
if info.IsDir() && strings.HasPrefix(info.Name(), ".") {
|
if info.IsDir() && strings.HasPrefix(info.Name(), ".") {
|
||||||
return filepath.SkipDir
|
return filepath.SkipDir
|
||||||
|
|
@ -41,23 +43,30 @@ func loadTrees(localPath string) (ret []*parse.Tree) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := filelock.NoLockFileRead(path)
|
ret[page] = append(ret[page], path)
|
||||||
if nil != err {
|
if pageSize <= len(ret[page]) {
|
||||||
util.LogErrorf("get data [path=%s] failed: %s", path, err)
|
page++
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tree, err := protyle.ParseJSONWithoutFix(luteEngine, data)
|
|
||||||
if nil != err {
|
|
||||||
util.LogErrorf("parse json to tree [%s] failed: %s", path, err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
ret = append(ret, tree)
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadTree(localPath string, luteEngine *lute.Lute) (ret *parse.Tree, err error) {
|
||||||
|
data, err := filelock.NoLockFileRead(localPath)
|
||||||
|
if nil != err {
|
||||||
|
util.LogErrorf("get data [path=%s] failed: %s", localPath, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ret, err = protyle.ParseJSONWithoutFix(luteEngine, data)
|
||||||
|
if nil != err {
|
||||||
|
util.LogErrorf("parse json to tree [%s] failed: %s", localPath, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var ErrBoxNotFound = errors.New("notebook not found")
|
var ErrBoxNotFound = errors.New("notebook not found")
|
||||||
var ErrBlockNotFound = errors.New("block not found")
|
var ErrBlockNotFound = errors.New("block not found")
|
||||||
var ErrTreeNotFound = errors.New("tree not found")
|
var ErrTreeNotFound = errors.New("tree not found")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue