siyuan-note/siyuan · error
Failed to insert asset file, please reopen the document
Error message
Failed to insert asset file, please reopen the document
What it means
Thrown by InsertAssetBytes when treenode.GetBlockTree(id) returns nil, meaning the provided block ID does not exist in the in-memory block tree (blocktree.db). Conf.Language(71) resolves to the localized message 'Failed to insert asset file, please reopen the document'. The function is designed to write asset bytes directly into a document's assets directory, and it needs the block's BoxID and Path to locate that directory, so a missing block tree entry is fatal.
Source
Thrown at kernel/model/upload.go:43
"path"
"path/filepath"
"strings"
"github.com/88250/gulu"
"github.com/88250/lute/ast"
"github.com/gin-gonic/gin"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
// InsertAssetBytes 将内存中的资源直接写入目标文档资源目录,避免生成内容经过明文临时文件。
func InsertAssetBytes(id, fileName string, data []byte) (assetPath string, created bool, err error) {
bt := treenode.GetBlockTree(id)
if bt == nil {
return "", false, errors.New(Conf.Language(71))
}
if len(data) == 0 {
return "", false, errors.New("asset data is empty")
}
baseName := filepath.Base(fileName)
fName := util.FilterUploadFileName(baseName)
ext := strings.ToLower(filepath.Ext(fName))
fName = strings.TrimSuffix(fName, filepath.Ext(fName)) + ext
if fName == "" || fName == "." || ext == "" {
return "", false, errors.New("invalid asset filename")
}
docDirLocalPath := filepath.Join(util.DataDir, bt.BoxID, path.Dir(bt.Path))
assetsDirPath := getAssetsDir(filepath.Join(util.DataDir, bt.BoxID), docDirLocalPath)
if err = os.MkdirAll(assetsDirPath, 0755); err != nil {
return "", false, err
}View on GitHub (pinned to 251596fc0d)
Solutions
- Ensure the document is fully loaded and indexed before inserting assets — call the kernel API to open/index the document first
- Verify the block ID still exists via the block tree query API before calling InsertAssetBytes
- Reopen the document in the UI to force a re-index, then retry the insert
- If this happens after kernel startup, wait for the boot indexing phase to complete (check server status endpoint)
Example fix
// before
assetPath, created, err := model.InsertAssetBytes(blockID, "file.png", data)
// after
if bt := treenode.GetBlockTree(blockID); bt == nil {
// trigger re-index or report stale block
return fmt.Errorf("block %s not found, reopen the document", blockID)
}
assetPath, created, err := model.InsertAssetBytes(blockID, "file.png", data) Defensive patterns
Strategy: validation
Validate before calling
// Go caller — check block tree before inserting
bt := treenode.GetBlockTree(blockID)
if bt == nil {
// trigger re-index or return a clear error to the user
return fmt.Errorf("block %s not in tree; reopen the document", blockID)
}
assetPath, created, err := model.InsertAssetBytes(blockID, fileName, data) Prevention
- Always verify a block ID exists in the block tree before any asset operation that depends on it
- After kernel startup, wait for the indexing phase to complete before processing asset inserts
- Handle the case where a user closes or deletes a document mid-operation by re-validating block IDs
When it happens
Trigger: Calling InsertAssetBytes with a block ID that was never indexed, was deleted, or belongs to a notebook whose block tree hasn't been loaded yet. Also triggered right after a kernel restart if the block tree is still rebuilding from disk.
Common situations: The document was just created and its .sy file hasn't been indexed into blocktree.db yet; the block was removed in a concurrent transaction; the workspace was just opened and indexing is incomplete; a stale block ID was cached client-side after a sync conflict.
Related errors
- asset data is empty
- invalid asset filename
- no file found
- field [file] or [url] must not be empty
- task list item marker length should be 1
AI-assisted analysis of siyuan-note/siyuan@251596fc0d (2026-08-12).
Data as JSON: /api/errors/8a6ae83e183dbd5a.
Report an issue: GitHub.