|
@@ -17,7 +17,6 @@
|
|
package api
|
|
package api
|
|
|
|
|
|
import (
|
|
import (
|
|
- "errors"
|
|
|
|
"fmt"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http"
|
|
"os"
|
|
"os"
|
|
@@ -29,7 +28,6 @@ import (
|
|
"github.com/88250/gulu"
|
|
"github.com/88250/gulu"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gofrs/flock"
|
|
"github.com/gofrs/flock"
|
|
- "github.com/siyuan-note/logging"
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/model"
|
|
"github.com/siyuan-note/siyuan/kernel/model"
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
|
)
|
|
)
|
|
@@ -64,7 +62,7 @@ func createWorkspaceDir(c *gin.Context) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- workspacePaths, err := readWorkspacePaths()
|
|
|
|
|
|
+ workspacePaths, err := util.ReadWorkspacePaths()
|
|
if nil != err {
|
|
if nil != err {
|
|
ret.Code = -1
|
|
ret.Code = -1
|
|
ret.Msg = err.Error()
|
|
ret.Msg = err.Error()
|
|
@@ -73,7 +71,7 @@ func createWorkspaceDir(c *gin.Context) {
|
|
|
|
|
|
workspacePaths = append(workspacePaths, absPath)
|
|
workspacePaths = append(workspacePaths, absPath)
|
|
|
|
|
|
- if err = writeWorkspacePaths(workspacePaths); nil != err {
|
|
|
|
|
|
+ if err = util.WriteWorkspacePaths(workspacePaths); nil != err {
|
|
ret.Code = -1
|
|
ret.Code = -1
|
|
ret.Msg = err.Error()
|
|
ret.Msg = err.Error()
|
|
return
|
|
return
|
|
@@ -91,7 +89,7 @@ func removeWorkspaceDir(c *gin.Context) {
|
|
|
|
|
|
path := arg["path"].(string)
|
|
path := arg["path"].(string)
|
|
|
|
|
|
- workspacePaths, err := readWorkspacePaths()
|
|
|
|
|
|
+ workspacePaths, err := util.ReadWorkspacePaths()
|
|
if nil != err {
|
|
if nil != err {
|
|
ret.Code = -1
|
|
ret.Code = -1
|
|
ret.Msg = err.Error()
|
|
ret.Msg = err.Error()
|
|
@@ -100,7 +98,7 @@ func removeWorkspaceDir(c *gin.Context) {
|
|
|
|
|
|
workspacePaths = gulu.Str.RemoveElem(workspacePaths, path)
|
|
workspacePaths = gulu.Str.RemoveElem(workspacePaths, path)
|
|
|
|
|
|
- if err = writeWorkspacePaths(workspacePaths); nil != err {
|
|
|
|
|
|
+ if err = util.WriteWorkspacePaths(workspacePaths); nil != err {
|
|
ret.Code = -1
|
|
ret.Code = -1
|
|
ret.Msg = err.Error()
|
|
ret.Msg = err.Error()
|
|
return
|
|
return
|
|
@@ -120,7 +118,7 @@ func getWorkspaces(c *gin.Context) {
|
|
ret := gulu.Ret.NewResult()
|
|
ret := gulu.Ret.NewResult()
|
|
defer c.JSON(http.StatusOK, ret)
|
|
defer c.JSON(http.StatusOK, ret)
|
|
|
|
|
|
- workspacePaths, err := readWorkspacePaths()
|
|
|
|
|
|
+ workspacePaths, err := util.ReadWorkspacePaths()
|
|
if nil != err {
|
|
if nil != err {
|
|
ret.Code = -1
|
|
ret.Code = -1
|
|
ret.Msg = err.Error()
|
|
ret.Msg = err.Error()
|
|
@@ -169,7 +167,7 @@ func setWorkspaceDir(c *gin.Context) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- workspacePaths, err := readWorkspacePaths()
|
|
|
|
|
|
+ workspacePaths, err := util.ReadWorkspacePaths()
|
|
if nil != err {
|
|
if nil != err {
|
|
ret.Code = -1
|
|
ret.Code = -1
|
|
ret.Msg = err.Error()
|
|
ret.Msg = err.Error()
|
|
@@ -181,7 +179,7 @@ func setWorkspaceDir(c *gin.Context) {
|
|
workspacePaths = gulu.Str.RemoveElem(workspacePaths, path)
|
|
workspacePaths = gulu.Str.RemoveElem(workspacePaths, path)
|
|
workspacePaths = append(workspacePaths, path) // 切换的工作空间固定放在最后一个
|
|
workspacePaths = append(workspacePaths, path) // 切换的工作空间固定放在最后一个
|
|
|
|
|
|
- if err = writeWorkspacePaths(workspacePaths); nil != err {
|
|
|
|
|
|
+ if err = util.WriteWorkspacePaths(workspacePaths); nil != err {
|
|
ret.Code = -1
|
|
ret.Code = -1
|
|
ret.Msg = err.Error()
|
|
ret.Msg = err.Error()
|
|
return
|
|
return
|
|
@@ -194,45 +192,6 @@ func setWorkspaceDir(c *gin.Context) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-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
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func isInvalidWorkspacePath(absPath string) bool {
|
|
func isInvalidWorkspacePath(absPath string) bool {
|
|
if "" == absPath {
|
|
if "" == absPath {
|
|
return true
|
|
return true
|