From 29c8b67a7d8659d0e57c232cf60bae3621c02b4d Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Thu, 5 Jan 2023 19:16:16 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=A1=8C=E9=9D=A2=E7=AB=AF=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=90=8C=E6=97=B6=E6=89=93=E5=BC=80=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E7=A9=BA=E9=97=B4=20https://github.com/siyua?= =?UTF-8?q?n-note/siyuan/issues/4567?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/api/workspace.go | 114 ++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 45 deletions(-) diff --git a/kernel/api/workspace.go b/kernel/api/workspace.go index b1b5970fb..73195d6a7 100644 --- a/kernel/api/workspace.go +++ b/kernel/api/workspace.go @@ -17,6 +17,7 @@ package api import ( + "errors" "fmt" "net/http" "os" @@ -53,6 +54,20 @@ func createWorkspaceDir(c *gin.Context) { return } + workspacePaths, err := readWorkspacePaths() + if nil != err { + ret.Code = -1 + ret.Msg = err.Error() + return + } + + workspacePaths = append(workspacePaths, path) + + if err = writeWorkspacePaths(workspacePaths); nil != err { + ret.Code = -1 + ret.Msg = err.Error() + return + } } func removeWorkspaceDir(c *gin.Context) { @@ -66,29 +81,18 @@ func removeWorkspaceDir(c *gin.Context) { path := arg["path"].(string) - var workspacePaths []string - workspaceConf := filepath.Join(util.HomeDir, ".config", "siyuan", "workspace.json") - data, err := os.ReadFile(workspaceConf) + workspacePaths, err := readWorkspacePaths() if nil != err { - logging.LogErrorf("read workspace conf failed: %s", err) - } else { - if err = gulu.JSON.UnmarshalJSON(data, &workspacePaths); nil != err { - logging.LogErrorf("unmarshal workspace conf failed: %s", err) - } - } - - workspacePaths = gulu.Str.RemoveElem(workspacePaths, path) - if data, err = gulu.JSON.MarshalJSON(workspacePaths); nil != err { - msg := fmt.Sprintf("marshal workspace conf [%s] failed: %s", workspaceConf, err) ret.Code = -1 - ret.Msg = msg + ret.Msg = err.Error() return } - if err = gulu.File.WriteFileSafer(workspaceConf, data, 0644); nil != err { - msg := fmt.Sprintf("write workspace conf [%s] failed: %s", workspaceConf, err) + workspacePaths = gulu.Str.RemoveElem(workspacePaths, path) + + if err = writeWorkspacePaths(workspacePaths); nil != err { ret.Code = -1 - ret.Msg = msg + ret.Msg = err.Error() return } @@ -108,17 +112,10 @@ func listWorkspaceDirs(c *gin.Context) { ret := gulu.Ret.NewResult() defer c.JSON(http.StatusOK, ret) - userHomeConfDir := filepath.Join(util.HomeDir, ".config", "siyuan") - workspaceConf := filepath.Join(userHomeConfDir, "workspace.json") - data, err := os.ReadFile(workspaceConf) + workspacePaths, err := readWorkspacePaths() if nil != err { - logging.LogErrorf("read workspace conf [%s] failed: %s", workspaceConf, err) - return - } - - var workspacePaths []string - if err = gulu.JSON.UnmarshalJSON(data, &workspacePaths); nil != err { - logging.LogErrorf("unmarshal workspace conf [%s] failed: %s", workspaceConf, err) + ret.Code = -1 + ret.Msg = err.Error() return } ret.Data = workspacePaths @@ -151,15 +148,11 @@ func setWorkspaceDir(c *gin.Context) { } } - var workspacePaths []string - workspaceConf := filepath.Join(util.HomeDir, ".config", "siyuan", "workspace.json") - data, err := os.ReadFile(workspaceConf) + workspacePaths, err := readWorkspacePaths() if nil != err { - logging.LogErrorf("read workspace conf failed: %s", err) - } else { - if err = gulu.JSON.UnmarshalJSON(data, &workspacePaths); nil != err { - logging.LogErrorf("unmarshal workspace conf failed: %s", err) - } + ret.Code = -1 + ret.Msg = err.Error() + return } workspacePaths = append(workspacePaths, path) @@ -167,18 +160,10 @@ func setWorkspaceDir(c *gin.Context) { workspacePaths = gulu.Str.RemoveElem(workspacePaths, path) workspacePaths = append(workspacePaths, path) // 切换的工作空间固定放在最后一个 - if data, err = gulu.JSON.MarshalJSON(workspacePaths); nil != err { - msg := fmt.Sprintf("marshal workspace conf [%s] failed: %s", workspaceConf, err) + if err = writeWorkspacePaths(workspacePaths); nil != err { ret.Code = -1 - ret.Msg = msg + ret.Msg = err.Error() return - } else { - if err = gulu.File.WriteFileSafer(workspaceConf, data, 0644); nil != err { - msg := fmt.Sprintf("create workspace conf [%s] failed: %s", workspaceConf, err) - ret.Code = -1 - ret.Msg = msg - return - } } if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container { @@ -187,3 +172,42 @@ func setWorkspaceDir(c *gin.Context) { model.Close(false, 1) } } + +func readWorkspacePaths() (ret []string, err error) { + ret = []string{} + workspaceConf := filepath.Join(util.HomeDir, ".config", "siyuan", "workspace.json") + data, err := os.ReadFile(workspaceConf) + if nil != err { + msg := fmt.Sprintf("read workspace conf [%s] failed: %s", workspaceConf, err) + logging.LogErrorf(msg) + err = errors.New(msg) + return + } + + if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err { + msg := fmt.Sprintf("unmarshal workspace conf [%s] failed: %s", workspaceConf, err) + logging.LogErrorf(msg) + err = errors.New(msg) + return + } + return +} + +func writeWorkspacePaths(workspacePaths []string) (err error) { + workspaceConf := filepath.Join(util.HomeDir, ".config", "siyuan", "workspace.json") + data, err := gulu.JSON.MarshalJSON(workspacePaths) + if nil != err { + msg := fmt.Sprintf("marshal workspace conf [%s] failed: %s", workspaceConf, err) + logging.LogErrorf(msg) + err = errors.New(msg) + return + } + + if err = os.WriteFile(workspaceConf, data, 0644); nil != err { + msg := fmt.Sprintf("write workspace conf [%s] failed: %s", workspaceConf, err) + logging.LogErrorf(msg) + err = errors.New(msg) + return + } + return +}