{"record":{"id":"46ca06230685e867","repo":"siyuan-note/siyuan","slug":"conf-language-71","errorCode":null,"errorMessage":"Conf.Language(71)","messagePattern":"Conf\\.Language\\(71\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/upload.go","lineNumber":44,"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":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/upload.go#L26-L62","documentation":"InsertAssetBytes writes in-memory asset bytes directly into the target document's assets directory. It first resolves the document ID via treenode.GetBlockTree; if the ID does not exist in the block tree the call fails with the localized 'block not found' message (Conf.Language(71)). The library refuses to guess a destination for an unknown document.","triggerScenarios":"Calling InsertAssetBytes(id, fileName, data) with an id that is not a live block/document ID — a stale ID from a deleted document, a typo'd ID, or an ID from an encrypted notebook that is not currently open (its tree is not loaded). Called by assetCreateHTML and GenerateDocumentImage.","commonSituations":"Plugin/AI code generating images into a document that was closed or deleted while processing; race between fetching a document ID and inserting into it; working with encrypted notebooks whose block tree lookup needs the notebook open.","solutions":["Verify the document exists before inserting: check that /api/filetree/getDocByID (or treenode.GetBlockTree) returns the block for the id","Re-fetch the current document ID from the UI/kernel instead of caching it across operations","For encrypted notebooks, ensure the notebook is opened in the workspace before inserting assets","Recreate the document if it was deleted, and capture its new ID","Surface the localized message to the user and abort the insert rather than writing to a default location"],"exampleFix":"// before\nassetPath, _, err := model.InsertAssetBytes(docID, \"img.png\", data)\n// after\nif bt := treenode.GetBlockTree(docID); bt == nil {\n    return fmt.Errorf(\"document %s does not exist; open or recreate it first\", docID)\n}\nassetPath, _, err := model.InsertAssetBytes(docID, \"img.png\", data)","handlingStrategy":"validation","validationCode":"func docExists(id string) bool { return treenode.GetBlockTree(id) != nil }\n// HTTP equivalent: POST /api/filetree/getDocByID {\"id\": docID} returns non-error","typeGuard":"func validDocID(id string) bool {\n    return id != \"\" && treenode.GetBlockTree(id) != nil\n}","tryCatchPattern":"assetPath, created, err := model.InsertAssetBytes(docID, name, data)\nif err != nil && err.Error() == model.Conf.Language(71) {\n    // block not found: refresh doc ID or open the encrypted notebook, then retry once\n}","preventionTips":["Always resolve document IDs immediately before use; never cache across document delete/close","Open encrypted notebooks before programmatic asset inserts","Validate IDs with getDocByID in pre-flight checks in plugins/scripts"],"tags":["block-not-found","document-id","asset"],"backgroundTag":"record-not-found","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}