Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: 提供列出文件 API Fix https://github.com/siyuan-note/siyuan/issues/7765

Liang Ding73f0b0583397
diff --git a/API.md b/API.md
index 48a386fa2d..d866eb0875 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 <kbd>Settings - About</kbd>, 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 13be50e40b..01404aab12 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 c63b0ad2c1..5845c43bb5 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 1c64a76012..a6b2fb55a3 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)