Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: 桌面端支持同时打开多个工作空间 https://github.com/siyuan-note/siyuan/issues/4567

Liang Ding29c8b67a7d86
diff --git a/kernel/api/workspace.go b/kernel/api/workspace.go
index b1b5970fb7..73195d6a71 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
+}