Skip to content

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

:bug: Preserve local shorthand sources when attachment migration fails https://github.com/siyuan-note/siyuan/issues/21003

Daniel19a1be1341e8
diff --git a/kernel/model/shortcuts.go b/kernel/model/shortcuts.go
index 9deb8d86d9..e2e7581304 100644
--- a/kernel/model/shortcuts.go
+++ b/kernel/model/shortcuts.go
@@ -56,11 +56,6 @@ func MoveLocalShorthands(boxID string) (retIDs []string, err error) {
return
}
defer ReleaseEncryptedBoxOperation(boxID)
- if recovered, recoveryErr := recoverLocalShorthandCreation(boxID); recoveryErr != nil {
- return nil, recoveryErr
- } else if recovered != "" {
- retIDs = append(retIDs, recovered)
- }
entries, err := os.ReadDir(shorthandsDir)
if err != nil {
return retIDs, err
@@ -72,7 +67,7 @@ func MoveLocalShorthands(boxID string) (retIDs []string, err error) {
assetsEntries, readErr := os.ReadDir(filepath.Join(shorthandsDir, entry.Name()))
if nil != readErr {
logging.LogErrorf("read dir [%s] failed: %s", shorthandsDir, readErr)
- continue
+ return retIDs, readErr
}
for _, assetEntry := range assetsEntries {
if assetEntry.IsDir() {
@@ -83,12 +78,19 @@ func MoveLocalShorthands(boxID string) (retIDs []string, err error) {
assetWritePath := filepath.Join(assetsDir, assetEntry.Name())
if renameErr := os.Rename(p, assetWritePath); nil != renameErr {
logging.LogErrorf("rename file [%s] to [%s] failed: %s", p, assetWritePath, renameErr)
- continue
+ return retIDs, renameErr
}
}
}
}
+ // Complete attachment migration before creating content or consuming a recovered source.
+ if recovered, recoveryErr := recoverLocalShorthandCreation(boxID); recoveryErr != nil {
+ return nil, recoveryErr
+ } else if recovered != "" {
+ retIDs = append(retIDs, recovered)
+ }
+
hPath := Conf.FileTree.Load().ShorthandSavePath
if "" != hPath {
var renderErr error
diff --git a/kernel/model/shortcuts_test.go b/kernel/model/shortcuts_test.go
index 8d55243dec..739f5d243b 100644
--- a/kernel/model/shortcuts_test.go
+++ b/kernel/model/shortcuts_test.go
@@ -17,6 +17,7 @@
package model
import (
+ "bytes"
"os"
"path/filepath"
"testing"
@@ -109,3 +110,37 @@ func TestVerifyShorthandDocPersisted(t *testing.T) {
t.Fatal("expected missing persisted shorthand document to fail verification")
}
}
+
+func TestMoveLocalShorthandsKeepsSourceWhenAttachmentMoveFails(t *testing.T) {
+ fixture := setupFileOperationTest(t)
+ original := util.ShortcutsPath
+ util.ShortcutsPath = t.TempDir()
+ t.Cleanup(func() { util.ShortcutsPath = original })
+ assets := filepath.Join(util.ShortcutsPath, "shorthands", "assets")
+ if err := os.MkdirAll(assets, 0755); err != nil {
+ t.Fatal(err)
+ }
+ source := filepath.Join(filepath.Dir(assets), "1788062238740.md")
+ content := []byte("![image](assets/image.png)")
+ if err := os.WriteFile(source, content, 0644); err != nil {
+ t.Fatal(err)
+ }
+ attachment := filepath.Join(assets, "image.png")
+ if err := os.WriteFile(attachment, []byte("image"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ // A directory at the destination makes rename fail on every supported platform.
+ if err := os.MkdirAll(filepath.Join(util.DataDir, "assets", "image.png"), 0755); err != nil {
+ t.Fatal(err)
+ }
+ ids, err := MoveLocalShorthands(fixture.box.ID)
+ if err == nil || len(ids) != 0 {
+ t.Fatalf("attachment failure created documents: %v, %v", ids, err)
+ }
+ if got, err := os.ReadFile(source); err != nil || !bytes.Equal(got, content) {
+ t.Fatalf("source changed: %q, %v", got, err)
+ }
+ if _, err := os.Stat(attachment); err != nil {
+ t.Fatalf("attachment lost: %v", err)
+ }
+}