|
@@ -17,7 +17,7 @@
|
|
|
package api
|
|
|
|
|
|
import (
|
|
|
- "github.com/jinzhu/copier"
|
|
|
+ "io"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
@@ -25,10 +25,10 @@ import (
|
|
|
"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"
|
|
@@ -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()
|
|
@@ -291,6 +293,8 @@ func exportConf(c *gin.Context) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ logging.LogInfof("exported conf")
|
|
|
+
|
|
|
zipPath := "/export/" + name + ".zip"
|
|
|
ret.Data = map[string]interface{}{
|
|
|
"name": name,
|
|
@@ -298,6 +302,96 @@ func exportConf(c *gin.Context) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+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)
|