From 14eb29b26c78b4cd21cd6eb7bff23cb5ef16024f Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 24 Mar 2023 11:40:12 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=8F=90=E4=BE=9B=E5=88=97=E5=87=BA?= =?UTF-8?q?=E6=96=87=E4=BB=B6=20API=20Fix=20https://github.com/siyuan-note?= =?UTF-8?q?/siyuan/issues/7765?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API.md | 30 +++++++++++++++++++++++++++ API_zh_CN.md | 31 ++++++++++++++++++++++++++++ kernel/api/file.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ kernel/api/router.go | 1 + 4 files changed, 110 insertions(+) diff --git a/API.md b/API.md index 48a386fa2..d866eb087 100644 --- a/API.md +++ b/API.md @@ -39,6 +39,7 @@ * [Get file](#Get-file) * [Put file](#Put-file) * [Remove file](#Remove-file) + * [List files](#List-files) * [Export](#Export) * [Export Markdown](#Export-Markdown) * [Notification](#Notification) @@ -886,6 +887,35 @@ View API token in Settings - About, request header: `Authorization: T } ``` +### List files + +* `/api/file/readDir` +* Parameters + + ```json + { + "path": "/data/20210808180117-6v0mkxr/20200923234011-ieuun1p.sy" + } + ``` + * `path`: the file path under the workspace path +* Return value + + ```json + { + "code": 0, + "msg": "", + "data": [ + { + "isDir": true, + "name": "20210808180320-abz7w6k" + }, + { + "isDir": false, + "name": "20210808180320-abz7w6k.sy" + } + ] + } + ``` ## Export diff --git a/API_zh_CN.md b/API_zh_CN.md index 13be50e40..01404aab1 100644 --- a/API_zh_CN.md +++ b/API_zh_CN.md @@ -39,6 +39,7 @@ * [获取文件](#获取文件) * [写入文件](#写入文件) * [删除文件](#删除文件) + * [列出文件](#列出文件) * [导出](#导出) * [导出 Markdown 文本](#导出-markdown-文本) * [通知](#通知) @@ -880,6 +881,36 @@ } ``` +### 列出文件 + +* `/api/file/readDir` +* 参数 + + ```json + { + "path": "/data/20210808180117-6v0mkxr/20200923234011-ieuun1p.sy" + } + ``` + * `path`:工作空间路径下的文件路径 +* 返回值 + + ```json + { + "code": 0, + "msg": "", + "data": [ + { + "isDir": true, + "name": "20210808180320-abz7w6k" + }, + { + "isDir": false, + "name": "20210808180320-abz7w6k.sy" + } + ] + } + ``` + ## 导出 ### 导出 Markdown 文本 diff --git a/kernel/api/file.go b/kernel/api/file.go index c63b0ad2c..5845c43bb 100644 --- a/kernel/api/file.go +++ b/kernel/api/file.go @@ -110,8 +110,56 @@ func getFile(c *gin.Context) { } } +func readDir(c *gin.Context) { + ret := gulu.Ret.NewResult() + defer c.JSON(http.StatusOK, ret) + + arg, ok := util.JsonArg(c, ret) + if !ok { + c.JSON(http.StatusOK, ret) + return + } + + dirPath := arg["path"].(string) + dirPath = filepath.Join(util.WorkspaceDir, dirPath) + info, err := os.Stat(dirPath) + if os.IsNotExist(err) { + c.Status(404) + return + } + if nil != err { + logging.LogErrorf("stat [%s] failed: %s", dirPath, err) + c.Status(500) + return + } + if !info.IsDir() { + logging.LogErrorf("file [%s] is not a directory", dirPath) + c.Status(405) + return + } + + entries, err := os.ReadDir(dirPath) + if nil != err { + logging.LogErrorf("read dir [%s] failed: %s", dirPath, err) + c.Status(500) + return + } + + files := []map[string]interface{}{} + for _, entry := range entries { + files = append(files, map[string]interface{}{ + "name": entry.Name(), + "isDir": entry.IsDir(), + }) + } + + ret.Data = files +} + func removeFile(c *gin.Context) { ret := gulu.Ret.NewResult() + defer c.JSON(http.StatusOK, ret) + arg, ok := util.JsonArg(c, ret) if !ok { c.JSON(http.StatusOK, ret) diff --git a/kernel/api/router.go b/kernel/api/router.go index 1c64a7601..a6b2fb55a 100644 --- a/kernel/api/router.go +++ b/kernel/api/router.go @@ -176,6 +176,7 @@ func ServeAPI(ginServer *gin.Engine) { ginServer.Handle("POST", "/api/file/putFile", model.CheckAuth, model.CheckReadonly, putFile) ginServer.Handle("POST", "/api/file/copyFile", model.CheckAuth, model.CheckReadonly, copyFile) ginServer.Handle("POST", "/api/file/removeFile", model.CheckAuth, model.CheckReadonly, removeFile) + ginServer.Handle("POST", "/api/file/readDir", model.CheckAuth, model.CheckReadonly, readDir) ginServer.Handle("POST", "/api/ref/refreshBacklink", model.CheckAuth, refreshBacklink) ginServer.Handle("POST", "/api/ref/getBacklink", model.CheckAuth, getBacklink)