Skip to content

Commit

Permalink
🎨 Support export and import settings #10617
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Sep 25, 2024
1 parent 1f47be1 commit 2deb986
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 7 deletions.
1 change: 1 addition & 0 deletions kernel/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/system/getChangelog", model.CheckAuth, getChangelog)
ginServer.Handle("POST", "/api/system/getNetwork", model.CheckAuth, model.CheckAdminRole, getNetwork)
ginServer.Handle("POST", "/api/system/exportConf", model.CheckAuth, model.CheckAdminRole, exportConf)
ginServer.Handle("POST", "/api/system/importConf", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, importConf)

ginServer.Handle("POST", "/api/storage/setLocalStorage", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, setLocalStorage)
ginServer.Handle("POST", "/api/storage/getLocalStorage", model.CheckAuth, getLocalStorage)
Expand Down
104 changes: 99 additions & 5 deletions kernel/api/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
package api

import (
"github.com/jinzhu/copier"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/88250/lute"

"github.com/88250/gulu"
"github.com/88250/lute"
"github.com/gin-gonic/gin"
"github.com/jinzhu/copier"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/conf"
"github.com/siyuan-note/siyuan/kernel/model"
Expand Down Expand Up @@ -211,17 +211,19 @@ func exportConf(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)

logging.LogInfof("exporting conf...")

name := "siyuan-conf-" + time.Now().Format("20060102150405") + ".json"
tmpDir := filepath.Join(util.TempDir, "export")
if err := os.MkdirAll(tmpDir, 0755); err != nil {
logging.LogErrorf("export WebDAV provider failed: %s", err)
logging.LogErrorf("export conf failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
return
}

clonedConf := &model.AppConf{}
if err := copier.Copy(clonedConf, model.Conf); err != nil {
if err := copier.CopyWithOption(clonedConf, model.Conf, copier.Option{IgnoreEmpty: false, DeepCopy: true}); err != nil {
logging.LogErrorf("export conf failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
Expand Down Expand Up @@ -291,13 +293,105 @@ func exportConf(c *gin.Context) {
return
}

logging.LogInfof("exported conf")

zipPath := "/export/" + name + ".zip"
ret.Data = map[string]interface{}{
"name": name,
"zip": zipPath,
}
}

func importConf(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(200, ret)

logging.LogInfof("importing conf...")

form, err := c.MultipartForm()
if err != nil {
logging.LogErrorf("read upload file failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
return
}

files := form.File["file"]
if 1 != len(files) {
ret.Code = -1
ret.Msg = "invalid upload file"
return
}

f := files[0]
fh, err := f.Open()
if err != nil {
logging.LogErrorf("read upload file failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
return
}

data, err := io.ReadAll(fh)
fh.Close()
if err != nil {
logging.LogErrorf("read upload file failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
return
}

tmpDir := filepath.Join(util.TempDir, "import")
if err = os.MkdirAll(tmpDir, 0755); err != nil {
logging.LogErrorf("import conf failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
return
}

tmp := filepath.Join(tmpDir, f.Filename)
if err = os.WriteFile(tmp, data, 0644); err != nil {
logging.LogErrorf("import conf failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
return
}

if err = gulu.Zip.Unzip(tmp, tmpDir); err != nil {
logging.LogErrorf("import conf failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
return
}

tmp = filepath.Join(tmpDir, f.Filename[:len(f.Filename)-4])
data, err = os.ReadFile(tmp)
if err != nil {
logging.LogErrorf("import conf failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
return
}

importedConf := model.NewAppConf()
if err = gulu.JSON.UnmarshalJSON(data, importedConf); err != nil {
logging.LogErrorf("import conf failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
return
}

if err = copier.CopyWithOption(model.Conf, importedConf, copier.Option{IgnoreEmpty: true, DeepCopy: true}); err != nil {
logging.LogErrorf("import conf failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
return
}

logging.LogInfof("imported conf")
model.Close(false, true, 1)
}

func getConf(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
Expand Down
8 changes: 6 additions & 2 deletions kernel/model/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var Conf *AppConf

// AppConf 维护应用元数据,保存在 ~/.siyuan/conf.json。
type AppConf struct {
LogLevel string `json:"logLevel"` // 日志级别:Off, Trace, Debug, Info, Warn, Error, Fatal
LogLevel string `json:"logLevel"` // 日志级别:off, trace, debug, info, warn, error, fatal
Appearance *conf.Appearance `json:"appearance"` // 外观
Langs []*conf.Lang `json:"langs"` // 界面语言列表
Lang string `json:"lang"` // 选择的界面语言,同 Appearance.Lang
Expand Down Expand Up @@ -87,6 +87,10 @@ type AppConf struct {
m *sync.Mutex
}

func NewAppConf() *AppConf {
return &AppConf{LogLevel: "debug", m: &sync.Mutex{}}
}

func (conf *AppConf) GetUILayout() *conf.UILayout {
conf.m.Lock()
defer conf.m.Unlock()
Expand Down Expand Up @@ -114,7 +118,7 @@ func (conf *AppConf) SetUser(user *conf.User) {
func InitConf() {
initLang()

Conf = &AppConf{LogLevel: "debug", m: &sync.Mutex{}}
Conf = NewAppConf()
confPath := filepath.Join(util.ConfDir, "conf.json")
if gulu.File.IsExist(confPath) {
if data, err := os.ReadFile(confPath); err != nil {
Expand Down

0 comments on commit 2deb986

Please sign in to comment.