system.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // SiYuan - Build Your Eternal Digital Garden
  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. name = subCustomEmoji.Name()
  111. if strings.HasPrefix(name, ".") {
  112. continue
  113. }
  114. addCustomEmoji(customEmoji.Name()+"/"+name, &items)
  115. }
  116. continue
  117. }
  118. addCustomEmoji(name, &items)
  119. }
  120. }
  121. }
  122. custom["items"] = items
  123. conf = append([]map[string]interface{}{custom}, conf...)
  124. ret.Data = conf
  125. return
  126. }
  127. func addCustomEmoji(name string, items *[]map[string]interface{}) {
  128. ext := filepath.Ext(name)
  129. nameWithoutExt := strings.TrimSuffix(name, ext)
  130. emoji := map[string]interface{}{
  131. "unicode": name,
  132. "description": nameWithoutExt,
  133. "description_zh_cn": nameWithoutExt,
  134. "keywords": nameWithoutExt,
  135. }
  136. *items = append(*items, emoji)
  137. imgSrc := "/emojis/" + name
  138. model.CustomEmojis.Store(nameWithoutExt, imgSrc)
  139. }
  140. func checkUpdate(c *gin.Context) {
  141. ret := gulu.Ret.NewResult()
  142. defer c.JSON(http.StatusOK, ret)
  143. arg, ok := util.JsonArg(c, ret)
  144. if !ok {
  145. return
  146. }
  147. showMsg := arg["showMsg"].(bool)
  148. model.CheckUpdate(showMsg)
  149. }
  150. func exportLog(c *gin.Context) {
  151. ret := gulu.Ret.NewResult()
  152. defer c.JSON(http.StatusOK, ret)
  153. zipPath := model.ExportSystemLog()
  154. ret.Data = map[string]interface{}{
  155. "zip": zipPath,
  156. }
  157. }
  158. func getConf(c *gin.Context) {
  159. ret := gulu.Ret.NewResult()
  160. defer c.JSON(http.StatusOK, ret)
  161. maskedConf, err := model.GetMaskedConf()
  162. if nil != err {
  163. ret.Code = -1
  164. ret.Msg = "get conf failed: " + err.Error()
  165. return
  166. }
  167. if !maskedConf.Sync.Enabled || (0 == maskedConf.Sync.Provider && !model.IsSubscriber()) {
  168. maskedConf.Sync.Stat = model.Conf.Language(53)
  169. }
  170. ret.Data = map[string]interface{}{
  171. "conf": maskedConf,
  172. "start": !util.IsUILoaded,
  173. }
  174. if !util.IsUILoaded {
  175. go func() {
  176. util.WaitForUILoaded()
  177. if model.Conf.Editor.ReadOnly {
  178. // 编辑器启用只读模式时启动后提示用户 https://github.com/siyuan-note/siyuan/issues/7700
  179. time.Sleep(time.Second * 3)
  180. if model.Conf.Editor.ReadOnly {
  181. util.PushMsg(model.Conf.Language(197), 7000)
  182. }
  183. }
  184. }()
  185. }
  186. }
  187. func setUILayout(c *gin.Context) {
  188. ret := gulu.Ret.NewResult()
  189. defer c.JSON(http.StatusOK, ret)
  190. if util.ReadOnly {
  191. return
  192. }
  193. arg, ok := util.JsonArg(c, ret)
  194. if !ok {
  195. return
  196. }
  197. param, err := gulu.JSON.MarshalJSON(arg["layout"])
  198. if nil != err {
  199. ret.Code = -1
  200. ret.Msg = err.Error()
  201. return
  202. }
  203. uiLayout := &conf.UILayout{}
  204. if err = gulu.JSON.UnmarshalJSON(param, uiLayout); nil != err {
  205. ret.Code = -1
  206. ret.Msg = err.Error()
  207. return
  208. }
  209. model.Conf.UILayout = uiLayout
  210. model.Conf.Save()
  211. }
  212. func setAccessAuthCode(c *gin.Context) {
  213. ret := gulu.Ret.NewResult()
  214. defer c.JSON(http.StatusOK, ret)
  215. arg, ok := util.JsonArg(c, ret)
  216. if !ok {
  217. return
  218. }
  219. aac := arg["accessAuthCode"].(string)
  220. if model.MaskedAccessAuthCode == aac {
  221. aac = model.Conf.AccessAuthCode
  222. }
  223. model.Conf.AccessAuthCode = aac
  224. model.Conf.Save()
  225. session := util.GetSession(c)
  226. workspaceSession := util.GetWorkspaceSession(session)
  227. workspaceSession.AccessAuthCode = aac
  228. session.Save(c)
  229. go func() {
  230. time.Sleep(200 * time.Millisecond)
  231. util.ReloadUI()
  232. }()
  233. return
  234. }
  235. func getSysFonts(c *gin.Context) {
  236. ret := gulu.Ret.NewResult()
  237. defer c.JSON(http.StatusOK, ret)
  238. ret.Data = util.GetSysFonts(model.Conf.Lang)
  239. }
  240. func version(c *gin.Context) {
  241. ret := gulu.Ret.NewResult()
  242. defer c.JSON(http.StatusOK, ret)
  243. ret.Data = util.Ver
  244. }
  245. func currentTime(c *gin.Context) {
  246. ret := gulu.Ret.NewResult()
  247. defer c.JSON(http.StatusOK, ret)
  248. ret.Data = util.CurrentTimeMillis()
  249. }
  250. func bootProgress(c *gin.Context) {
  251. ret := gulu.Ret.NewResult()
  252. defer c.JSON(http.StatusOK, ret)
  253. progress, details := util.GetBootProgressDetails()
  254. ret.Data = map[string]interface{}{"progress": progress, "details": details}
  255. }
  256. func setAppearanceMode(c *gin.Context) {
  257. ret := gulu.Ret.NewResult()
  258. defer c.JSON(http.StatusOK, ret)
  259. arg, ok := util.JsonArg(c, ret)
  260. if !ok {
  261. return
  262. }
  263. mode := int(arg["mode"].(float64))
  264. model.Conf.Appearance.Mode = mode
  265. if 0 == mode {
  266. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeLight, "theme.js"))
  267. } else {
  268. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeDark, "theme.js"))
  269. }
  270. model.Conf.Save()
  271. ret.Data = map[string]interface{}{
  272. "appearance": model.Conf.Appearance,
  273. }
  274. }
  275. func setNetworkServe(c *gin.Context) {
  276. ret := gulu.Ret.NewResult()
  277. defer c.JSON(http.StatusOK, ret)
  278. arg, ok := util.JsonArg(c, ret)
  279. if !ok {
  280. return
  281. }
  282. networkServe := arg["networkServe"].(bool)
  283. model.Conf.System.NetworkServe = networkServe
  284. model.Conf.Save()
  285. util.PushMsg(model.Conf.Language(42), 1000*15)
  286. time.Sleep(time.Second * 3)
  287. }
  288. func setGoogleAnalytics(c *gin.Context) {
  289. ret := gulu.Ret.NewResult()
  290. defer c.JSON(http.StatusOK, ret)
  291. arg, ok := util.JsonArg(c, ret)
  292. if !ok {
  293. return
  294. }
  295. googleAnalytics := arg["googleAnalytics"].(bool)
  296. model.Conf.System.DisableGoogleAnalytics = !googleAnalytics
  297. model.Conf.Save()
  298. }
  299. func setUploadErrLog(c *gin.Context) {
  300. ret := gulu.Ret.NewResult()
  301. defer c.JSON(http.StatusOK, ret)
  302. arg, ok := util.JsonArg(c, ret)
  303. if !ok {
  304. return
  305. }
  306. uploadErrLog := arg["uploadErrLog"].(bool)
  307. model.Conf.System.UploadErrLog = uploadErrLog
  308. model.Conf.Save()
  309. util.PushMsg(model.Conf.Language(42), 1000*15)
  310. time.Sleep(time.Second * 3)
  311. }
  312. func setAutoLaunch(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. autoLaunch := arg["autoLaunch"].(bool)
  320. model.Conf.System.AutoLaunch = autoLaunch
  321. model.Conf.Save()
  322. }
  323. func setDownloadInstallPkg(c *gin.Context) {
  324. ret := gulu.Ret.NewResult()
  325. defer c.JSON(http.StatusOK, ret)
  326. arg, ok := util.JsonArg(c, ret)
  327. if !ok {
  328. return
  329. }
  330. downloadInstallPkg := arg["downloadInstallPkg"].(bool)
  331. model.Conf.System.DownloadInstallPkg = downloadInstallPkg
  332. model.Conf.Save()
  333. }
  334. func setNetworkProxy(c *gin.Context) {
  335. ret := gulu.Ret.NewResult()
  336. defer c.JSON(http.StatusOK, ret)
  337. arg, ok := util.JsonArg(c, ret)
  338. if !ok {
  339. return
  340. }
  341. scheme := arg["scheme"].(string)
  342. host := arg["host"].(string)
  343. port := arg["port"].(string)
  344. model.Conf.System.NetworkProxy = &conf.NetworkProxy{
  345. Scheme: scheme,
  346. Host: host,
  347. Port: port,
  348. }
  349. model.Conf.Save()
  350. proxyURL := model.Conf.System.NetworkProxy.String()
  351. util.SetNetworkProxy(proxyURL)
  352. util.PushMsg(model.Conf.Language(102), 3000)
  353. }
  354. func addUIProcess(c *gin.Context) {
  355. pid := c.Query("pid")
  356. util.UIProcessIDs.Store(pid, true)
  357. }
  358. func exit(c *gin.Context) {
  359. ret := gulu.Ret.NewResult()
  360. defer c.JSON(http.StatusOK, ret)
  361. arg, ok := util.JsonArg(c, ret)
  362. if !ok {
  363. return
  364. }
  365. forceArg := arg["force"]
  366. var force bool
  367. if nil != forceArg {
  368. force = forceArg.(bool)
  369. }
  370. execInstallPkgArg := arg["execInstallPkg"] // 0:默认检查新版本,1:不执行新版本安装,2:执行新版本安装
  371. execInstallPkg := 0
  372. if nil != execInstallPkgArg {
  373. execInstallPkg = int(execInstallPkgArg.(float64))
  374. }
  375. exitCode := model.Close(force, execInstallPkg)
  376. ret.Code = exitCode
  377. switch exitCode {
  378. case 0:
  379. case 1: // 同步执行失败
  380. ret.Msg = model.Conf.Language(96) + "<div class=\"fn__space\"></div><button class=\"b3-button b3-button--white\">" + model.Conf.Language(97) + "</button>"
  381. ret.Data = map[string]interface{}{"closeTimeout": 0}
  382. case 2: // 提示新安装包
  383. ret.Msg = model.Conf.Language(61)
  384. ret.Data = map[string]interface{}{"closeTimeout": 0}
  385. }
  386. }