Skip to content

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

:bug: Recognize directory assets without trailing slashes https://github.com/siyuan-note/siyuan/issues/18568

Danielf6059daa6243
diff --git a/kernel/model/assets.go b/kernel/model/assets.go
index aad01f8315..12c0705eef 100644
--- a/kernel/model/assets.go
+++ b/kernel/model/assets.go
@@ -1641,6 +1641,65 @@ type missingAssetReference struct {
encrypted bool
}
+func lookupAssetPath(assetsPathMap map[string]string, dest string) (resolvedDest, absPath string, found bool) {
+ if absPath, found = assetsPathMap[dest]; found {
+ return dest, absPath, true
+ }
+ if strings.HasSuffix(dest, "/") {
+ return "", "", false
+ }
+
+ resolvedDest = dest + "/"
+ absPath, found = assetsPathMap[resolvedDest]
+ if found {
+ return resolvedDest, absPath, true
+ }
+ return "", "", false
+}
+
+func removeReferencedAssetPaths(assetsPathMap map[string]string, dests map[string]bool) (linkDestFilePaths []string) {
+ var linkDestFolderPaths []string
+ for dest := range dests {
+ if !strings.HasPrefix(dest, "assets/") {
+ continue
+ }
+
+ if idx := strings.Index(dest, "?"); 0 < idx {
+ // `pdf?page` 资源文件链接会被判定为未引用资源 https://github.com/siyuan-note/siyuan/issues/5649
+ dest = dest[:idx]
+ }
+
+ resolvedDest, _, found := lookupAssetPath(assetsPathMap, dest)
+ if !found {
+ continue
+ }
+ if strings.HasSuffix(resolvedDest, "/") {
+ linkDestFolderPaths = append(linkDestFolderPaths, resolvedDest)
+ } else {
+ linkDestFilePaths = append(linkDestFilePaths, resolvedDest)
+ }
+ }
+
+ // 排除文件夹链接
+ var toRemoves []string
+ for asset := range assetsPathMap {
+ for _, linkDestFolder := range linkDestFolderPaths {
+ if strings.HasPrefix(asset, linkDestFolder) {
+ toRemoves = append(toRemoves, asset)
+ }
+ }
+ for _, linkDestPath := range linkDestFilePaths {
+ if strings.HasPrefix(linkDestPath, asset) {
+ toRemoves = append(toRemoves, asset)
+ }
+ }
+ }
+ for _, toRemove := range toRemoves {
+ delete(assetsPathMap, toRemove)
+ }
+ return
+}
+
func UnusedAssets(sorted bool) (ret []*UnusedItem) {
defer logging.Recover()
ret = []*UnusedItem{}
@@ -1694,44 +1753,7 @@ func UnusedAssets(sorted bool) (ret []*UnusedItem) {
}
}
- var linkDestFolderPaths, linkDestFilePaths []string
- for dest := range dests {
- if !strings.HasPrefix(dest, "assets/") {
- continue
- }
-
- if idx := strings.Index(dest, "?"); 0 < idx {
- // `pdf?page` 资源文件链接会被判定为未引用资源 https://github.com/siyuan-note/siyuan/issues/5649
- dest = dest[:idx]
- }
-
- if "" == assetsPathMap[dest] {
- continue
- }
- if strings.HasSuffix(dest, "/") {
- linkDestFolderPaths = append(linkDestFolderPaths, dest)
- } else {
- linkDestFilePaths = append(linkDestFilePaths, dest)
- }
- }
-
- // 排除文件夹链接
- var toRemoves []string
- for asset := range assetsPathMap {
- for _, linkDestFolder := range linkDestFolderPaths {
- if strings.HasPrefix(asset, linkDestFolder) {
- toRemoves = append(toRemoves, asset)
- }
- }
- for _, linkDestPath := range linkDestFilePaths {
- if strings.HasPrefix(linkDestPath, asset) {
- toRemoves = append(toRemoves, asset)
- }
- }
- }
- for _, toRemove := range toRemoves {
- delete(assetsPathMap, toRemove)
- }
+ linkDestFilePaths := removeReferencedAssetPaths(assetsPathMap, dests)
for _, dest := range linkDestFilePaths {
linkDestMap[dest] = true
@@ -1940,7 +1962,7 @@ func assetReferenceExists(reference missingAssetReference, assetsPathMap map[str
if _, _, err := assetPathAndBox(reference.rawDest, reference.boxID); nil != err {
return false
}
- if "" != assetsPathMap[reference.dest] {
+ if _, _, found := lookupAssetPath(assetsPathMap, reference.dest); found {
return true
}
if strings.HasPrefix(reference.dest, "assets/.") {
diff --git a/kernel/model/assets_test.go b/kernel/model/assets_test.go
index 2a51de204e..1701a9ff40 100644
--- a/kernel/model/assets_test.go
+++ b/kernel/model/assets_test.go
@@ -177,6 +177,71 @@ func TestNormalizeMissingAssetLinkDest(t *testing.T) {
}
}
+func TestLookupAssetPath(t *testing.T) {
+ assetsPathMap := map[string]string{
+ "assets/file": "file",
+ "assets/folder/": "folder",
+ "assets/shared": "shared-file",
+ "assets/shared/": "shared-folder",
+ }
+ tests := []struct {
+ name string
+ dest string
+ wantDest string
+ wantAbsPath string
+ wantFound bool
+ }{
+ {name: "file", dest: "assets/file", wantDest: "assets/file", wantAbsPath: "file", wantFound: true},
+ {name: "folder alias", dest: "assets/folder", wantDest: "assets/folder/", wantAbsPath: "folder", wantFound: true},
+ {name: "folder", dest: "assets/folder/", wantDest: "assets/folder/", wantAbsPath: "folder", wantFound: true},
+ {name: "exact file before folder alias", dest: "assets/shared", wantDest: "assets/shared", wantAbsPath: "shared-file", wantFound: true},
+ {name: "folder does not match file", dest: "assets/file/"},
+ {name: "missing", dest: "assets/missing"},
+ }
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ gotDest, gotAbsPath, gotFound := lookupAssetPath(assetsPathMap, test.dest)
+ if gotDest != test.wantDest || gotAbsPath != test.wantAbsPath || gotFound != test.wantFound {
+ t.Fatalf("lookup asset path: got [%q, %q, %v], want [%q, %q, %v]",
+ gotDest, gotAbsPath, gotFound, test.wantDest, test.wantAbsPath, test.wantFound)
+ }
+ })
+ }
+}
+
+func TestRemoveReferencedAssetPathsWithFolderAlias(t *testing.T) {
+ assetsPathMap := map[string]string{
+ "assets/ch4/": "ch4",
+ "assets/ch4/notes.md": "notes.md",
+ "assets/ch4/demo.js": "demo.js",
+ "assets/ch40/": "ch40",
+ }
+ linkDestFilePaths := removeReferencedAssetPaths(assetsPathMap, map[string]bool{"assets/ch4": true})
+ if 0 != len(linkDestFilePaths) {
+ t.Fatalf("folder alias should not be classified as a file: %v", linkDestFilePaths)
+ }
+ expected := map[string]string{"assets/ch40/": "ch40"}
+ if !reflect.DeepEqual(assetsPathMap, expected) {
+ t.Fatalf("unexpected assets after removing referenced folder: got %#v, want %#v", assetsPathMap, expected)
+ }
+}
+
+func TestMissingAssetItemsWithFolderAlias(t *testing.T) {
+ referenceBlockIDs := map[missingAssetReference]map[string]bool{}
+ addAssetLinkDestBlockID(referenceBlockIDs, "20260806120000-abcdefg", false, "assets/existing-folder", "20260806120001-hijklmn")
+ addAssetLinkDestBlockID(referenceBlockIDs, "20260806120000-abcdefg", false, "assets/missing-folder", "20260806120002-opqrstu")
+
+ items := missingAssetItems(referenceBlockIDs, map[string]string{"assets/existing-folder/": "existing-folder"})
+ expected := []*UnusedItem{{
+ Item: "assets/missing-folder",
+ Name: "missing-folder",
+ BlockIDs: []string{"20260806120002-opqrstu"},
+ }}
+ if !reflect.DeepEqual(items, expected) {
+ t.Fatalf("unexpected missing folder assets: got %#v, want %#v", items, expected)
+ }
+}
+
func TestMissingAssetItemsAreScopedToEncryptedNotebook(t *testing.T) {
originalDataDir, originalWorkspaceDir := util.DataDir, util.WorkspaceDir
originalConf, originalLangs := Conf, util.Langs
diff --git a/kernel/model/export.go b/kernel/model/export.go
index ede23c282f..56203b4026 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -2546,7 +2546,7 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string, i
srcPath, _ = GetAssetAbsPathInBox(asset, tree.Box)
}
if "" == srcPath {
- srcPath = assetPathMap[cleanAsset]
+ _, srcPath, _ = lookupAssetPath(assetPathMap, cleanAsset)
}
if "" == srcPath {
logging.LogWarnf("get asset [%s] abs path failed", asset)
@@ -2782,7 +2782,7 @@ func exportAv(avID, boxID, exportStorageAvDir, exportFolder string, assetPathMap
srcPath, _ = GetAssetAbsPathInBox(asset.Content, boxID)
}
if "" == srcPath {
- srcPath = assetPathMap[AssetPathWithoutQuery(asset.Content)]
+ _, srcPath, _ = lookupAssetPath(assetPathMap, AssetPathWithoutQuery(asset.Content))
}
if "" == srcPath {
logging.LogWarnf("get asset [%s] abs path failed", asset.Content)
@@ -4168,7 +4168,7 @@ func exportPandocConvertZip(boxID, baseFolderName string, docPaths, defBlockIDs
srcPath, _ = GetAssetAbsPathInBox(spaceDecodedOldAsset, treeBoxID)
}
if "" == srcPath {
- srcPath = assetsPathMap[AssetPathWithoutQuery(spaceDecodedOldAsset)]
+ _, srcPath, _ = lookupAssetPath(assetsPathMap, AssetPathWithoutQuery(spaceDecodedOldAsset))
}
if "" == srcPath {
logging.LogWarnf("get asset [%s] abs path failed", spaceDecodedOldAsset)