system.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 getChangelog(c *gin.Context) {
  33. ret := gulu.Ret.NewResult()
  34. defer c.JSON(http.StatusOK, ret)
  35. data := map[string]interface{}{"show": false, "html": ""}
  36. ret.Data = data
  37. changelogsDir := filepath.Join(util.WorkingDir, "changelogs")
  38. if !gulu.File.IsDir(changelogsDir) {
  39. return
  40. }
  41. if !model.Conf.ShowChangelog {
  42. return
  43. }
  44. changelogPath := filepath.Join(changelogsDir, "v"+util.Ver, "v"+util.Ver+"_"+model.Conf.Lang+".md")
  45. if !gulu.File.IsExist(changelogPath) {
  46. changelogPath = filepath.Join(changelogsDir, "v"+util.Ver, "v"+util.Ver+".md")
  47. if !gulu.File.IsExist(changelogPath) {
  48. logging.LogErrorf("changelog not found: %s", changelogPath)
  49. return
  50. }
  51. }
  52. contentData, err := os.ReadFile(changelogPath)
  53. if nil != err {
  54. logging.LogErrorf("read changelog failed: %s", err)
  55. return
  56. }
  57. model.Conf.ShowChangelog = false
  58. luteEngine := lute.New()
  59. htmlContent := luteEngine.MarkdownStr("", string(contentData))
  60. htmlContent = util.LinkTarget(htmlContent, "")
  61. data["show"] = true
  62. data["html"] = htmlContent
  63. ret.Data = data
  64. }
  65. func getEmojiConf(c *gin.Context) {
  66. ret := gulu.Ret.NewResult()
  67. defer c.JSON(http.StatusOK, ret)
  68. builtConfPath := filepath.Join(util.AppearancePath, "emojis", "conf.json")
  69. data, err := os.ReadFile(builtConfPath)
  70. if nil != err {
  71. logging.LogErrorf("read emojis conf.json failed: %s", err)
  72. ret.Code = -1
  73. ret.Msg = err.Error()
  74. return
  75. }
  76. var conf []map[string]interface{}
  77. if err = gulu.JSON.UnmarshalJSON(data, &conf); nil != err {
  78. logging.LogErrorf("unmarshal emojis conf.json failed: %s", err)
  79. ret.Code = -1
  80. ret.Msg = err.Error()
  81. return
  82. }
  83. customConfDir := filepath.Join(util.DataDir, "emojis")
  84. custom := map[string]interface{}{
  85. "id": "custom",
  86. "title": "Custom",
  87. "title_zh_cn": "自定义",
  88. }
  89. items := []map[string]interface{}{}
  90. custom["items"] = items
  91. if gulu.File.IsDir(customConfDir) {
  92. model.CustomEmojis = sync.Map{}
  93. customEmojis, err := os.ReadDir(customConfDir)
  94. if nil != err {
  95. logging.LogErrorf("read custom emojis failed: %s", err)
  96. } else {
  97. for _, customEmoji := range customEmojis {
  98. name := customEmoji.Name()
  99. if strings.HasPrefix(name, ".") {
  100. continue
  101. }
  102. if customEmoji.IsDir() {
  103. // 子级
  104. subCustomEmojis, err := os.ReadDir(filepath.Join(customConfDir, name))
  105. if nil != err {
  106. logging.LogErrorf("read custom emojis failed: %s", err)
  107. continue
  108. }
  109. for _, subCustomEmoji := range subCustomEmojis {
  110. if subCustomEmoji.IsDir() {
  111. continue
  112. }
  113. name = subCustomEmoji.Name()
  114. if strings.HasPrefix(name, ".") {
  115. continue
  116. }
  117. addCustomEmoji(customEmoji.Name()+"/"+name, &items)
  118. }
  119. continue
  120. }
  121. addCustomEmoji(name, &items)
  122. }
  123. }
  124. }
  125. custom["items"] = items
  126. conf = append([]map[string]interface{}{custom}, conf...)
  127. ret.Data = conf
  128. return
  129. }
  130. func addCustomEmoji(name string, items *[]map[string]interface{}) {
  131. ext := filepath.Ext(name)
  132. nameWithoutExt := strings.TrimSuffix(name, ext)
  133. emoji := map[string]interface{}{
  134. "unicode": name,
  135. "description": nameWithoutExt,
  136. "description_zh_cn": nameWithoutExt,
  137. "keywords": nameWithoutExt,
  138. }
  139. *items = append(*items, emoji)
  140. imgSrc := "/emojis/" + name
  141. model.CustomEmojis.Store(nameWithoutExt, imgSrc)
  142. }
  143. func checkUpdate(c *gin.Context) {
  144. ret := gulu.Ret.NewResult()
  145. defer c.JSON(http.StatusOK, ret)
  146. arg, ok := util.JsonArg(c, ret)
  147. if !ok {
  148. return
  149. }
  150. showMsg := arg["showMsg"].(bool)
  151. model.CheckUpdate(showMsg)
  152. }
  153. func exportLog(c *gin.Context) {
  154. ret := gulu.Ret.NewResult()
  155. defer c.JSON(http.StatusOK, ret)
  156. zipPath := model.ExportSystemLog()
  157. ret.Data = map[string]interface{}{
  158. "zip": zipPath,
  159. }
  160. }
  161. func getConf(c *gin.Context) {
  162. ret := gulu.Ret.NewResult()
  163. defer c.JSON(http.StatusOK, ret)
  164. maskedConf, err := model.GetMaskedConf()
  165. if nil != err {
  166. ret.Code = -1
  167. ret.Msg = "get conf failed: " + err.Error()
  168. return
  169. }
  170. if !maskedConf.Sync.Enabled || (0 == maskedConf.Sync.Provider && !model.IsSubscriber()) {
  171. maskedConf.Sync.Stat = model.Conf.Language(53)
  172. }
  173. ret.Data = map[string]interface{}{
  174. "conf": maskedConf,
  175. "start": !util.IsUILoaded,
  176. }
  177. if !util.IsUILoaded {
  178. go func() {
  179. util.WaitForUILoaded()
  180. if model.Conf.Editor.ReadOnly {
  181. // 编辑器启用只读模式时启动后提示用户 https://github.com/siyuan-note/siyuan/issues/7700
  182. time.Sleep(time.Second * 3)
  183. if model.Conf.Editor.ReadOnly {
  184. util.PushMsg(model.Conf.Language(197), 7000)
  185. }
  186. }
  187. }()
  188. }
  189. }
  190. func setUILayout(c *gin.Context) {
  191. ret := gulu.Ret.NewResult()
  192. defer c.JSON(http.StatusOK, ret)
  193. if util.ReadOnly {
  194. return
  195. }
  196. arg, ok := util.JsonArg(c, ret)
  197. if !ok {
  198. return
  199. }
  200. param, err := gulu.JSON.MarshalJSON(arg["layout"])
  201. if nil != err {
  202. ret.Code = -1
  203. ret.Msg = err.Error()
  204. return
  205. }
  206. uiLayout := &conf.UILayout{}
  207. if err = gulu.JSON.UnmarshalJSON(param, uiLayout); nil != err {
  208. ret.Code = -1
  209. ret.Msg = err.Error()
  210. return
  211. }
  212. model.Conf.UILayout = uiLayout
  213. model.Conf.Save()
  214. }
  215. func setAccessAuthCode(c *gin.Context) {
  216. ret := gulu.Ret.NewResult()
  217. defer c.JSON(http.StatusOK, ret)
  218. arg, ok := util.JsonArg(c, ret)
  219. if !ok {
  220. return
  221. }
  222. aac := arg["accessAuthCode"].(string)
  223. if model.MaskedAccessAuthCode == aac {
  224. aac = model.Conf.AccessAuthCode
  225. }
  226. model.Conf.AccessAuthCode = aac
  227. model.Conf.Save()
  228. session := util.GetSession(c)
  229. workspaceSession := util.GetWorkspaceSession(session)
  230. workspaceSession.AccessAuthCode = aac
  231. session.Save(c)
  232. go func() {
  233. time.Sleep(200 * time.Millisecond)
  234. util.ReloadUI()
  235. }()
  236. return
  237. }
  238. func getSysFonts(c *gin.Context) {
  239. ret := gulu.Ret.NewResult()
  240. defer c.JSON(http.StatusOK, ret)
  241. ret.Data = util.GetSysFonts(model.Conf.Lang)
  242. }
  243. func version(c *gin.Context) {
  244. ret := gulu.Ret.NewResult()
  245. defer c.JSON(http.StatusOK, ret)
  246. ret.Data = util.Ver
  247. }
  248. func currentTime(c *gin.Context) {
  249. ret := gulu.Ret.NewResult()
  250. defer c.JSON(http.StatusOK, ret)
  251. ret.Data = util.CurrentTimeMillis()
  252. }
  253. func bootProgress(c *gin.Context) {
  254. ret := gulu.Ret.NewResult()
  255. defer c.JSON(http.StatusOK, ret)
  256. progress, details := util.GetBootProgressDetails()
  257. ret.Data = map[string]interface{}{"progress": progress, "details": details}
  258. }
  259. func setAppearanceMode(c *gin.Context) {
  260. ret := gulu.Ret.NewResult()
  261. defer c.JSON(http.StatusOK, ret)
  262. arg, ok := util.JsonArg(c, ret)
  263. if !ok {
  264. return
  265. }
  266. mode := int(arg["mode"].(float64))
  267. model.Conf.Appearance.Mode = mode
  268. if 0 == mode {
  269. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeLight, "theme.js"))
  270. } else {
  271. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeDark, "theme.js"))
  272. }
  273. model.Conf.Save()
  274. ret.Data = map[string]interface{}{
  275. "appearance": model.Conf.Appearance,
  276. }
  277. }
  278. func setNetworkServe(c *gin.Context) {
  279. ret := gulu.Ret.NewResult()
  280. defer c.JSON(http.StatusOK, ret)
  281. arg, ok := util.JsonArg(c, ret)
  282. if !ok {
  283. return
  284. }
  285. networkServe := arg["networkServe"].(bool)
  286. model.Conf.System.NetworkServe = networkServe
  287. model.Conf.Save()
  288. util.PushMsg(model.Conf.Language(42), 1000*15)
  289. time.Sleep(time.Second * 3)
  290. }
  291. func setGoogleAnalytics(c *gin.Context) {
  292. ret := gulu.Ret.NewResult()
  293. defer c.JSON(http.StatusOK, ret)
  294. arg, ok := util.JsonArg(c, ret)
  295. if !ok {
  296. return
  297. }
  298. googleAnalytics := arg["googleAnalytics"].(bool)
  299. model.Conf.System.DisableGoogleAnalytics = !googleAnalytics
  300. model.Conf.Save()
  301. }
  302. func setUploadErrLog(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. uploadErrLog := arg["uploadErrLog"].(bool)
  310. model.Conf.System.UploadErrLog = uploadErrLog
  311. model.Conf.Save()
  312. util.PushMsg(model.Conf.Language(42), 1000*15)
  313. time.Sleep(time.Second * 3)
  314. }
  315. func setAutoLaunch(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. autoLaunch := arg["autoLaunch"].(bool)
  323. model.Conf.System.AutoLaunch = autoLaunch
  324. model.Conf.Save()
  325. }
  326. func setDownloadInstallPkg(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. downloadInstallPkg := arg["downloadInstallPkg"].(bool)
  334. model.Conf.System.DownloadInstallPkg = downloadInstallPkg
  335. model.Conf.Save()
  336. }
  337. func setNetworkProxy(c *gin.Context) {
  338. ret := gulu.Ret.NewResult()
  339. defer c.JSON(http.StatusOK, ret)
  340. arg, ok := util.JsonArg(c, ret)
  341. if !ok {
  342. return
  343. }
  344. scheme := arg["scheme"].(string)
  345. host := arg["host"].(string)
  346. port := arg["port"].(string)
  347. model.Conf.System.NetworkProxy = &conf.NetworkProxy{
  348. Scheme: scheme,
  349. Host: host,
  350. Port: port,
  351. }
  352. model.Conf.Save()
  353. proxyURL := model.Conf.System.NetworkProxy.String()
  354. util.SetNetworkProxy(proxyURL)
  355. util.PushMsg(model.Conf.Language(102), 3000)
  356. }
  357. func addUIProcess(c *gin.Context) {
  358. pid := c.Query("pid")
  359. util.UIProcessIDs.Store(pid, true)
  360. }
  361. func exit(c *gin.Context) {
  362. ret := gulu.Ret.NewResult()
  363. defer c.JSON(http.StatusOK, ret)
  364. arg, ok := util.JsonArg(c, ret)
  365. if !ok {
  366. return
  367. }
  368. forceArg := arg["force"]
  369. var force bool
  370. if nil != forceArg {
  371. force = forceArg.(bool)
  372. }
  373. execInstallPkgArg := arg["execInstallPkg"] // 0:默认检查新版本,1:不执行新版本安装,2:执行新版本安装
  374. execInstallPkg := 0
  375. if nil != execInstallPkgArg {
  376. execInstallPkg = int(execInstallPkgArg.(float64))
  377. }
  378. exitCode := model.Close(force, execInstallPkg)
  379. ret.Code = exitCode
  380. switch exitCode {
  381. case 0:
  382. case 1: // 同步执行失败
  383. ret.Msg = model.Conf.Language(96) + "<div class=\"fn__space\"></div><button class=\"b3-button b3-button--white\">" + model.Conf.Language(97) + "</button>"
  384. ret.Data = map[string]interface{}{"closeTimeout": 0}
  385. case 2: // 提示新安装包
  386. ret.Msg = model.Conf.Language(61)
  387. ret.Data = map[string]interface{}{"closeTimeout": 0}
  388. }
  389. }