{"record":{"id":"8a6ae83e183dbd5a","repo":"siyuan-note/siyuan","slug":"failed-to-insert-asset-file-please-reopen-the-doc","errorCode":null,"errorMessage":"Failed to insert asset file, please reopen the document","messagePattern":"Failed to insert asset file, please reopen the document","errorType":"http","errorClass":null,"httpStatus":200,"severity":"error","filePath":"kernel/model/upload.go","lineNumber":43,"sourceCode":"\t\"path\"\n\t\"path/filepath\"\n\t\"strings\"\n\n\t\"github.com/88250/gulu\"\n\t\"github.com/88250/lute/ast\"\n\t\"github.com/gin-gonic/gin\"\n\t\"github.com/siyuan-note/filelock\"\n\t\"github.com/siyuan-note/logging\"\n\t\"github.com/siyuan-note/siyuan/kernel/cache\"\n\t\"github.com/siyuan-note/siyuan/kernel/treenode\"\n\t\"github.com/siyuan-note/siyuan/kernel/util\"\n)\n\n// InsertAssetBytes 将内存中的资源直接写入目标文档资源目录，避免生成内容经过明文临时文件。\nfunc InsertAssetBytes(id, fileName string, data []byte) (assetPath string, created bool, err error) {\n\tbt := treenode.GetBlockTree(id)\n\tif bt == nil {\n\t\treturn \"\", false, errors.New(Conf.Language(71))\n\t}\n\tif len(data) == 0 {\n\t\treturn \"\", false, errors.New(\"asset data is empty\")\n\t}\n\n\tbaseName := filepath.Base(fileName)\n\tfName := util.FilterUploadFileName(baseName)\n\text := strings.ToLower(filepath.Ext(fName))\n\tfName = strings.TrimSuffix(fName, filepath.Ext(fName)) + ext\n\tif fName == \"\" || fName == \".\" || ext == \"\" {\n\t\treturn \"\", false, errors.New(\"invalid asset filename\")\n\t}\n\n\tdocDirLocalPath := filepath.Join(util.DataDir, bt.BoxID, path.Dir(bt.Path))\n\tassetsDirPath := getAssetsDir(filepath.Join(util.DataDir, bt.BoxID), docDirLocalPath)\n\tif err = os.MkdirAll(assetsDirPath, 0755); err != nil {\n\t\treturn \"\", false, err\n\t}","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/afa823b6b4e4f183511e0bc0a3be93caa94c7c97/kernel/model/upload.go#L25-L61","documentation":"InsertAssetBytes (kernel/model/upload.go:36) writes in-memory asset data directly into the target document's assets directory. Its first step is treenode.GetBlockTree(id); a nil entry means the kernel blocktree does not know this doc/block id, so the assets dir cannot be computed and the localized message Conf.Language(71) - \"Failed to insert asset file, please reopen the document\" - is returned.","triggerScenarios":"Inserting an asset for a document whose id is not indexed yet (brand-new doc before the index queue commits, kernel still booting/indexing), a stale id after the doc was deleted, or an id from a different workspace.","commonSituations":"Agents/plugins inserting assets immediately after creating a document without waiting for indexing; retries against the wrong workspace; kernel just started and indexing is in progress.","solutions":["Make sure the document exists and is open/indexed, then retry - the message itself says 'reopen the document'","Use the doc id exactly as returned by the creation API and let indexing settle (wait for the kernel index task to finish)","Verify the id via /api/query/block or /api/block/getBlockInfo before inserting","If it persists, rebuild the index (设置 - 搜索 - 重建索引)"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Confirm the target doc resolves before inserting:\n// POST /api/query/block {\"stmt\": \"SELECT * FROM blocks WHERE id='<docID>'\"}\n// Non-empty result -> blocktree knows the doc; InsertAssetBytes will resolve the assets dir.","typeGuard":null,"tryCatchPattern":"assetPath, created, err := model.InsertAssetBytes(id, name, data)\nif err != nil && err.Error() == model.Conf.Language(71) {\n\t// doc not indexed yet: open/reopen the doc, wait for indexing, retry once or twice with backoff\n}","preventionTips":["Wait for doc indexing to commit before inserting assets (or open the doc in the editor first)","Use the id returned by the doc-creation API, unmodified","Verify you are on the workspace that actually contains the doc"],"tags":["asset","upload","blocktree","indexing","not-found"],"backgroundTag":"block-id-not-found","analyzedSha":"afa823b6b4e4f183511e0bc0a3be93caa94c7c97","analyzedAt":"2026-08-18T17:04:10.865Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}