123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- // SiYuan - Refactor your thinking
- // 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 (
- "github.com/88250/lute"
- "net/http"
- "os"
- "path/filepath"
- "strings"
- "sync"
- "time"
- "github.com/88250/gulu"
- "github.com/gin-gonic/gin"
- "github.com/siyuan-note/logging"
- "github.com/siyuan-note/siyuan/kernel/conf"
- "github.com/siyuan-note/siyuan/kernel/model"
- "github.com/siyuan-note/siyuan/kernel/util"
- )
- func getNetwork(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- maskedConf, err := model.GetMaskedConf()
- if nil != err {
- ret.Code = -1
- ret.Msg = "get conf failed: " + err.Error()
- return
- }
- ret.Data = map[string]interface{}{
- "proxy": maskedConf.System.NetworkProxy,
- }
- }
- func getChangelog(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- data := map[string]interface{}{"show": false, "html": ""}
- ret.Data = data
- changelogsDir := filepath.Join(util.WorkingDir, "changelogs")
- if !gulu.File.IsDir(changelogsDir) {
- return
- }
- if !model.Conf.ShowChangelog {
- return
- }
- changelogPath := filepath.Join(changelogsDir, "v"+util.Ver, "v"+util.Ver+"_"+model.Conf.Lang+".md")
- if !gulu.File.IsExist(changelogPath) {
- changelogPath = filepath.Join(changelogsDir, "v"+util.Ver, "v"+util.Ver+".md")
- if !gulu.File.IsExist(changelogPath) {
- logging.LogErrorf("changelog not found: %s", changelogPath)
- return
- }
- }
- contentData, err := os.ReadFile(changelogPath)
- if nil != err {
- logging.LogErrorf("read changelog failed: %s", err)
- return
- }
- model.Conf.ShowChangelog = false
- luteEngine := lute.New()
- htmlContent := luteEngine.MarkdownStr("", string(contentData))
- htmlContent = util.LinkTarget(htmlContent, "")
- data["show"] = true
- data["html"] = htmlContent
- ret.Data = data
- }
- func getEmojiConf(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- builtConfPath := filepath.Join(util.AppearancePath, "emojis", "conf.json")
- data, err := os.ReadFile(builtConfPath)
- if nil != err {
- logging.LogErrorf("read emojis conf.json failed: %s", err)
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- var conf []map[string]interface{}
- if err = gulu.JSON.UnmarshalJSON(data, &conf); nil != err {
- logging.LogErrorf("unmarshal emojis conf.json failed: %s", err)
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- customConfDir := filepath.Join(util.DataDir, "emojis")
- custom := map[string]interface{}{
- "id": "custom",
- "title": "Custom",
- "title_zh_cn": "自定义",
- }
- items := []map[string]interface{}{}
- custom["items"] = items
- if gulu.File.IsDir(customConfDir) {
- model.CustomEmojis = sync.Map{}
- customEmojis, err := os.ReadDir(customConfDir)
- if nil != err {
- logging.LogErrorf("read custom emojis failed: %s", err)
- } else {
- for _, customEmoji := range customEmojis {
- name := customEmoji.Name()
- if strings.HasPrefix(name, ".") {
- continue
- }
- if customEmoji.IsDir() {
- // 子级
- subCustomEmojis, err := os.ReadDir(filepath.Join(customConfDir, name))
- if nil != err {
- logging.LogErrorf("read custom emojis failed: %s", err)
- continue
- }
- for _, subCustomEmoji := range subCustomEmojis {
- if subCustomEmoji.IsDir() {
- continue
- }
- name = subCustomEmoji.Name()
- if strings.HasPrefix(name, ".") {
- continue
- }
- addCustomEmoji(customEmoji.Name()+"/"+name, &items)
- }
- continue
- }
- addCustomEmoji(name, &items)
- }
- }
- }
- custom["items"] = items
- conf = append([]map[string]interface{}{custom}, conf...)
- ret.Data = conf
- return
- }
- func addCustomEmoji(name string, items *[]map[string]interface{}) {
- ext := filepath.Ext(name)
- nameWithoutExt := strings.TrimSuffix(name, ext)
- emoji := map[string]interface{}{
- "unicode": name,
- "description": nameWithoutExt,
- "description_zh_cn": nameWithoutExt,
- "keywords": nameWithoutExt,
- }
- *items = append(*items, emoji)
- imgSrc := "/emojis/" + name
- model.CustomEmojis.Store(nameWithoutExt, imgSrc)
- }
- func checkUpdate(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- showMsg := arg["showMsg"].(bool)
- model.CheckUpdate(showMsg)
- }
- func exportLog(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- zipPath := model.ExportSystemLog()
- ret.Data = map[string]interface{}{
- "zip": zipPath,
- }
- }
- func getConf(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- maskedConf, err := model.GetMaskedConf()
- if nil != err {
- ret.Code = -1
- ret.Msg = "get conf failed: " + err.Error()
- return
- }
- if !maskedConf.Sync.Enabled || (0 == maskedConf.Sync.Provider && !model.IsSubscriber()) {
- maskedConf.Sync.Stat = model.Conf.Language(53)
- }
- ret.Data = map[string]interface{}{
- "conf": maskedConf,
- "start": !util.IsUILoaded,
- }
- }
- func setUILayout(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- if util.ReadOnly {
- return
- }
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- param, err := gulu.JSON.MarshalJSON(arg["layout"])
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- uiLayout := &conf.UILayout{}
- if err = gulu.JSON.UnmarshalJSON(param, uiLayout); nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- model.Conf.SetUILayout(uiLayout)
- model.Conf.Save()
- }
- func setAPIToken(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- token := arg["token"].(string)
- model.Conf.Api.Token = token
- model.Conf.Save()
- }
- func setAccessAuthCode(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- aac := arg["accessAuthCode"].(string)
- if model.MaskedAccessAuthCode == aac {
- aac = model.Conf.AccessAuthCode
- }
- model.Conf.AccessAuthCode = aac
- model.Conf.Save()
- session := util.GetSession(c)
- workspaceSession := util.GetWorkspaceSession(session)
- workspaceSession.AccessAuthCode = aac
- session.Save(c)
- go func() {
- time.Sleep(200 * time.Millisecond)
- util.ReloadUI()
- }()
- return
- }
- func setFollowSystemLockScreen(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- lockScreenMode := int(arg["lockScreenMode"].(float64))
- model.Conf.System.LockScreenMode = lockScreenMode
- model.Conf.Save()
- return
- }
- func getSysFonts(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- ret.Data = util.GetSysFonts(model.Conf.Lang)
- }
- func version(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- ret.Data = util.Ver
- }
- func currentTime(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- ret.Data = util.CurrentTimeMillis()
- }
- func bootProgress(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- progress, details := util.GetBootProgressDetails()
- ret.Data = map[string]interface{}{"progress": progress, "details": details}
- }
- func setAppearanceMode(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- mode := int(arg["mode"].(float64))
- model.Conf.Appearance.Mode = mode
- if 0 == mode {
- model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeLight, "theme.js"))
- } else {
- model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeDark, "theme.js"))
- }
- model.Conf.Save()
- ret.Data = map[string]interface{}{
- "appearance": model.Conf.Appearance,
- }
- }
- func setNetworkServe(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- networkServe := arg["networkServe"].(bool)
- model.Conf.System.NetworkServe = networkServe
- model.Conf.Save()
- util.PushMsg(model.Conf.Language(42), 1000*15)
- time.Sleep(time.Second * 3)
- }
- func setGoogleAnalytics(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- googleAnalytics := arg["googleAnalytics"].(bool)
- model.Conf.System.DisableGoogleAnalytics = !googleAnalytics
- model.Conf.Save()
- }
- func setUploadErrLog(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- uploadErrLog := arg["uploadErrLog"].(bool)
- model.Conf.System.UploadErrLog = uploadErrLog
- model.Conf.Save()
- util.PushMsg(model.Conf.Language(42), 1000*15)
- time.Sleep(time.Second * 3)
- }
- func setAutoLaunch(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- autoLaunch := int(arg["autoLaunch"].(float64))
- model.Conf.System.AutoLaunch2 = autoLaunch
- model.Conf.Save()
- windowStateConf := filepath.Join(util.ConfDir, "windowState.json")
- windowState := map[string]interface{}{}
- if gulu.File.IsExist(windowStateConf) {
- succ := false
- const maxRetry = 7
- for i := 0; i < maxRetry; i++ {
- data, err := os.ReadFile(windowStateConf)
- if nil != err {
- logging.LogErrorf("read [windowState.json] failed [%d/%d]: %s", i, maxRetry, err)
- time.Sleep(time.Second)
- } else {
- if err = gulu.JSON.UnmarshalJSON(data, &windowState); nil != err {
- logging.LogErrorf("unmarshal [windowState.json] failed: %s", err)
- } else {
- succ = true
- break
- }
- }
- }
- if !succ {
- logging.LogErrorf("read [windowState.json] failed")
- }
- }
- windowState["autoLaunch"] = autoLaunch
- data, err := gulu.JSON.MarshalJSON(windowState)
- if nil != err {
- logging.LogErrorf("marshal [windowState.json] failed: %s", err)
- return
- }
- if err = gulu.File.WriteFileSafer(windowStateConf, data, 0644); nil != err {
- logging.LogErrorf("create [windowState.json] failed: %s", err)
- }
- }
- func setDownloadInstallPkg(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- downloadInstallPkg := arg["downloadInstallPkg"].(bool)
- model.Conf.System.DownloadInstallPkg = downloadInstallPkg
- model.Conf.Save()
- }
- func setNetworkProxy(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- scheme := arg["scheme"].(string)
- host := arg["host"].(string)
- port := arg["port"].(string)
- model.Conf.System.NetworkProxy = &conf.NetworkProxy{
- Scheme: scheme,
- Host: host,
- Port: port,
- }
- model.Conf.Save()
- proxyURL := model.Conf.System.NetworkProxy.String()
- util.SetNetworkProxy(proxyURL)
- util.PushMsg(model.Conf.Language(102), 3000)
- }
- func addUIProcess(c *gin.Context) {
- pid := c.Query("pid")
- util.UIProcessIDs.Store(pid, true)
- }
- func exit(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- forceArg := arg["force"]
- var force bool
- if nil != forceArg {
- force = forceArg.(bool)
- }
- execInstallPkgArg := arg["execInstallPkg"] // 0:默认检查新版本,1:不执行新版本安装,2:执行新版本安装
- execInstallPkg := 0
- if nil != execInstallPkgArg {
- execInstallPkg = int(execInstallPkgArg.(float64))
- }
- exitCode := model.Close(force, true, execInstallPkg)
- ret.Code = exitCode
- switch exitCode {
- case 0:
- case 1: // 同步执行失败
- ret.Msg = model.Conf.Language(96) + "<div class=\"fn__space\"></div><button class=\"b3-button b3-button--white\">" + model.Conf.Language(97) + "</button>"
- ret.Data = map[string]interface{}{"closeTimeout": 0}
- case 2: // 提示新安装包
- ret.Msg = model.Conf.Language(61)
- ret.Data = map[string]interface{}{"closeTimeout": 0}
- }
- }
|