Skip to content

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

:bug: Reuse existing notebook asset directories https://github.com/siyuan-note/siyuan/issues/18657

Daniel7f736ca837cf
diff --git a/kernel/api/extension.go b/kernel/api/extension.go
index 766ec09266..9be93671f7 100644
--- a/kernel/api/extension.go
+++ b/kernel/api/extension.go
@@ -46,15 +46,19 @@ func extensionCopy(c *gin.Context) {
form, _ := c.MultipartForm()
dom := form.Value["dom"][0]
assets := filepath.Join(util.DataDir, "assets")
- boxID := ""
+ targetBoxID := ""
+ encryptedBoxID := ""
if notebookVal := form.Value["notebook"]; 0 < len(notebookVal) {
nb := notebookVal[0]
- if model.IsEncryptedBox(nb) {
- boxID = nb
- assets = filepath.Join(util.DataDir, nb, "assets")
+ if ast.IsNodeIDPattern(nb) {
+ targetBoxID = nb
+ assets = model.GetImportAssetsDir(targetBoxID, "")
+ if model.IsEncryptedBox(targetBoxID) {
+ encryptedBoxID = targetBoxID
+ }
}
}
- if err := holdEncryptedBoxRequest(c, boxID); err != nil {
+ if err := holdEncryptedBoxRequest(c, encryptedBoxID); err != nil {
ret.Code = -1
ret.Msg = err.Error()
return
@@ -166,7 +170,7 @@ func extensionCopy(c *gin.Context) {
}
// 统一通过 storeAssetForBox 写入,加密 box 自动脱敏 + 加密落盘 + 追加 ?box=
- storedName, storeErr := model.StoreAssetForBox(boxID, assets, fName, data)
+ storedName, storeErr := model.StoreAssetForBox(targetBoxID, assets, fName, data)
if storeErr != nil {
ret.Code = -1
ret.Msg = storeErr.Error()
@@ -174,8 +178,8 @@ func extensionCopy(c *gin.Context) {
}
assetURL := "assets/" + storedName
- if boxID != "" {
- assetURL += "?box=" + boxID
+ if encryptedBoxID != "" {
+ assetURL += "?box=" + encryptedBoxID
}
uploaded[unescaped] = assetURL
}
@@ -200,7 +204,7 @@ func extensionCopy(c *gin.Context) {
md = string(bodyData)
luteEngine.SetIndentCodeBlock(true) // 链滴支持缩进代码块,因此需要开启
tree := parse.Parse("", []byte(md), luteEngine.ParseOptions)
- tree.Box = boxID
+ tree.Box = targetBoxID
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if ast.NodeInlineMath == n.Type {
withMath = true
@@ -245,7 +249,7 @@ func extensionCopy(c *gin.Context) {
return s
})
- tree, withMath = model.HTML2Tree(dom, luteEngine, boxID)
+ tree, withMath = model.HTML2Tree(dom, luteEngine, targetBoxID)
} else {
tree = parse.Parse("", []byte(md), luteEngine.ParseOptions)
}
diff --git a/kernel/model/import.go b/kernel/model/import.go
index 55f0bc36ea..0db01c4f38 100644
--- a/kernel/model/import.go
+++ b/kernel/model/import.go
@@ -59,17 +59,34 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
-// getImportAssetsDir 返回导入资源的固定落盘目录。普通笔记本使用全局目录,加密笔记本保持资源隔离。
-func getImportAssetsDir(boxID string) string {
+// GetImportAssetsDir 返回导入资源的落盘目录。普通笔记本复用已有本地目录,否则回退到全局目录。
+func GetImportAssetsDir(boxID, docDirLocalPath string) string {
+ globalAssetsDir := filepath.Join(util.DataDir, "assets")
+ if !ast.IsNodeIDPattern(boxID) {
+ return globalAssetsDir
+ }
+
+ boxLocalPath := filepath.Join(util.DataDir, boxID)
+ boxAssetsDir := filepath.Join(boxLocalPath, "assets")
if IsEncryptedBox(boxID) {
- return filepath.Join(util.DataDir, boxID, "assets")
+ return boxAssetsDir
+ }
+
+ if docDirLocalPath != "" && gulu.File.IsSubPath(boxLocalPath, docDirLocalPath) {
+ docAssetsDir := filepath.Join(docDirLocalPath, "assets")
+ if gulu.File.IsDir(docAssetsDir) {
+ return docAssetsDir
+ }
+ }
+ if gulu.File.IsDir(boxAssetsDir) {
+ return boxAssetsDir
}
- return filepath.Join(util.DataDir, "assets")
+ return globalAssetsDir
}
func HTML2Tree(htmlStr string, luteEngine *lute.Lute, boxID string) (tree *parse.Tree, withMath bool) {
htmlStr = gulu.Str.RemovePUA(htmlStr)
- assetDirPath := getImportAssetsDir(boxID)
+ assetDirPath := GetImportAssetsDir(boxID, "")
_ = os.MkdirAll(assetDirPath, 0755)
tree = luteEngine.HTML2Tree(htmlStr)
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
@@ -1210,7 +1227,9 @@ func importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool)
baseHPath = block.HPath
baseTargetPath = strings.TrimSuffix(block.Path, ".sy")
}
- assetDirPath := getImportAssetsDir(boxID)
+ targetDocDirLocalPath := filepath.Join(util.DataDir, boxID,
+ filepath.FromSlash(strings.TrimPrefix(baseTargetPath, "/")))
+ assetDirPath := GetImportAssetsDir(boxID, targetDocDirLocalPath)
if err = os.MkdirAll(assetDirPath, 0755); err != nil {
return
}
diff --git a/kernel/model/import_test.go b/kernel/model/import_test.go
index 1ecf5115d7..c1a684dfa8 100644
--- a/kernel/model/import_test.go
+++ b/kernel/model/import_test.go
@@ -79,7 +79,7 @@ func TestImportFromLocalPathRejectsClosedNotebookBeforeWriting(t *testing.T) {
}
}
-func TestGetImportAssetsDirSeparatesEncryptedNotebooks(t *testing.T) {
+func TestGetImportAssetsDir(t *testing.T) {
originalDataDir := util.DataDir
util.DataDir = t.TempDir()
t.Cleanup(func() {
@@ -87,20 +87,74 @@ func TestGetImportAssetsDirSeparatesEncryptedNotebooks(t *testing.T) {
})
normalBoxID := "20260812000000-normal0"
- if err := os.MkdirAll(filepath.Join(util.DataDir, normalBoxID, "assets"), 0755); err != nil {
+ normalBoxDir := filepath.Join(util.DataDir, normalBoxID)
+ globalAssetsDir := filepath.Join(util.DataDir, "assets")
+ if got := GetImportAssetsDir(normalBoxID, normalBoxDir); got != globalAssetsDir {
+ t.Fatalf("ordinary notebook without local assets dir = %q, want %q", got, globalAssetsDir)
+ }
+ if _, err := os.Stat(filepath.Join(normalBoxDir, "assets")); !os.IsNotExist(err) {
+ t.Fatalf("selecting ordinary notebook assets unexpectedly created the directory: %v", err)
+ }
+
+ boxAssetsDir := filepath.Join(normalBoxDir, "assets")
+ if err := os.MkdirAll(boxAssetsDir, 0755); err != nil {
+ t.Fatal(err)
+ }
+ if got := GetImportAssetsDir(normalBoxID, normalBoxDir); got != boxAssetsDir {
+ t.Fatalf("ordinary notebook assets dir = %q, want %q", got, boxAssetsDir)
+ }
+
+ docDir := filepath.Join(normalBoxDir, "20260812000001-docdir")
+ docAssetsDir := filepath.Join(docDir, "assets")
+ if err := os.MkdirAll(docAssetsDir, 0755); err != nil {
t.Fatal(err)
}
- if got, want := getImportAssetsDir(normalBoxID), filepath.Join(util.DataDir, "assets"); got != want {
- t.Fatalf("ordinary notebook import assets dir = %q, want %q", got, want)
+ if got := GetImportAssetsDir(normalBoxID, docDir); got != docAssetsDir {
+ t.Fatalf("ordinary document assets dir = %q, want %q", got, docAssetsDir)
}
- encryptedBoxID := "20260812000001-encrypt"
+ encryptedBoxID := "20260812000002-encrypt"
markRuntimeEncryptedBox(encryptedBoxID)
t.Cleanup(func() {
forgetRuntimeEncryptedBox(encryptedBoxID)
})
- if got, want := getImportAssetsDir(encryptedBoxID), filepath.Join(util.DataDir, encryptedBoxID, "assets"); got != want {
- t.Fatalf("encrypted notebook import assets dir = %q, want %q", got, want)
+ encryptedBoxAssetsDir := filepath.Join(util.DataDir, encryptedBoxID, "assets")
+ if got := GetImportAssetsDir(encryptedBoxID, docDir); got != encryptedBoxAssetsDir {
+ t.Fatalf("encrypted notebook assets dir = %q, want %q", got, encryptedBoxAssetsDir)
+ }
+}
+
+func TestHTML2TreeUsesExistingNotebookAssets(t *testing.T) {
+ originalDataDir := util.DataDir
+ util.DataDir = t.TempDir()
+ t.Cleanup(func() {
+ util.DataDir = originalDataDir
+ })
+
+ boxID := "20260812000003-htmlimg"
+ boxAssetsDir := filepath.Join(util.DataDir, boxID, "assets")
+ if err := os.MkdirAll(boxAssetsDir, 0755); err != nil {
+ t.Fatal(err)
+ }
+
+ tree, _ := HTML2Tree(`<img alt="diagram" src="data:image/svg+xml;base64,PHN2Zz48L3N2Zz4=">`, util.NewLute(), boxID)
+ entries, err := os.ReadDir(boxAssetsDir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(entries) != 1 {
+ t.Fatalf("notebook assets count = %d, want 1", len(entries))
+ }
+ if _, err = os.Stat(filepath.Join(util.DataDir, "assets")); !os.IsNotExist(err) {
+ t.Fatalf("HTML conversion unexpectedly created global assets: %v", err)
+ }
+
+ assets := getAssetsLinkDests(tree.Root, false)
+ if len(assets) != 1 {
+ t.Fatalf("converted asset references = %v, want one reference", assets)
+ }
+ if strings.Contains(assets[0], "?box=") {
+ t.Fatalf("ordinary notebook asset reference contains box query: %q", assets[0])
}
}