Sfoglia il codice sorgente

:art: Add kernel API `/api/file/renameFile` https://github.com/siyuan-note/siyuan/issues/8328

Daniel 2 anni fa
parent
commit
e4229f2bad
6 ha cambiato i file con 82 aggiunte e 1 eliminazioni
  1. 24 0
      API.md
  2. 24 0
      API_zh_CN.md
  3. 30 0
      kernel/api/file.go
  4. 1 0
      kernel/api/router.go
  5. 1 1
      kernel/go.mod
  6. 2 0
      kernel/go.sum

+ 24 - 0
API.md

@@ -42,6 +42,7 @@
     * [Get file](#Get-file)
     * [Put file](#Put-file)
     * [Remove file](#Remove-file)
+    * [Rename file](#Rename-file)
     * [List files](#List-files)
 * [Export](#Export)
     * [Export Markdown](#Export-Markdown)
@@ -1003,6 +1004,29 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
   }
   ```
 
+### Rename file
+
+* `/api/file/renameFile`
+* Parameters
+
+  ```json
+  {
+    "path": "/data/assets/image-20230523085812-k3o9t32.png",
+    "newPath": "/data/assets/test-20230523085812-k3o9t32.png"
+  }
+  ```
+  * `path`: the file path under the workspace path
+  * `newPath`: the new file path under the workspace path
+* Return value
+
+  ```json
+  {
+    "code": 0,
+    "msg": "",
+    "data": null
+  }
+  ```
+
 ### List files
 
 * `/api/file/readDir`

+ 24 - 0
API_zh_CN.md

@@ -42,6 +42,7 @@
     * [获取文件](#获取文件)
     * [写入文件](#写入文件)
     * [删除文件](#删除文件)
+    * [重命名文件](#重命名文件)
     * [列出文件](#列出文件)
 * [导出](#导出)
     * [导出 Markdown 文本](#导出-markdown-文本)
@@ -995,6 +996,29 @@
   }
   ```
 
+### 重命名文件
+
+* `/api/file/renameFile`
+* 参数
+
+  ```json
+  {
+    "path": "/data/assets/image-20230523085812-k3o9t32.png",
+    "newPath": "/data/assets/test-20230523085812-k3o9t32.png"
+  }
+  ```
+  * `path`:工作空间路径下的文件路径
+  * `newPath`:新的文件路径
+* 返回值
+
+  ```json
+  {
+    "code": 0,
+    "msg": "",
+    "data": null
+  }
+  ```
+
 ### 列出文件
 
 * `/api/file/readDir`

+ 30 - 0
kernel/api/file.go

@@ -171,6 +171,36 @@ func readDir(c *gin.Context) {
 	ret.Data = files
 }
 
+func renameFile(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
+	}
+
+	filePath := arg["path"].(string)
+	newPath := arg["newPath"].(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 = filelock.Rename(filePath, newPath); nil != err {
+		c.Status(500)
+		return
+	}
+}
+
 func removeFile(c *gin.Context) {
 	ret := gulu.Ret.NewResult()
 	defer c.JSON(http.StatusOK, ret)

+ 1 - 0
kernel/api/router.go

@@ -179,6 +179,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/renameFile", model.CheckAuth, model.CheckReadonly, renameFile)
 	ginServer.Handle("POST", "/api/file/readDir", model.CheckAuth, model.CheckReadonly, readDir)
 
 	ginServer.Handle("POST", "/api/ref/refreshBacklink", model.CheckAuth, refreshBacklink)

+ 1 - 1
kernel/go.mod

@@ -47,7 +47,7 @@ require (
 	github.com/siyuan-note/dejavu v0.0.0-20230501032540-9d7760329f9d
 	github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75
 	github.com/siyuan-note/eventbus v0.0.0-20230216103454-41885eac6c2b
-	github.com/siyuan-note/filelock v0.0.0-20230501032014-b981a05568ef
+	github.com/siyuan-note/filelock v0.0.0-20230523004741-d9121740f638
 	github.com/siyuan-note/httpclient v0.0.0-20230501032226-9e9018416f53
 	github.com/siyuan-note/logging v0.0.0-20230327073243-ebe83aec1493
 	github.com/siyuan-note/riff v0.0.0-20230516073320-b440b8feaabd

+ 2 - 0
kernel/go.sum

@@ -291,6 +291,8 @@ github.com/siyuan-note/eventbus v0.0.0-20230216103454-41885eac6c2b h1:828lTUW2C0
 github.com/siyuan-note/eventbus v0.0.0-20230216103454-41885eac6c2b/go.mod h1:Sqo4FYX5lAXu7gWkbEdJF0e6P57tNNVV4WDKYDctokI=
 github.com/siyuan-note/filelock v0.0.0-20230501032014-b981a05568ef h1:ZNJKZ+inFBk2k4PnMT8XZLIyfhzTRd29oOVOe+dg0kE=
 github.com/siyuan-note/filelock v0.0.0-20230501032014-b981a05568ef/go.mod h1:lZ/T8iztsw7BIR4MJxLhsC1tnb81o8r8b8JstcpUkr0=
+github.com/siyuan-note/filelock v0.0.0-20230523004741-d9121740f638 h1:0dvT+1FvuUf6/GFkFRQML0Q3DLulbswj1GJJ0j7BlD8=
+github.com/siyuan-note/filelock v0.0.0-20230523004741-d9121740f638/go.mod h1:v2bpUtjvsvt1TRHgly1mIXQjAEo/t50dOM9YiC813qQ=
 github.com/siyuan-note/httpclient v0.0.0-20230501032226-9e9018416f53 h1:CXYTR4QhuibnTYkvDuLQsTnAQiq0Vd8J7ukRXyqrJPs=
 github.com/siyuan-note/httpclient v0.0.0-20230501032226-9e9018416f53/go.mod h1:S/pXlPZYCJTOZjmdmQyVga//24x3XEM+MG8vIYO26gw=
 github.com/siyuan-note/logging v0.0.0-20230327073243-ebe83aec1493 h1:oaN5b0WDFkjdBgGxmmBnMrtZxaJ76LZLwhQSZnznJMI=