Skip to content

Commit

Permalink
🎨 Add internal kernel API /api/file/globalCopyFiles #10614
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Mar 18, 2024
1 parent 8287631 commit 27c55b9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
39 changes: 39 additions & 0 deletions kernel/api/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package api

import (
"fmt"
"io"
"mime"
"mime/multipart"
Expand All @@ -35,6 +36,44 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)

func globalCopyFiles(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)

arg, ok := util.JsonArg(c, ret)
if !ok {
return
}

var srcs []string
srcsArg := arg["srcs"].([]interface{})
for _, s := range srcsArg {
srcs = append(srcs, s.(string))
}

for _, src := range srcs {
if !filelock.IsExist(src) {
msg := fmt.Sprintf("file [%s] does not exist", src)
logging.LogErrorf(msg)
ret.Code = -1
ret.Msg = msg
return
}
}

destDir := arg["destDir"].(string) // 相对于工作空间的路径
destDir = filepath.Join(util.WorkspaceDir, destDir)
for _, src := range srcs {
dest := filepath.Join(destDir, filepath.Base(src))
if err := filelock.Copy(src, dest); nil != err {
logging.LogErrorf("copy file [%s] to [%s] failed: %s", src, dest, err)
ret.Code = -1
ret.Msg = err.Error()
return
}
}
}

func copyFile(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
Expand Down
1 change: 1 addition & 0 deletions kernel/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func ServeAPI(ginServer *gin.Engine) {
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/copyFile", model.CheckAuth, model.CheckReadonly, copyFile)
ginServer.Handle("POST", "/api/file/globalCopyFiles", model.CheckAuth, model.CheckReadonly, globalCopyFiles)
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, readDir)
Expand Down

0 comments on commit 27c55b9

Please sign in to comment.