From 38099e489285ae729e8a8147202aef5d61289afe Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Wed, 15 Nov 2023 09:08:41 +0800 Subject: [PATCH] :art: Add kernel API `/api/filetree/getIDsByHPath` https://github.com/siyuan-note/siyuan/issues/9654 --- API.md | 30 ++++++++++++++++++++++++++++-- API_zh_CN.md | 27 +++++++++++++++++++++++++++ kernel/api/filetree.go | 30 ++++++++++++++++++++++++++++++ kernel/api/router.go | 1 + kernel/model/file.go | 13 +++++++++++++ kernel/treenode/blocktree.go | 16 ++++++++++++++++ 6 files changed, 115 insertions(+), 2 deletions(-) diff --git a/API.md b/API.md index 453e92d97..3c426f07d 100644 --- a/API.md +++ b/API.md @@ -415,7 +415,7 @@ View API token in Settings - About, request header: `Authorization: T } ``` -### Get human readable path based on path +### Get human-readable path based on path * `/api/filetree/getHPathByPath` * Parameters @@ -439,7 +439,7 @@ View API token in Settings - About, request header: `Authorization: T } ``` -### Get human readable path based on ID +### Get human-readable path based on ID * `/api/filetree/getHPathByID` * Parameters @@ -461,6 +461,32 @@ View API token in Settings - About, request header: `Authorization: T } ``` +### Get IDs based on human-readable path + +* `/api/filetree/getIDsByHPath` +* Parameters + + ```json + { + "path": "/foo/bar", + "notebook": "20210808180117-czj9bvb" + } + ``` + + * `path`: Human-readable path + * `notebook`: Notebook ID +* Return value + + ```json + { + "code": 0, + "msg": "", + "data": [ + "20200813004931-q4cu8na" + ] + } + ``` + ## Assets ### Upload assets diff --git a/API_zh_CN.md b/API_zh_CN.md index 4e9e221fe..83e4745bf 100644 --- a/API_zh_CN.md +++ b/API_zh_CN.md @@ -19,6 +19,7 @@ * [移动文档](#移动文档) * [根据路径获取人类可读路径](#根据路径获取人类可读路径) * [根据 ID 获取人类可读路径](#根据-ID-获取人类可读路径) + * [根据人类可读路径获取 IDs](#根据人类可读路径获取-IDs) * [资源文件](#资源文件) * [上传资源文件](#上传资源文件) * [块](#块) @@ -459,6 +460,32 @@ } ``` +### 根据人类可读路径获取 IDs + +* `/api/filetree/getIDsByHPath` +* 参数 + + ```json + { + "path": "/foo/bar", + "notebook": "20210808180117-czj9bvb" + } + ``` + + * `path`:人类可读路径 + * `notebook`:笔记本 ID +* 返回值 + + ```json + { + "code": 0, + "msg": "", + "data": [ + "20200813004931-q4cu8na" + ] + } + ``` + ## 资源文件 ### 上传资源文件 diff --git a/kernel/api/filetree.go b/kernel/api/filetree.go index 0d879dbe9..bdc7e5a4e 100644 --- a/kernel/api/filetree.go +++ b/kernel/api/filetree.go @@ -249,6 +249,36 @@ func getFullHPathByID(c *gin.Context) { ret.Data = hPath } +func getIDsByHPath(c *gin.Context) { + ret := gulu.Ret.NewResult() + defer c.JSON(http.StatusOK, ret) + + arg, ok := util.JsonArg(c, ret) + if !ok { + return + } + if nil == arg["path"] { + return + } + if nil == arg["notebook"] { + return + } + + notebook := arg["notebook"].(string) + if util.InvalidIDPattern(notebook, ret) { + return + } + + p := arg["path"].(string) + ids, err := model.GetIDsByHPath(p, notebook) + if nil != err { + ret.Code = -1 + ret.Msg = err.Error() + return + } + ret.Data = ids +} + func moveDocs(c *gin.Context) { ret := gulu.Ret.NewResult() defer c.JSON(http.StatusOK, ret) diff --git a/kernel/api/router.go b/kernel/api/router.go index 67113f88c..919233d5b 100644 --- a/kernel/api/router.go +++ b/kernel/api/router.go @@ -106,6 +106,7 @@ func ServeAPI(ginServer *gin.Engine) { ginServer.Handle("POST", "/api/filetree/getHPathsByPaths", model.CheckAuth, getHPathsByPaths) ginServer.Handle("POST", "/api/filetree/getHPathByID", model.CheckAuth, getHPathByID) ginServer.Handle("POST", "/api/filetree/getFullHPathByID", model.CheckAuth, getFullHPathByID) + ginServer.Handle("POST", "/api/filetree/getIDsByHPath", model.CheckAuth, getIDsByHPath) ginServer.Handle("POST", "/api/filetree/doc2Heading", model.CheckAuth, model.CheckReadonly, doc2Heading) ginServer.Handle("POST", "/api/filetree/heading2Doc", model.CheckAuth, model.CheckReadonly, heading2Doc) ginServer.Handle("POST", "/api/filetree/li2Doc", model.CheckAuth, model.CheckReadonly, li2Doc) diff --git a/kernel/model/file.go b/kernel/model/file.go index 1d8dfd80d..d5d637a15 100644 --- a/kernel/model/file.go +++ b/kernel/model/file.go @@ -1183,6 +1183,19 @@ func GetFullHPathByID(id string) (hPath string, err error) { return } +func GetIDsByHPath(hpath, boxID string) (ret []string, err error) { + roots := treenode.GetBlockTreeRootsByHPath(boxID, hpath) + if 1 > len(roots) { + return + } + + for _, root := range roots { + ret = append(ret, root.ID) + } + ret = gulu.Str.RemoveDuplicatedElem(ret) + return +} + func MoveDocs(fromPaths []string, toBoxID, toPath string, callback interface{}) (err error) { toBox := Conf.Box(toBoxID) if nil == toBox { diff --git a/kernel/treenode/blocktree.go b/kernel/treenode/blocktree.go index b8dcf13e0..3929c503a 100644 --- a/kernel/treenode/blocktree.go +++ b/kernel/treenode/blocktree.go @@ -143,6 +143,22 @@ func GetBlockTreeRootByHPath(boxID, hPath string) (ret *BlockTree) { return } +func GetBlockTreeRootsByHPath(boxID, hPath string) (ret []*BlockTree) { + hPath = gulu.Str.RemoveInvisible(hPath) + blockTrees.Range(func(key, value interface{}) bool { + slice := value.(*btSlice) + slice.m.Lock() + for _, b := range slice.data { + if b.BoxID == boxID && b.HPath == hPath && b.RootID == b.ID { + ret = append(ret, b) + } + } + slice.m.Unlock() + return true + }) + return +} + func GetBlockTreeRootByHPathPreferredParentID(boxID, hPath, preferredParentID string) (ret *BlockTree) { hPath = gulu.Str.RemoveInvisible(hPath) var roots []*BlockTree