Skip to content

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

:bug: Store ordinary imported assets globally https://github.com/siyuan-note/siyuan/issues/18657

Daniel7c0177b4b56c
diff --git a/kernel/api/extension.go b/kernel/api/extension.go
index 5f05f7b611..766ec09266 100644
--- a/kernel/api/extension.go
+++ b/kernel/api/extension.go
@@ -52,11 +52,6 @@ func extensionCopy(c *gin.Context) {
if model.IsEncryptedBox(nb) {
boxID = nb
assets = filepath.Join(util.DataDir, nb, "assets")
- } else {
- assets = filepath.Join(util.DataDir, nb, "assets")
- if !gulu.File.IsDir(assets) {
- assets = filepath.Join(util.DataDir, "assets")
- }
}
}
if err := holdEncryptedBoxRequest(c, boxID); err != nil {
diff --git a/kernel/model/import.go b/kernel/model/import.go
index 6aa5cf3c87..fcdef4977a 100644
--- a/kernel/model/import.go
+++ b/kernel/model/import.go
@@ -59,13 +59,18 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
+// getImportAssetsDir 返回导入资源的固定落盘目录。普通笔记本使用全局目录,加密笔记本保持资源隔离。
+func getImportAssetsDir(boxID string) string {
+ if IsEncryptedBox(boxID) {
+ return filepath.Join(util.DataDir, boxID, "assets")
+ }
+ return filepath.Join(util.DataDir, "assets")
+}
+
func HTML2Tree(htmlStr string, luteEngine *lute.Lute, boxID string) (tree *parse.Tree, withMath bool) {
htmlStr = gulu.Str.RemovePUA(htmlStr)
- assetDirPath := filepath.Join(util.DataDir, "assets")
- if boxID != "" {
- assetDirPath = filepath.Join(util.DataDir, boxID, "assets")
- _ = os.MkdirAll(assetDirPath, 0755)
- }
+ assetDirPath := getImportAssetsDir(boxID)
+ _ = os.MkdirAll(assetDirPath, 0755)
tree = luteEngine.HTML2Tree(htmlStr)
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
@@ -1202,7 +1207,7 @@ func importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool)
FlushTxQueue()
- var baseHPath, baseTargetPath, boxLocalPath string
+ var baseHPath, baseTargetPath string
if "/" == toPath {
baseHPath = "/"
baseTargetPath = "/"
@@ -1215,7 +1220,10 @@ func importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool)
baseHPath = block.HPath
baseTargetPath = strings.TrimSuffix(block.Path, ".sy")
}
- boxLocalPath = filepath.Join(util.DataDir, boxID)
+ assetDirPath := getImportAssetsDir(boxID)
+ if err = os.MkdirAll(assetDirPath, 0755); err != nil {
+ return
+ }
hPathsIDs := map[string]string{}
idPaths := map[string]string{}
@@ -1252,12 +1260,7 @@ func importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool)
logging.LogErrorf("read asset [%s] failed: %s", currentPath, readErr)
return nil
}
- assetDirForBox := filepath.Join(util.DataDir, "assets")
- if boxID != "" {
- assetDirForBox = filepath.Join(util.DataDir, boxID, "assets")
- _ = os.MkdirAll(assetDirForBox, 0755)
- }
- name, err = storeAssetForBox(boxID, assetDirForBox, baseName, data)
+ name, err = storeAssetForBox(boxID, assetDirPath, baseName, data)
if err != nil {
logging.LogErrorf("store asset [%s] for box [%s] failed: %s", currentPath, boxID, err)
return nil
@@ -1371,8 +1374,6 @@ func importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool)
tree.HPath = hPath
tree.Root.Spec = treenode.CurrentSpec
- docDirLocalPath := filepath.Dir(filepath.Join(boxLocalPath, targetPath))
- assetDirPath := getAssetsDir(boxLocalPath, docDirLocalPath)
currentDir := filepath.Dir(currentPath)
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering || (ast.NodeLinkDest != n.Type && !n.IsTextMarkType("a")) {
@@ -1429,29 +1430,15 @@ func importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool)
var name string
if "" == existName {
baseName := filepath.Base(absolutePath)
- if IsEncryptedBox(boxID) {
- // 加密笔记本:文件名脱敏 + 内容加密
- ext := filepath.Ext(baseName)
- blockID := ast.NewNodeID()
- name = encryptedAssetName(ext, blockID)
- assetTargetPath := filepath.Join(assetDirPath, name)
- src, readErr := filelock.ReadFile(absolutePath)
- if readErr != nil {
- logging.LogErrorf("read asset [%s] failed: %s", absolutePath, readErr)
- return ast.WalkContinue
- }
- if err = writeAssetFile(assetTargetPath, bytes.NewReader(src), boxID, baseName); err != nil {
- logging.LogErrorf("write encrypted asset [%s] failed: %s", assetTargetPath, err)
- return ast.WalkContinue
- }
- } else {
- name = util.FilterUploadFileName(baseName)
- name = util.AssetName(name, ast.NewNodeID())
- assetTargetPath := filepath.Join(assetDirPath, name)
- if err = filelock.Copy(absolutePath, assetTargetPath); err != nil {
- logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", absolutePath, assetTargetPath, err)
- return ast.WalkContinue
- }
+ data, readErr := filelock.ReadFile(absolutePath)
+ if readErr != nil {
+ logging.LogErrorf("read asset [%s] failed: %s", absolutePath, readErr)
+ return ast.WalkContinue
+ }
+ name, err = storeAssetForBox(boxID, assetDirPath, util.FilterUploadFileName(baseName), data)
+ if err != nil {
+ logging.LogErrorf("store asset [%s] for box [%s] failed: %s", absolutePath, boxID, err)
+ return ast.WalkContinue
}
assetsDone[absolutePath] = name
} else {
@@ -1532,8 +1519,6 @@ func importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool)
tree.Root.Spec = treenode.CurrentSpec
localPathParentDir := filepath.Dir(localPath)
- docDirLocalPath := filepath.Dir(filepath.Join(boxLocalPath, targetPath))
- assetDirPath := getAssetsDir(boxLocalPath, docDirLocalPath)
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering || (ast.NodeLinkDest != n.Type && !n.IsTextMarkType("a")) {
return ast.WalkContinue
@@ -1581,29 +1566,15 @@ func importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool)
var name string
if "" == existName {
baseName := filepath.Base(absolutePath)
- if IsEncryptedBox(boxID) {
- // 加密笔记本:文件名脱敏 + 内容加密
- ext := filepath.Ext(baseName)
- blockID := ast.NewNodeID()
- name = encryptedAssetName(ext, blockID)
- assetTargetPath := filepath.Join(assetDirPath, name)
- src, readErr := filelock.ReadFile(absolutePath)
- if readErr != nil {
- logging.LogErrorf("read asset [%s] failed: %s", absolutePath, readErr)
- return ast.WalkContinue
- }
- if err = writeAssetFile(assetTargetPath, bytes.NewReader(src), boxID, baseName); err != nil {
- logging.LogErrorf("write encrypted asset [%s] failed: %s", assetTargetPath, err)
- return ast.WalkContinue
- }
- } else {
- name = util.FilterUploadFileName(baseName)
- name = util.AssetName(name, ast.NewNodeID())
- assetTargetPath := filepath.Join(assetDirPath, name)
- if err = filelock.Copy(absolutePath, assetTargetPath); err != nil {
- logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", absolutePath, assetTargetPath, err)
- return ast.WalkContinue
- }
+ data, readErr := filelock.ReadFile(absolutePath)
+ if readErr != nil {
+ logging.LogErrorf("read asset [%s] failed: %s", absolutePath, readErr)
+ return ast.WalkContinue
+ }
+ name, err = storeAssetForBox(boxID, assetDirPath, util.FilterUploadFileName(baseName), data)
+ if err != nil {
+ logging.LogErrorf("store asset [%s] for box [%s] failed: %s", absolutePath, boxID, err)
+ return ast.WalkContinue
}
assetsDone[absolutePath] = name
} else {
diff --git a/kernel/model/import_test.go b/kernel/model/import_test.go
index 71d78c118b..56e283ab5b 100644
--- a/kernel/model/import_test.go
+++ b/kernel/model/import_test.go
@@ -77,6 +77,66 @@ func TestImportFromLocalPathRejectsClosedNotebookBeforeWriting(t *testing.T) {
}
}
+func TestGetImportAssetsDirSeparatesEncryptedNotebooks(t *testing.T) {
+ originalDataDir := util.DataDir
+ util.DataDir = t.TempDir()
+ t.Cleanup(func() {
+ util.DataDir = originalDataDir
+ })
+
+ normalBoxID := "20260812000000-normal0"
+ if err := os.MkdirAll(filepath.Join(util.DataDir, normalBoxID, "assets"), 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)
+ }
+
+ encryptedBoxID := "20260812000001-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)
+ }
+}
+
+func TestStoreAssetForBoxAvoidsGlobalNameCollision(t *testing.T) {
+ assetsDir := t.TempDir()
+ existingName := "image-20260812000002-abcdefg.png"
+ existingData := []byte("existing")
+ if err := os.WriteFile(filepath.Join(assetsDir, existingName), existingData, 0644); err != nil {
+ t.Fatal(err)
+ }
+ reusedName, err := storeAssetForBox("", assetsDir, existingName, existingData)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if reusedName != existingName {
+ t.Fatalf("identical global asset name = %q, want %q", reusedName, existingName)
+ }
+
+ newData := []byte("new")
+ storedName, err := storeAssetForBox("", assetsDir, existingName, newData)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if storedName == existingName {
+ t.Fatalf("conflicting global asset reused existing name %q", storedName)
+ }
+ if data, readErr := os.ReadFile(filepath.Join(assetsDir, existingName)); readErr != nil {
+ t.Fatal(readErr)
+ } else if string(data) != string(existingData) {
+ t.Fatalf("existing global asset was overwritten: %q", data)
+ }
+ if data, readErr := os.ReadFile(filepath.Join(assetsDir, storedName)); readErr != nil {
+ t.Fatal(readErr)
+ } else if string(data) != string(newData) {
+ t.Fatalf("new global asset content = %q, want %q", data, newData)
+ }
+}
+
func TestReplaceAssetNamePreservesExistingQuery(t *testing.T) {
assetNameMap := map[string]string{"document.pdf": "encrypted.pdf"}
boxSuffix := "?box=20260731190414-j45dgmm"
diff --git a/kernel/model/upload.go b/kernel/model/upload.go
index 26f88bc3af..af70c1a144 100644
--- a/kernel/model/upload.go
+++ b/kernel/model/upload.go
@@ -551,6 +551,28 @@ func storeAssetForBox(boxID, assetDirPath, originalName string, data []byte) (di
// 普通 box:生成带 ID 的文件名,明文写入
diskName = util.AssetName(originalName, ast.NewNodeID())
writePath := filepath.Join(assetDirPath, diskName)
+ if existing, readErr := filelock.ReadFile(writePath); readErr == nil {
+ if bytes.Equal(existing, data) {
+ return diskName, nil
+ }
+
+ // 导入带有既有 NodeID 的文件时不能覆盖全局同名资源,冲突后强制生成新的资源 ID。
+ cleanName := util.RemoveID(originalName)
+ ext := filepath.Ext(cleanName)
+ name := strings.TrimSuffix(cleanName, ext)
+ if name == "" || ast.IsNodeIDPattern(name) {
+ name = "asset"
+ }
+ for {
+ diskName = util.AssetName(name+ext, ast.NewNodeID())
+ writePath = filepath.Join(assetDirPath, diskName)
+ if !filelock.IsExist(writePath) {
+ break
+ }
+ }
+ } else if !os.IsNotExist(readErr) {
+ return "", readErr
+ }
if err = filelock.WriteFile(writePath, data); err != nil {
return "", err
}