system.go 9.7 KB

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