Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Avoid reporting encrypted notebook assets as missing https://github.com/siyuan-note/siyuan/issues/18034

Daniel746490f1e920
diff --git a/kernel/model/assets.go b/kernel/model/assets.go
index 20fb0d2add..006551da44 100644
--- a/kernel/model/assets.go
+++ b/kernel/model/assets.go
@@ -1665,6 +1665,13 @@ type UnusedItem struct {
AbsPath string `json:"-"`
}
+type missingAssetReference struct {
+ boxID string
+ rawDest string
+ dest string
+ encrypted bool
+}
+
func UnusedAssets(sorted bool) (ret []*UnusedItem) {
defer logging.Recover()
ret = []*UnusedItem{}
@@ -1880,11 +1887,12 @@ func MissingAssets() (ret []*UnusedItem) {
return
}
luteEngine := util.NewLute()
- destBlockIDs := map[string]map[string]bool{}
+ referenceBlockIDs := map[missingAssetReference]map[string]bool{}
for _, notebook := range notebooks {
if notebook.Closed {
continue
}
+ encrypted := IsEncryptedBox(notebook.ID)
pages := pagedPaths(filepath.Join(util.DataDir, notebook.ID), 32)
for _, paths := range pages {
@@ -1904,7 +1912,7 @@ func MissingAssets() (ret []*UnusedItem) {
blockID := assetLinkDestBlockID(n)
for _, dest := range getAssetLinkDestsByNode(n, false) {
- addAssetLinkDestBlockID(destBlockIDs, dest, blockID)
+ addAssetLinkDestBlockID(referenceBlockIDs, notebook.ID, encrypted, dest, blockID)
}
return ast.WalkContinue
})
@@ -1914,28 +1922,39 @@ func MissingAssets() (ret []*UnusedItem) {
if !util.IsAssetLinkDest([]byte(titleImgPath), false) {
continue
}
- addAssetLinkDestBlockID(destBlockIDs, titleImgPath, tree.Root.ID)
+ addAssetLinkDestBlockID(referenceBlockIDs, notebook.ID, encrypted, titleImgPath, tree.Root.ID)
}
}
}
}
- for dest, blockIDSet := range destBlockIDs {
- if "" == assetsPathMap[dest] {
- if strings.HasPrefix(dest, "assets/.") {
- // Assets starting with `.` should not be considered missing assets https://github.com/siyuan-note/siyuan/issues/8821
- if filelock.IsExist(filepath.Join(util.DataDir, dest)) {
- continue
- }
- }
+ return missingAssetItems(referenceBlockIDs, assetsPathMap)
+}
- blockIDs := make([]string, 0, len(blockIDSet))
- for blockID := range blockIDSet {
- blockIDs = append(blockIDs, blockID)
- }
- sort.Strings(blockIDs)
- ret = append(ret, &UnusedItem{Item: dest, Name: path.Base(dest), BlockIDs: blockIDs})
+func missingAssetItems(referenceBlockIDs map[missingAssetReference]map[string]bool, assetsPathMap map[string]string) (ret []*UnusedItem) {
+ ret = []*UnusedItem{}
+ missingBlockIDs := map[string]map[string]bool{}
+ for reference, blockIDSet := range referenceBlockIDs {
+ if assetReferenceExists(reference, assetsPathMap) {
+ continue
+ }
+ destBlockIDs := missingBlockIDs[reference.dest]
+ if nil == destBlockIDs {
+ destBlockIDs = map[string]bool{}
+ missingBlockIDs[reference.dest] = destBlockIDs
+ }
+ for blockID := range blockIDSet {
+ destBlockIDs[blockID] = true
+ }
+ }
+
+ for dest, blockIDSet := range missingBlockIDs {
+ blockIDs := make([]string, 0, len(blockIDSet))
+ for blockID := range blockIDSet {
+ blockIDs = append(blockIDs, blockID)
}
+ sort.Strings(blockIDs)
+ ret = append(ret, &UnusedItem{Item: dest, Name: path.Base(dest), BlockIDs: blockIDs})
}
sort.Slice(ret, func(i, j int) bool {
return ret[i].Item < ret[j].Item
@@ -1943,16 +1962,37 @@ func MissingAssets() (ret []*UnusedItem) {
return
}
-func addAssetLinkDestBlockID(destBlockIDs map[string]map[string]bool, dest, blockID string) {
- dest = normalizeMissingAssetLinkDest(dest)
+func assetReferenceExists(reference missingAssetReference, assetsPathMap map[string]string) bool {
+ if reference.encrypted {
+ _, err := GetAssetAbsPathInBox(reference.rawDest, reference.boxID)
+ return nil == err
+ }
+
+ if _, _, err := assetPathAndBox(reference.rawDest, reference.boxID); nil != err {
+ return false
+ }
+ if "" != assetsPathMap[reference.dest] {
+ return true
+ }
+ if strings.HasPrefix(reference.dest, "assets/.") {
+ // Assets starting with `.` should not be considered missing assets https://github.com/siyuan-note/siyuan/issues/8821
+ return filelock.IsExist(filepath.Join(util.DataDir, reference.dest))
+ }
+ return false
+}
+
+func addAssetLinkDestBlockID(referenceBlockIDs map[missingAssetReference]map[string]bool, boxID string, encrypted bool, rawDest, blockID string) {
+ rawDest = strings.TrimSpace(rawDest)
+ dest := normalizeMissingAssetLinkDest(rawDest)
if "" == dest {
return
}
- blockIDs := destBlockIDs[dest]
+ reference := missingAssetReference{boxID: boxID, rawDest: rawDest, dest: dest, encrypted: encrypted}
+ blockIDs := referenceBlockIDs[reference]
if nil == blockIDs {
blockIDs = map[string]bool{}
- destBlockIDs[dest] = blockIDs
+ referenceBlockIDs[reference] = blockIDs
}
if "" != blockID {
blockIDs[blockID] = true
diff --git a/kernel/model/assets_test.go b/kernel/model/assets_test.go
index 2625d1ea68..2c3e47c824 100644
--- a/kernel/model/assets_test.go
+++ b/kernel/model/assets_test.go
@@ -222,6 +222,70 @@ func TestNormalizeMissingAssetLinkDest(t *testing.T) {
}
}
+func TestMissingAssetItemsAreScopedToEncryptedNotebook(t *testing.T) {
+ originalDataDir, originalWorkspaceDir := util.DataDir, util.WorkspaceDir
+ originalConf, originalLangs := Conf, util.Langs
+ t.Cleanup(func() {
+ util.DataDir, util.WorkspaceDir = originalDataDir, originalWorkspaceDir
+ Conf, util.Langs = originalConf, originalLangs
+ })
+
+ workspaceDir := t.TempDir()
+ util.WorkspaceDir = workspaceDir
+ util.DataDir = filepath.Join(workspaceDir, "data")
+ Conf = NewAppConf()
+ Conf.Lang = "en"
+ util.Langs = map[string]map[int]string{"en": {12: "Asset [%s] not found"}}
+ boxA := "20260728160000-abcdefg"
+ boxB := "20260728160001-hijklmn"
+ normalBox := "20260728160002-opqrstu"
+ for _, boxID := range []string{boxA, boxB} {
+ confDir := filepath.Join(util.DataDir, boxID, ".siyuan")
+ if err := os.MkdirAll(confDir, 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(confDir, "conf.json"), []byte(`{"encrypted":true}`), 0644); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ sharedDest := "assets/shared-20260728160003-vwxyzab.bin"
+ for _, assetPath := range []string{
+ filepath.Join(util.DataDir, boxA, filepath.FromSlash(sharedDest)),
+ filepath.Join(util.DataDir, boxA, "assets", "mismatch.bin"),
+ filepath.Join(util.DataDir, boxB, "assets", "mismatch.bin"),
+ } {
+ if err := os.MkdirAll(filepath.Dir(assetPath), 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(assetPath, []byte("asset"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ referenceBlockIDs := map[missingAssetReference]map[string]bool{}
+ addAssetLinkDestBlockID(referenceBlockIDs, boxA, true, sharedDest+"?box="+boxA, "20260728160100-aaaaaaa")
+ addAssetLinkDestBlockID(referenceBlockIDs, boxB, true, sharedDest+"?box="+boxB, "20260728160101-bbbbbbb")
+ addAssetLinkDestBlockID(referenceBlockIDs, boxA, true, "assets/mismatch.bin?box="+boxB, "20260728160102-ccccccc")
+ addAssetLinkDestBlockID(referenceBlockIDs, normalBox, false, "assets/global.pdf?page=2", "20260728160103-ddddddd")
+ addAssetLinkDestBlockID(referenceBlockIDs, normalBox, false, "assets/normal-missing.txt", "20260728160104-eeeeeee")
+
+ items := missingAssetItems(referenceBlockIDs, map[string]string{"assets/global.pdf": "global.pdf"})
+ expected := []*UnusedItem{
+ {Item: "assets/mismatch.bin", Name: "mismatch.bin", BlockIDs: []string{"20260728160102-ccccccc"}},
+ {Item: "assets/normal-missing.txt", Name: "normal-missing.txt", BlockIDs: []string{"20260728160104-eeeeeee"}},
+ {Item: sharedDest, Name: path.Base(sharedDest), BlockIDs: []string{"20260728160101-bbbbbbb"}},
+ }
+ if !reflect.DeepEqual(items, expected) {
+ t.Fatalf("unexpected missing assets: got %#v, want %#v", items, expected)
+ }
+
+ items = missingAssetItems(map[missingAssetReference]map[string]bool{}, map[string]string{})
+ if nil == items || 0 != len(items) {
+ t.Fatalf("empty missing assets should be a non-nil empty slice: %#v", items)
+ }
+}
+
func TestGetAssetAbsPathWithSymlinkedWorkspaceAncestor(t *testing.T) {
originalDataDir, originalWorkspaceDir := util.DataDir, util.WorkspaceDir
t.Cleanup(func() {