system.go 11 KB

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