From d7796ac98b1fcdb5f45ad5bfac75ee2411f4181c Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sun, 30 Oct 2022 09:37:17 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=8C=81=E4=B9=85=E5=8C=96=20localStor?= =?UTF-8?q?age=20https://github.com/siyuan-note/siyuan/issues/6404?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/api/router.go | 2 ++ kernel/api/system.go | 69 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/kernel/api/router.go b/kernel/api/router.go index c643dc3f6..f149f83b7 100644 --- a/kernel/api/router.go +++ b/kernel/api/router.go @@ -53,6 +53,8 @@ func ServeAPI(ginServer *gin.Engine) { ginServer.Handle("POST", "/api/system/getConf", model.CheckAuth, getConf) ginServer.Handle("POST", "/api/system/checkUpdate", model.CheckAuth, checkUpdate) ginServer.Handle("POST", "/api/system/exportLog", model.CheckAuth, exportLog) + ginServer.Handle("POST", "/api/system/setLocalStorage", model.CheckAuth, setLocalStorage) + ginServer.Handle("POST", "/api/system/getLocalStorage", model.CheckAuth, getLocalStorage) ginServer.Handle("POST", "/api/account/login", model.CheckAuth, login) ginServer.Handle("POST", "/api/account/checkActivationcode", model.CheckAuth, checkActivationcode) diff --git a/kernel/api/system.go b/kernel/api/system.go index 1c9c59ce9..4581474e5 100644 --- a/kernel/api/system.go +++ b/kernel/api/system.go @@ -26,6 +26,7 @@ import ( "github.com/88250/gulu" "github.com/gin-gonic/gin" + "github.com/siyuan-note/filelock" "github.com/siyuan-note/logging" "github.com/siyuan-note/siyuan/kernel/conf" "github.com/siyuan-note/siyuan/kernel/model" @@ -163,6 +164,74 @@ func getConf(c *gin.Context) { } } +func getLocalStorage(c *gin.Context) { + ret := gulu.Ret.NewResult() + defer c.JSON(http.StatusOK, ret) + + arg, ok := util.JsonArg(c, ret) + if !ok { + return + } + + key := arg["key"].(string) + + lsPath := filepath.Join(util.DataDir, "storage/local.json") + if !gulu.File.IsExist(lsPath) { + return + } + + data, err := filelock.ReadFile(lsPath) + if nil != err { + ret.Code = -1 + ret.Msg = err.Error() + return + } + + ls := map[string]interface{}{} + if err = gulu.JSON.UnmarshalJSON(data, &ls); nil != err { + ret.Code = -1 + ret.Msg = err.Error() + return + } + + value := ls[key] + ret.Data = value +} + +func setLocalStorage(c *gin.Context) { + ret := gulu.Ret.NewResult() + defer c.JSON(http.StatusOK, ret) + + arg, ok := util.JsonArg(c, ret) + if !ok { + return + } + + val := arg["val"].(interface{}) + + dirPath := filepath.Join(util.DataDir, "storage") + if err := os.MkdirAll(dirPath, 0755); nil != err { + ret.Code = -1 + ret.Msg = err.Error() + return + } + + data, err := gulu.JSON.MarshalJSON(val) + if nil != err { + ret.Code = -1 + ret.Msg = err.Error() + return + } + + lsPath := filepath.Join(dirPath, "local.json") + err = filelock.WriteFile(lsPath, data) + if nil != err { + ret.Code = -1 + ret.Msg = err.Error() + return + } +} + func setUILayout(c *gin.Context) { ret := gulu.Ret.NewResult() defer c.JSON(http.StatusOK, ret)