system.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "github.com/88250/lute"
  19. "net/http"
  20. "os"
  21. "path/filepath"
  22. "strings"
  23. "sync"
  24. "time"
  25. "github.com/88250/gulu"
  26. "github.com/gin-gonic/gin"
  27. "github.com/siyuan-note/logging"
  28. "github.com/siyuan-note/siyuan/kernel/conf"
  29. "github.com/siyuan-note/siyuan/kernel/model"
  30. "github.com/siyuan-note/siyuan/kernel/util"
  31. )
  32. func getNetwork(c *gin.Context) {
  33. ret := gulu.Ret.NewResult()
  34. defer c.JSON(http.StatusOK, ret)
  35. maskedConf, err := model.GetMaskedConf()
  36. if nil != err {
  37. ret.Code = -1
  38. ret.Msg = "get conf failed: " + err.Error()
  39. return
  40. }
  41. ret.Data = map[string]interface{}{
  42. "proxy": maskedConf.System.NetworkProxy,
  43. }
  44. }
  45. func getChangelog(c *gin.Context) {
  46. ret := gulu.Ret.NewResult()
  47. defer c.JSON(http.StatusOK, ret)
  48. data := map[string]interface{}{"show": false, "html": ""}
  49. ret.Data = data
  50. changelogsDir := filepath.Join(util.WorkingDir, "changelogs")
  51. if !gulu.File.IsDir(changelogsDir) {
  52. return
  53. }
  54. if !model.Conf.ShowChangelog {
  55. return
  56. }
  57. changelogPath := filepath.Join(changelogsDir, "v"+util.Ver, "v"+util.Ver+"_"+model.Conf.Lang+".md")
  58. if !gulu.File.IsExist(changelogPath) {
  59. changelogPath = filepath.Join(changelogsDir, "v"+util.Ver, "v"+util.Ver+".md")
  60. if !gulu.File.IsExist(changelogPath) {
  61. logging.LogErrorf("changelog not found: %s", changelogPath)
  62. return
  63. }
  64. }
  65. contentData, err := os.ReadFile(changelogPath)
  66. if nil != err {
  67. logging.LogErrorf("read changelog failed: %s", err)
  68. return
  69. }
  70. model.Conf.ShowChangelog = false
  71. luteEngine := lute.New()
  72. htmlContent := luteEngine.MarkdownStr("", string(contentData))
  73. htmlContent = util.LinkTarget(htmlContent, "")
  74. data["show"] = true
  75. data["html"] = htmlContent
  76. ret.Data = data
  77. }
  78. func getEmojiConf(c *gin.Context) {
  79. ret := gulu.Ret.NewResult()
  80. defer c.JSON(http.StatusOK, ret)
  81. builtConfPath := filepath.Join(util.AppearancePath, "emojis", "conf.json")
  82. data, err := os.ReadFile(builtConfPath)
  83. if nil != err {
  84. logging.LogErrorf("read emojis conf.json failed: %s", err)
  85. ret.Code = -1
  86. ret.Msg = err.Error()
  87. return
  88. }
  89. var conf []map[string]interface{}
  90. if err = gulu.JSON.UnmarshalJSON(data, &conf); nil != err {
  91. logging.LogErrorf("unmarshal emojis conf.json failed: %s", err)
  92. ret.Code = -1
  93. ret.Msg = err.Error()
  94. return
  95. }
  96. customConfDir := filepath.Join(util.DataDir, "emojis")
  97. custom := map[string]interface{}{
  98. "id": "custom",
  99. "title": "Custom",
  100. "title_zh_cn": "自定义",
  101. }
  102. items := []map[string]interface{}{}
  103. custom["items"] = items
  104. if gulu.File.IsDir(customConfDir) {
  105. model.CustomEmojis = sync.Map{}
  106. customEmojis, err := os.ReadDir(customConfDir)
  107. if nil != err {
  108. logging.LogErrorf("read custom emojis failed: %s", err)
  109. } else {
  110. for _, customEmoji := range customEmojis {
  111. name := customEmoji.Name()
  112. if strings.HasPrefix(name, ".") {
  113. continue
  114. }
  115. if customEmoji.IsDir() {
  116. // 子级
  117. subCustomEmojis, err := os.ReadDir(filepath.Join(customConfDir, name))
  118. if nil != err {
  119. logging.LogErrorf("read custom emojis failed: %s", err)
  120. continue
  121. }
  122. for _, subCustomEmoji := range subCustomEmojis {
  123. if subCustomEmoji.IsDir() {
  124. continue
  125. }
  126. name = subCustomEmoji.Name()
  127. if strings.HasPrefix(name, ".") {
  128. continue
  129. }
  130. addCustomEmoji(customEmoji.Name()+"/"+name, &items)
  131. }
  132. continue
  133. }
  134. addCustomEmoji(name, &items)
  135. }
  136. }
  137. }
  138. custom["items"] = items
  139. conf = append([]map[string]interface{}{custom}, conf...)
  140. ret.Data = conf
  141. return
  142. }
  143. func addCustomEmoji(name string, items *[]map[string]interface{}) {
  144. ext := filepath.Ext(name)
  145. nameWithoutExt := strings.TrimSuffix(name, ext)
  146. emoji := map[string]interface{}{
  147. "unicode": name,
  148. "description": nameWithoutExt,
  149. "description_zh_cn": nameWithoutExt,
  150. "keywords": nameWithoutExt,
  151. }
  152. *items = append(*items, emoji)
  153. imgSrc := "/emojis/" + name
  154. model.CustomEmojis.Store(nameWithoutExt, imgSrc)
  155. }
  156. func checkUpdate(c *gin.Context) {
  157. ret := gulu.Ret.NewResult()
  158. defer c.JSON(http.StatusOK, ret)
  159. arg, ok := util.JsonArg(c, ret)
  160. if !ok {
  161. return
  162. }
  163. showMsg := arg["showMsg"].(bool)
  164. model.CheckUpdate(showMsg)
  165. }
  166. func exportLog(c *gin.Context) {
  167. ret := gulu.Ret.NewResult()
  168. defer c.JSON(http.StatusOK, ret)
  169. zipPath := model.ExportSystemLog()
  170. ret.Data = map[string]interface{}{
  171. "zip": zipPath,
  172. }
  173. }
  174. func getConf(c *gin.Context) {
  175. ret := gulu.Ret.NewResult()
  176. defer c.JSON(http.StatusOK, ret)
  177. maskedConf, err := model.GetMaskedConf()
  178. if nil != err {
  179. ret.Code = -1
  180. ret.Msg = "get conf failed: " + err.Error()
  181. return
  182. }
  183. if !maskedConf.Sync.Enabled || (0 == maskedConf.Sync.Provider && !model.IsSubscriber()) {
  184. maskedConf.Sync.Stat = model.Conf.Language(53)
  185. }
  186. ret.Data = map[string]interface{}{
  187. "conf": maskedConf,
  188. "start": !util.IsUILoaded,
  189. }
  190. }
  191. func setUILayout(c *gin.Context) {
  192. ret := gulu.Ret.NewResult()
  193. defer c.JSON(http.StatusOK, ret)
  194. if util.ReadOnly {
  195. return
  196. }
  197. arg, ok := util.JsonArg(c, ret)
  198. if !ok {
  199. return
  200. }
  201. param, err := gulu.JSON.MarshalJSON(arg["layout"])
  202. if nil != err {
  203. ret.Code = -1
  204. ret.Msg = err.Error()
  205. return
  206. }
  207. uiLayout := &conf.UILayout{}
  208. if err = gulu.JSON.UnmarshalJSON(param, uiLayout); nil != err {
  209. ret.Code = -1
  210. ret.Msg = err.Error()
  211. return
  212. }
  213. model.Conf.SetUILayout(uiLayout)
  214. model.Conf.Save()
  215. }
  216. func setAPIToken(c *gin.Context) {
  217. ret := gulu.Ret.NewResult()
  218. defer c.JSON(http.StatusOK, ret)
  219. arg, ok := util.JsonArg(c, ret)
  220. if !ok {
  221. return
  222. }
  223. token := arg["token"].(string)
  224. model.Conf.Api.Token = token
  225. model.Conf.Save()
  226. }
  227. func setAccessAuthCode(c *gin.Context) {
  228. ret := gulu.Ret.NewResult()
  229. defer c.JSON(http.StatusOK, ret)
  230. arg, ok := util.JsonArg(c, ret)
  231. if !ok {
  232. return
  233. }
  234. aac := arg["accessAuthCode"].(string)
  235. if model.MaskedAccessAuthCode == aac {
  236. aac = model.Conf.AccessAuthCode
  237. }
  238. model.Conf.AccessAuthCode = aac
  239. model.Conf.Save()
  240. session := util.GetSession(c)
  241. workspaceSession := util.GetWorkspaceSession(session)
  242. workspaceSession.AccessAuthCode = aac
  243. session.Save(c)
  244. go func() {
  245. time.Sleep(200 * time.Millisecond)
  246. util.ReloadUI()
  247. }()
  248. return
  249. }
  250. func setFollowSystemLockScreen(c *gin.Context) {
  251. ret := gulu.Ret.NewResult()
  252. defer c.JSON(http.StatusOK, ret)
  253. arg, ok := util.JsonArg(c, ret)
  254. if !ok {
  255. return
  256. }
  257. lockScreenMode := int(arg["lockScreenMode"].(float64))
  258. model.Conf.System.LockScreenMode = lockScreenMode
  259. model.Conf.Save()
  260. return
  261. }
  262. func getSysFonts(c *gin.Context) {
  263. ret := gulu.Ret.NewResult()
  264. defer c.JSON(http.StatusOK, ret)
  265. ret.Data = util.GetSysFonts(model.Conf.Lang)
  266. }
  267. func version(c *gin.Context) {
  268. ret := gulu.Ret.NewResult()
  269. defer c.JSON(http.StatusOK, ret)
  270. ret.Data = util.Ver
  271. }
  272. func currentTime(c *gin.Context) {
  273. ret := gulu.Ret.NewResult()
  274. defer c.JSON(http.StatusOK, ret)
  275. ret.Data = util.CurrentTimeMillis()
  276. }
  277. func bootProgress(c *gin.Context) {
  278. ret := gulu.Ret.NewResult()
  279. defer c.JSON(http.StatusOK, ret)
  280. progress, details := util.GetBootProgressDetails()
  281. ret.Data = map[string]interface{}{"progress": progress, "details": details}
  282. }
  283. func setAppearanceMode(c *gin.Context) {
  284. ret := gulu.Ret.NewResult()
  285. defer c.JSON(http.StatusOK, ret)
  286. arg, ok := util.JsonArg(c, ret)
  287. if !ok {
  288. return
  289. }
  290. mode := int(arg["mode"].(float64))
  291. model.Conf.Appearance.Mode = mode
  292. if 0 == mode {
  293. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeLight, "theme.js"))
  294. } else {
  295. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeDark, "theme.js"))
  296. }
  297. model.Conf.Save()
  298. ret.Data = map[string]interface{}{
  299. "appearance": model.Conf.Appearance,
  300. }
  301. }
  302. func setNetworkServe(c *gin.Context) {
  303. ret := gulu.Ret.NewResult()
  304. defer c.JSON(http.StatusOK, ret)
  305. arg, ok := util.JsonArg(c, ret)
  306. if !ok {
  307. return
  308. }
  309. networkServe := arg["networkServe"].(bool)
  310. model.Conf.System.NetworkServe = networkServe
  311. model.Conf.Save()
  312. util.PushMsg(model.Conf.Language(42), 1000*15)
  313. time.Sleep(time.Second * 3)
  314. }
  315. func setGoogleAnalytics(c *gin.Context) {
  316. ret := gulu.Ret.NewResult()
  317. defer c.JSON(http.StatusOK, ret)
  318. arg, ok := util.JsonArg(c, ret)
  319. if !ok {
  320. return
  321. }
  322. googleAnalytics := arg["googleAnalytics"].(bool)
  323. model.Conf.System.DisableGoogleAnalytics = !googleAnalytics
  324. model.Conf.Save()
  325. }
  326. func setUploadErrLog(c *gin.Context) {
  327. ret := gulu.Ret.NewResult()
  328. defer c.JSON(http.StatusOK, ret)
  329. arg, ok := util.JsonArg(c, ret)
  330. if !ok {
  331. return
  332. }
  333. uploadErrLog := arg["uploadErrLog"].(bool)
  334. model.Conf.System.UploadErrLog = uploadErrLog
  335. model.Conf.Save()
  336. util.PushMsg(model.Conf.Language(42), 1000*15)
  337. time.Sleep(time.Second * 3)
  338. }
  339. func setAutoLaunch(c *gin.Context) {
  340. ret := gulu.Ret.NewResult()
  341. defer c.JSON(http.StatusOK, ret)
  342. arg, ok := util.JsonArg(c, ret)
  343. if !ok {
  344. return
  345. }
  346. autoLaunch := int(arg["autoLaunch"].(float64))
  347. model.Conf.System.AutoLaunch2 = autoLaunch
  348. model.Conf.Save()
  349. windowStateConf := filepath.Join(util.ConfDir, "windowState.json")
  350. windowState := map[string]interface{}{}
  351. if gulu.File.IsExist(windowStateConf) {
  352. succ := false
  353. const maxRetry = 7
  354. for i := 0; i < maxRetry; i++ {
  355. data, err := os.ReadFile(windowStateConf)
  356. if nil != err {
  357. logging.LogErrorf("read [windowState.json] failed [%d/%d]: %s", i, maxRetry, err)
  358. time.Sleep(time.Second)
  359. } else {
  360. if err = gulu.JSON.UnmarshalJSON(data, &windowState); nil != err {
  361. logging.LogErrorf("unmarshal [windowState.json] failed: %s", err)
  362. } else {
  363. succ = true
  364. break
  365. }
  366. }
  367. }
  368. if !succ {
  369. logging.LogErrorf("read [windowState.json] failed")
  370. }
  371. }
  372. windowState["autoLaunch"] = autoLaunch
  373. data, err := gulu.JSON.MarshalJSON(windowState)
  374. if nil != err {
  375. logging.LogErrorf("marshal [windowState.json] failed: %s", err)
  376. return
  377. }
  378. if err = gulu.File.WriteFileSafer(windowStateConf, data, 0644); nil != err {
  379. logging.LogErrorf("create [windowState.json] failed: %s", err)
  380. }
  381. }
  382. func setDownloadInstallPkg(c *gin.Context) {
  383. ret := gulu.Ret.NewResult()
  384. defer c.JSON(http.StatusOK, ret)
  385. arg, ok := util.JsonArg(c, ret)
  386. if !ok {
  387. return
  388. }
  389. downloadInstallPkg := arg["downloadInstallPkg"].(bool)
  390. model.Conf.System.DownloadInstallPkg = downloadInstallPkg
  391. model.Conf.Save()
  392. }
  393. func setNetworkProxy(c *gin.Context) {
  394. ret := gulu.Ret.NewResult()
  395. defer c.JSON(http.StatusOK, ret)
  396. arg, ok := util.JsonArg(c, ret)
  397. if !ok {
  398. return
  399. }
  400. scheme := arg["scheme"].(string)
  401. host := arg["host"].(string)
  402. port := arg["port"].(string)
  403. model.Conf.System.NetworkProxy = &conf.NetworkProxy{
  404. Scheme: scheme,
  405. Host: host,
  406. Port: port,
  407. }
  408. model.Conf.Save()
  409. proxyURL := model.Conf.System.NetworkProxy.String()
  410. util.SetNetworkProxy(proxyURL)
  411. util.PushMsg(model.Conf.Language(102), 3000)
  412. }
  413. func addUIProcess(c *gin.Context) {
  414. pid := c.Query("pid")
  415. util.UIProcessIDs.Store(pid, true)
  416. }
  417. func exit(c *gin.Context) {
  418. ret := gulu.Ret.NewResult()
  419. defer c.JSON(http.StatusOK, ret)
  420. arg, ok := util.JsonArg(c, ret)
  421. if !ok {
  422. return
  423. }
  424. forceArg := arg["force"]
  425. var force bool
  426. if nil != forceArg {
  427. force = forceArg.(bool)
  428. }
  429. execInstallPkgArg := arg["execInstallPkg"] // 0:默认检查新版本,1:不执行新版本安装,2:执行新版本安装
  430. execInstallPkg := 0
  431. if nil != execInstallPkgArg {
  432. execInstallPkg = int(execInstallPkgArg.(float64))
  433. }
  434. exitCode := model.Close(force, true, execInstallPkg)
  435. ret.Code = exitCode
  436. switch exitCode {
  437. case 0:
  438. case 1: // 同步执行失败
  439. ret.Msg = model.Conf.Language(96) + "<div class=\"fn__space\"></div><button class=\"b3-button b3-button--white\">" + model.Conf.Language(97) + "</button>"
  440. ret.Data = map[string]interface{}{"closeTimeout": 0}
  441. case 2: // 提示新安装包
  442. ret.Msg = model.Conf.Language(61)
  443. ret.Data = map[string]interface{}{"closeTimeout": 0}
  444. }
  445. }