mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-02 20:11:53 +08:00

* 🐛 fix #5857 * 🎨 #5990 桌面端 `SiYuan` 可执行程序支持使用参数连接非本机内核服务 * 🎨 style * 🎨 style * 🎨 style * 🎨 改进伺服代码片段 `/snippets/` #6356 * 🎨 调整 win 安装包打包选项 * 🎨 鉴权支持 IPv6 本机回环地址 * 🎨 支持 HEVC 编码视频播放 * 🎨 保存 `data/storage/local.json` 时格式化
111 lines
2.8 KiB
Go
111 lines
2.8 KiB
Go
// 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 setLocalStorage(localStorage)
|
|
}
|
|
|
|
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.MarshalIndentJSON(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
|
|
}
|