Browse Source

:art: 提供删除文件 API Fix https://github.com/siyuan-note/siyuan/issues/7678

Liang Ding 2 years ago
parent
commit
d04eacbf9a
4 changed files with 76 additions and 3 deletions
  1. 26 3
      API.md
  2. 22 0
      API_zh_CN.md
  3. 27 0
      kernel/api/file.go
  4. 1 0
      kernel/api/router.go

+ 26 - 3
API.md

@@ -38,6 +38,7 @@
 * [File](#File)
 * [File](#File)
     * [Get file](#Get-file)
     * [Get file](#Get-file)
     * [Put file](#Put-file)
     * [Put file](#Put-file)
+    * [Remove file](#Remove-file)
 * [Export](#Export)
 * [Export](#Export)
     * [Export Markdown](#Export-Markdown)
     * [Export Markdown](#Export-Markdown)
 * [Notification](#Notification)
 * [Notification](#Notification)
@@ -439,7 +440,7 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
   }
   }
   ```
   ```
 
 
-    * `id`Block ID
+    * `id`: Block ID
 * Return value
 * Return value
 
 
   ```json
   ```json
@@ -854,7 +855,7 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
     * `isDir`: whether to create a folder, when `true` only create a folder, ignore `file`
     * `isDir`: whether to create a folder, when `true` only create a folder, ignore `file`
     * `modTime`: last access and modification time, Unix time
     * `modTime`: last access and modification time, Unix time
     * `file`: the uploaded file
     * `file`: the uploaded file
-* return value
+* Return value
 
 
    ```json
    ```json
    {
    {
@@ -864,6 +865,28 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
    }
    }
    ```
    ```
 
 
+### Remove file
+
+* `/api/file/removeFile`
+* Parameters
+
+  ```json
+  {
+    "path": "/data/20210808180117-6v0mkxr/20200923234011-ieuun1p.sy"
+  }
+  ```
+  * `path`: the file path under the workspace path
+* Return value
+
+  ```json
+  {
+    "code": 0,
+    "msg": "",
+    "data": null
+  }
+  ```
+
+
 ## Export
 ## Export
 
 
 ### Export Markdown
 ### Export Markdown
@@ -946,7 +969,7 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
     }
     }
   }
   }
   ```
   ```
-    * `id`Message ID
+    * `id`: Message ID
 
 
 ## System
 ## System
 
 

+ 22 - 0
API_zh_CN.md

@@ -38,6 +38,7 @@
 * [文件](#文件)
 * [文件](#文件)
     * [获取文件](#获取文件)
     * [获取文件](#获取文件)
     * [写入文件](#写入文件)
     * [写入文件](#写入文件)
+    * [删除文件](#删除文件)
 * [导出](#导出)
 * [导出](#导出)
     * [导出 Markdown 文本](#导出-markdown-文本)
     * [导出 Markdown 文本](#导出-markdown-文本)
 * [通知](#通知)
 * [通知](#通知)
@@ -858,6 +859,27 @@
   }
   }
   ```
   ```
 
 
+### 删除文件
+
+* `/api/file/removeFile`
+* 参数
+
+  ```json
+  {
+    "path": "/data/20210808180117-6v0mkxr/20200923234011-ieuun1p.sy"
+  }
+  ```
+  * `path`:工作空间路径下的文件路径
+* 返回值
+
+  ```json
+  {
+    "code": 0,
+    "msg": "",
+    "data": null
+  }
+  ```
+
 ## 导出
 ## 导出
 
 
 ### 导出 Markdown 文本
 ### 导出 Markdown 文本

+ 27 - 0
kernel/api/file.go

@@ -110,6 +110,33 @@ func getFile(c *gin.Context) {
 	}
 	}
 }
 }
 
 
+func removeFile(c *gin.Context) {
+	ret := gulu.Ret.NewResult()
+	arg, ok := util.JsonArg(c, ret)
+	if !ok {
+		c.JSON(http.StatusOK, ret)
+		return
+	}
+
+	filePath := arg["path"].(string)
+	filePath = filepath.Join(util.WorkspaceDir, filePath)
+	_, err := os.Stat(filePath)
+	if os.IsNotExist(err) {
+		c.Status(404)
+		return
+	}
+	if nil != err {
+		logging.LogErrorf("stat [%s] failed: %s", filePath, err)
+		c.Status(500)
+		return
+	}
+
+	if err = os.RemoveAll(filePath); nil != err {
+		c.Status(500)
+		return
+	}
+}
+
 func putFile(c *gin.Context) {
 func putFile(c *gin.Context) {
 	ret := gulu.Ret.NewResult()
 	ret := gulu.Ret.NewResult()
 	defer c.JSON(http.StatusOK, ret)
 	defer c.JSON(http.StatusOK, ret)

+ 1 - 0
kernel/api/router.go

@@ -175,6 +175,7 @@ func ServeAPI(ginServer *gin.Engine) {
 	ginServer.Handle("POST", "/api/file/getFile", model.CheckAuth, getFile)
 	ginServer.Handle("POST", "/api/file/getFile", model.CheckAuth, getFile)
 	ginServer.Handle("POST", "/api/file/putFile", model.CheckAuth, model.CheckReadonly, putFile)
 	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/copyFile", model.CheckAuth, model.CheckReadonly, copyFile)
+	ginServer.Handle("POST", "/api/file/removeFile", model.CheckAuth, model.CheckReadonly, removeFile)
 
 
 	ginServer.Handle("POST", "/api/ref/refreshBacklink", model.CheckAuth, refreshBacklink)
 	ginServer.Handle("POST", "/api/ref/refreshBacklink", model.CheckAuth, refreshBacklink)
 	ginServer.Handle("POST", "/api/ref/getBacklink", model.CheckAuth, getBacklink)
 	ginServer.Handle("POST", "/api/ref/getBacklink", model.CheckAuth, getBacklink)