瀏覽代碼

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

Liang Ding 2 年之前
父節點
當前提交
14eb29b26c
共有 4 個文件被更改,包括 110 次插入0 次删除
  1. 30 0
      API.md
  2. 31 0
      API_zh_CN.md
  3. 48 0
      kernel/api/file.go
  4. 1 0
      kernel/api/router.go

+ 30 - 0
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
 

+ 31 - 0
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 文本

+ 48 - 0
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)

+ 1 - 0
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)