system.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. "net/http"
  19. "os"
  20. "path/filepath"
  21. "strings"
  22. "sync"
  23. "time"
  24. "github.com/88250/lute"
  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. // REF: https://github.com/siyuan-note/siyuan/issues/11364
  187. role := model.GetGinContextRole(c)
  188. if model.IsReadOnlyRole(role) {
  189. maskedConf.ReadOnly = true
  190. }
  191. if !model.IsValidRole(role, []model.Role{
  192. model.RoleAdministrator,
  193. }) {
  194. model.HideConfSecret(maskedConf)
  195. }
  196. ret.Data = map[string]interface{}{
  197. "conf": maskedConf,
  198. "start": !util.IsUILoaded,
  199. }
  200. }
  201. func setUILayout(c *gin.Context) {
  202. ret := gulu.Ret.NewResult()
  203. defer c.JSON(http.StatusOK, ret)
  204. if util.ReadOnly {
  205. return
  206. }
  207. arg, ok := util.JsonArg(c, ret)
  208. if !ok {
  209. return
  210. }
  211. param, err := gulu.JSON.MarshalJSON(arg["layout"])
  212. if nil != err {
  213. ret.Code = -1
  214. ret.Msg = err.Error()
  215. return
  216. }
  217. uiLayout := &conf.UILayout{}
  218. if err = gulu.JSON.UnmarshalJSON(param, uiLayout); nil != err {
  219. ret.Code = -1
  220. ret.Msg = err.Error()
  221. return
  222. }
  223. model.Conf.SetUILayout(uiLayout)
  224. model.Conf.Save()
  225. }
  226. func setAPIToken(c *gin.Context) {
  227. ret := gulu.Ret.NewResult()
  228. defer c.JSON(http.StatusOK, ret)
  229. arg, ok := util.JsonArg(c, ret)
  230. if !ok {
  231. return
  232. }
  233. token := arg["token"].(string)
  234. model.Conf.Api.Token = token
  235. model.Conf.Save()
  236. }
  237. func setAccessAuthCode(c *gin.Context) {
  238. ret := gulu.Ret.NewResult()
  239. defer c.JSON(http.StatusOK, ret)
  240. arg, ok := util.JsonArg(c, ret)
  241. if !ok {
  242. return
  243. }
  244. aac := arg["accessAuthCode"].(string)
  245. if model.MaskedAccessAuthCode == aac {
  246. aac = model.Conf.AccessAuthCode
  247. }
  248. model.Conf.AccessAuthCode = aac
  249. model.Conf.Save()
  250. session := util.GetSession(c)
  251. workspaceSession := util.GetWorkspaceSession(session)
  252. workspaceSession.AccessAuthCode = aac
  253. session.Save(c)
  254. go func() {
  255. time.Sleep(200 * time.Millisecond)
  256. util.ReloadUI()
  257. }()
  258. return
  259. }
  260. func setFollowSystemLockScreen(c *gin.Context) {
  261. ret := gulu.Ret.NewResult()
  262. defer c.JSON(http.StatusOK, ret)
  263. arg, ok := util.JsonArg(c, ret)
  264. if !ok {
  265. return
  266. }
  267. lockScreenMode := int(arg["lockScreenMode"].(float64))
  268. model.Conf.System.LockScreenMode = lockScreenMode
  269. model.Conf.Save()
  270. return
  271. }
  272. func getSysFonts(c *gin.Context) {
  273. ret := gulu.Ret.NewResult()
  274. defer c.JSON(http.StatusOK, ret)
  275. ret.Data = util.GetSysFonts(model.Conf.Lang)
  276. }
  277. func version(c *gin.Context) {
  278. ret := gulu.Ret.NewResult()
  279. defer c.JSON(http.StatusOK, ret)
  280. ret.Data = util.Ver
  281. }
  282. func currentTime(c *gin.Context) {
  283. ret := gulu.Ret.NewResult()
  284. defer c.JSON(http.StatusOK, ret)
  285. ret.Data = util.CurrentTimeMillis()
  286. }
  287. func bootProgress(c *gin.Context) {
  288. ret := gulu.Ret.NewResult()
  289. defer c.JSON(http.StatusOK, ret)
  290. progress, details := util.GetBootProgressDetails()
  291. ret.Data = map[string]interface{}{"progress": progress, "details": details}
  292. }
  293. func setAppearanceMode(c *gin.Context) {
  294. ret := gulu.Ret.NewResult()
  295. defer c.JSON(http.StatusOK, ret)
  296. arg, ok := util.JsonArg(c, ret)
  297. if !ok {
  298. return
  299. }
  300. mode := int(arg["mode"].(float64))
  301. model.Conf.Appearance.Mode = mode
  302. if 0 == mode {
  303. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeLight, "theme.js"))
  304. } else {
  305. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeDark, "theme.js"))
  306. }
  307. model.Conf.Save()
  308. ret.Data = map[string]interface{}{
  309. "appearance": model.Conf.Appearance,
  310. }
  311. }
  312. func setNetworkServe(c *gin.Context) {
  313. ret := gulu.Ret.NewResult()
  314. defer c.JSON(http.StatusOK, ret)
  315. arg, ok := util.JsonArg(c, ret)
  316. if !ok {
  317. return
  318. }
  319. networkServe := arg["networkServe"].(bool)
  320. model.Conf.System.NetworkServe = networkServe
  321. model.Conf.Save()
  322. util.PushMsg(model.Conf.Language(42), 1000*15)
  323. time.Sleep(time.Second * 3)
  324. }
  325. func setGoogleAnalytics(c *gin.Context) {
  326. ret := gulu.Ret.NewResult()
  327. defer c.JSON(http.StatusOK, ret)
  328. arg, ok := util.JsonArg(c, ret)
  329. if !ok {
  330. return
  331. }
  332. googleAnalytics := arg["googleAnalytics"].(bool)
  333. model.Conf.System.DisableGoogleAnalytics = !googleAnalytics
  334. model.Conf.Save()
  335. }
  336. func setUploadErrLog(c *gin.Context) {
  337. ret := gulu.Ret.NewResult()
  338. defer c.JSON(http.StatusOK, ret)
  339. arg, ok := util.JsonArg(c, ret)
  340. if !ok {
  341. return
  342. }
  343. uploadErrLog := arg["uploadErrLog"].(bool)
  344. model.Conf.System.UploadErrLog = uploadErrLog
  345. model.Conf.Save()
  346. util.PushMsg(model.Conf.Language(42), 1000*15)
  347. time.Sleep(time.Second * 3)
  348. }
  349. func setAutoLaunch(c *gin.Context) {
  350. ret := gulu.Ret.NewResult()
  351. defer c.JSON(http.StatusOK, ret)
  352. arg, ok := util.JsonArg(c, ret)
  353. if !ok {
  354. return
  355. }
  356. autoLaunch := int(arg["autoLaunch"].(float64))
  357. model.Conf.System.AutoLaunch2 = autoLaunch
  358. model.Conf.Save()
  359. }
  360. func setDownloadInstallPkg(c *gin.Context) {
  361. ret := gulu.Ret.NewResult()
  362. defer c.JSON(http.StatusOK, ret)
  363. arg, ok := util.JsonArg(c, ret)
  364. if !ok {
  365. return
  366. }
  367. downloadInstallPkg := arg["downloadInstallPkg"].(bool)
  368. model.Conf.System.DownloadInstallPkg = downloadInstallPkg
  369. model.Conf.Save()
  370. }
  371. func setNetworkProxy(c *gin.Context) {
  372. ret := gulu.Ret.NewResult()
  373. defer c.JSON(http.StatusOK, ret)
  374. arg, ok := util.JsonArg(c, ret)
  375. if !ok {
  376. return
  377. }
  378. scheme := arg["scheme"].(string)
  379. host := arg["host"].(string)
  380. port := arg["port"].(string)
  381. model.Conf.System.NetworkProxy = &conf.NetworkProxy{
  382. Scheme: scheme,
  383. Host: host,
  384. Port: port,
  385. }
  386. model.Conf.Save()
  387. proxyURL := model.Conf.System.NetworkProxy.String()
  388. util.SetNetworkProxy(proxyURL)
  389. util.PushMsg(model.Conf.Language(102), 3000)
  390. }
  391. func addUIProcess(c *gin.Context) {
  392. pid := c.Query("pid")
  393. util.UIProcessIDs.Store(pid, true)
  394. }
  395. func exit(c *gin.Context) {
  396. ret := gulu.Ret.NewResult()
  397. defer c.JSON(http.StatusOK, ret)
  398. arg, ok := util.JsonArg(c, ret)
  399. if !ok {
  400. return
  401. }
  402. forceArg := arg["force"]
  403. var force bool
  404. if nil != forceArg {
  405. force = forceArg.(bool)
  406. }
  407. execInstallPkgArg := arg["execInstallPkg"] // 0:默认检查新版本,1:不执行新版本安装,2:执行新版本安装
  408. execInstallPkg := 0
  409. if nil != execInstallPkgArg {
  410. execInstallPkg = int(execInstallPkgArg.(float64))
  411. }
  412. exitCode := model.Close(force, true, execInstallPkg)
  413. ret.Code = exitCode
  414. switch exitCode {
  415. case 0:
  416. case 1: // 同步执行失败
  417. ret.Msg = model.Conf.Language(96) + "<div class=\"fn__space\"></div><button class=\"b3-button b3-button--white\">" + model.Conf.Language(97) + "</button>"
  418. ret.Data = map[string]interface{}{"closeTimeout": 0}
  419. case 2: // 提示新安装包
  420. ret.Msg = model.Conf.Language(61)
  421. ret.Data = map[string]interface{}{"closeTimeout": 0}
  422. }
  423. }