瀏覽代碼

Merge remote-tracking branch 'origin/dev' into dev

Vanessa 2 年之前
父節點
當前提交
20e2e7b41a
共有 4 個文件被更改,包括 212 次插入63 次删除
  1. 5 2
      kernel/api/router.go
  2. 97 0
      kernel/api/storage.go
  3. 0 61
      kernel/api/system.go
  4. 110 0
      kernel/model/localstorage.go

+ 5 - 2
kernel/api/router.go

@@ -53,8 +53,11 @@ func ServeAPI(ginServer *gin.Engine) {
 	ginServer.Handle("POST", "/api/system/getConf", model.CheckAuth, getConf)
 	ginServer.Handle("POST", "/api/system/getConf", model.CheckAuth, getConf)
 	ginServer.Handle("POST", "/api/system/checkUpdate", model.CheckAuth, checkUpdate)
 	ginServer.Handle("POST", "/api/system/checkUpdate", model.CheckAuth, checkUpdate)
 	ginServer.Handle("POST", "/api/system/exportLog", model.CheckAuth, exportLog)
 	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/storage/setLocalStorage", model.CheckAuth, setLocalStorage)
+	ginServer.Handle("POST", "/api/storage/getLocalStorage", model.CheckAuth, getLocalStorage)
+	ginServer.Handle("POST", "/api/storage/setLocalStorageVal", model.CheckAuth, setLocalStorageVal)
+	ginServer.Handle("POST", "/api/storage/removeLocalStorageVal", model.CheckAuth, removeLocalStorageVal)
 
 
 	ginServer.Handle("POST", "/api/account/login", model.CheckAuth, login)
 	ginServer.Handle("POST", "/api/account/login", model.CheckAuth, login)
 	ginServer.Handle("POST", "/api/account/checkActivationcode", model.CheckAuth, checkActivationcode)
 	ginServer.Handle("POST", "/api/account/checkActivationcode", model.CheckAuth, checkActivationcode)

+ 97 - 0
kernel/api/storage.go

@@ -0,0 +1,97 @@
+// SiYuan - Build Your Eternal Digital Garden
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+package api
+
+import (
+	"net/http"
+
+	"github.com/88250/gulu"
+	"github.com/gin-gonic/gin"
+	"github.com/siyuan-note/siyuan/kernel/model"
+	"github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func removeLocalStorageVal(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)
+
+	err := model.RemoveLocalStorageVal(key)
+	if nil != err {
+		ret.Code = -1
+		ret.Msg = err.Error()
+		return
+	}
+}
+
+func setLocalStorageVal(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)
+	val := arg["val"].(interface{})
+
+	err := model.SetLocalStorageVal(key, val)
+	if nil != err {
+		ret.Code = -1
+		ret.Msg = err.Error()
+		return
+	}
+}
+
+func getLocalStorage(c *gin.Context) {
+	ret := gulu.Ret.NewResult()
+	defer c.JSON(http.StatusOK, ret)
+
+	data, err := model.GetLocalStorage()
+	if nil != err {
+		ret.Code = -1
+		ret.Msg = err.Error()
+		return
+	}
+	ret.Data = data
+}
+
+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{})
+
+	err := model.SetLocalStorage(val)
+	if nil != err {
+		ret.Code = -1
+		ret.Msg = err.Error()
+		return
+	}
+}

+ 0 - 61
kernel/api/system.go

@@ -26,7 +26,6 @@ import (
 
 
 	"github.com/88250/gulu"
 	"github.com/88250/gulu"
 	"github.com/gin-gonic/gin"
 	"github.com/gin-gonic/gin"
-	"github.com/siyuan-note/filelock"
 	"github.com/siyuan-note/logging"
 	"github.com/siyuan-note/logging"
 	"github.com/siyuan-note/siyuan/kernel/conf"
 	"github.com/siyuan-note/siyuan/kernel/conf"
 	"github.com/siyuan-note/siyuan/kernel/model"
 	"github.com/siyuan-note/siyuan/kernel/model"
@@ -164,66 +163,6 @@ func getConf(c *gin.Context) {
 	}
 	}
 }
 }
 
 
-func getLocalStorage(c *gin.Context) {
-	ret := gulu.Ret.NewResult()
-	defer c.JSON(http.StatusOK, ret)
-
-	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
-	}
-
-	ret.Data = ls
-}
-
-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) {
 func setUILayout(c *gin.Context) {
 	ret := gulu.Ret.NewResult()
 	ret := gulu.Ret.NewResult()
 	defer c.JSON(http.StatusOK, ret)
 	defer c.JSON(http.StatusOK, ret)

+ 110 - 0
kernel/model/localstorage.go

@@ -0,0 +1,110 @@
+// SiYuan - Build Your Eternal Digital Garden
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+package model
+
+import (
+	"os"
+	"path/filepath"
+	"sync"
+
+	"github.com/88250/gulu"
+	"github.com/siyuan-note/filelock"
+	"github.com/siyuan-note/logging"
+	"github.com/siyuan-note/siyuan/kernel/util"
+)
+
+var localStorageLock = sync.Mutex{}
+
+func RemoveLocalStorageVal(key string) (err error) {
+	localStorageLock.Lock()
+	defer localStorageLock.Unlock()
+
+	localStorage, err := getLocalStorage()
+	if nil != err {
+		return
+	}
+
+	delete(localStorage, key)
+	return
+}
+
+func SetLocalStorageVal(key string, val interface{}) (err error) {
+	localStorageLock.Lock()
+	defer localStorageLock.Unlock()
+
+	localStorage, err := getLocalStorage()
+	if nil != err {
+		return
+	}
+
+	localStorage[key] = val
+	return setLocalStorage(localStorage)
+}
+
+func SetLocalStorage(val interface{}) (err error) {
+	localStorageLock.Lock()
+	defer localStorageLock.Unlock()
+	return setLocalStorage(val)
+}
+
+func GetLocalStorage() (ret map[string]interface{}, err error) {
+	localStorageLock.Lock()
+	defer localStorageLock.Unlock()
+	return getLocalStorage()
+}
+
+func setLocalStorage(val interface{}) (err error) {
+	dirPath := filepath.Join(util.DataDir, "storage")
+	if err = os.MkdirAll(dirPath, 0755); nil != err {
+		logging.LogErrorf("create local storage dir failed: %s", err)
+		return
+	}
+
+	data, err := gulu.JSON.MarshalJSON(val)
+	if nil != err {
+		logging.LogErrorf("marshal local storage failed: %s", err)
+		return
+	}
+
+	lsPath := filepath.Join(dirPath, "local.json")
+	err = filelock.WriteFile(lsPath, data)
+	if nil != err {
+		logging.LogErrorf("write local storage failed: %s", err)
+		return
+	}
+	return
+}
+
+func getLocalStorage() (ret map[string]interface{}, err error) {
+	lsPath := filepath.Join(util.DataDir, "storage/local.json")
+	if !gulu.File.IsExist(lsPath) {
+		return
+	}
+
+	data, err := filelock.ReadFile(lsPath)
+	if nil != err {
+		logging.LogErrorf("read local storage failed: %s", err)
+		return
+	}
+
+	ret = map[string]interface{}{}
+	if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
+		logging.LogErrorf("unmarshal local storage failed: %s", err)
+		return
+	}
+	return
+}