system.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. ret.Data = map[string]interface{}{
  135. "conf": maskedConf,
  136. "start": start,
  137. }
  138. if start {
  139. start = false
  140. }
  141. }
  142. func setUILayout(c *gin.Context) {
  143. ret := gulu.Ret.NewResult()
  144. defer c.JSON(http.StatusOK, ret)
  145. arg, ok := util.JsonArg(c, ret)
  146. if !ok {
  147. return
  148. }
  149. param, err := gulu.JSON.MarshalJSON(arg["layout"])
  150. if nil != err {
  151. ret.Code = -1
  152. ret.Msg = err.Error()
  153. return
  154. }
  155. uiLayout := &conf.UILayout{}
  156. if err = gulu.JSON.UnmarshalJSON(param, uiLayout); nil != err {
  157. ret.Code = -1
  158. ret.Msg = err.Error()
  159. return
  160. }
  161. model.Conf.UILayout = uiLayout
  162. model.Conf.Save()
  163. }
  164. func setAccessAuthCode(c *gin.Context) {
  165. ret := gulu.Ret.NewResult()
  166. defer c.JSON(http.StatusOK, ret)
  167. arg, ok := util.JsonArg(c, ret)
  168. if !ok {
  169. return
  170. }
  171. aac := arg["accessAuthCode"].(string)
  172. if model.MaskedAccessAuthCode == aac {
  173. aac = model.Conf.AccessAuthCode
  174. }
  175. model.Conf.AccessAuthCode = aac
  176. model.Conf.Save()
  177. session := util.GetSession(c)
  178. session.AccessAuthCode = aac
  179. session.Save(c)
  180. go func() {
  181. time.Sleep(200 * time.Millisecond)
  182. util.ReloadUI()
  183. }()
  184. return
  185. }
  186. func getSysFonts(c *gin.Context) {
  187. ret := gulu.Ret.NewResult()
  188. defer c.JSON(http.StatusOK, ret)
  189. ret.Data = util.GetSysFonts(model.Conf.Lang)
  190. }
  191. func version(c *gin.Context) {
  192. ret := gulu.Ret.NewResult()
  193. defer c.JSON(http.StatusOK, ret)
  194. ret.Data = util.Ver
  195. }
  196. func currentTime(c *gin.Context) {
  197. ret := gulu.Ret.NewResult()
  198. defer c.JSON(http.StatusOK, ret)
  199. ret.Data = util.CurrentTimeMillis()
  200. }
  201. func bootProgress(c *gin.Context) {
  202. ret := gulu.Ret.NewResult()
  203. defer c.JSON(http.StatusOK, ret)
  204. progress, details := util.GetBootProgressDetails()
  205. ret.Data = map[string]interface{}{"progress": progress, "details": details}
  206. }
  207. func setAppearanceMode(c *gin.Context) {
  208. ret := gulu.Ret.NewResult()
  209. defer c.JSON(http.StatusOK, ret)
  210. arg, ok := util.JsonArg(c, ret)
  211. if !ok {
  212. return
  213. }
  214. mode := int(arg["mode"].(float64))
  215. model.Conf.Appearance.Mode = mode
  216. if 0 == mode {
  217. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeLight, "theme.js"))
  218. } else {
  219. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeDark, "theme.js"))
  220. }
  221. model.Conf.Save()
  222. ret.Data = map[string]interface{}{
  223. "appearance": model.Conf.Appearance,
  224. }
  225. }
  226. func setNetworkServe(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. networkServe := arg["networkServe"].(bool)
  234. model.Conf.System.NetworkServe = networkServe
  235. model.Conf.Save()
  236. util.PushMsg(model.Conf.Language(42), 1000*15)
  237. time.Sleep(time.Second * 3)
  238. }
  239. func setGoogleAnalytics(c *gin.Context) {
  240. ret := gulu.Ret.NewResult()
  241. defer c.JSON(http.StatusOK, ret)
  242. arg, ok := util.JsonArg(c, ret)
  243. if !ok {
  244. return
  245. }
  246. googleAnalytics := arg["googleAnalytics"].(bool)
  247. model.Conf.System.DisableGoogleAnalytics = !googleAnalytics
  248. model.Conf.Save()
  249. }
  250. func setUploadErrLog(c *gin.Context) {
  251. ret := gulu.Ret.NewResult()
  252. defer c.JSON(http.StatusOK, ret)
  253. arg, ok := util.JsonArg(c, ret)
  254. if !ok {
  255. return
  256. }
  257. uploadErrLog := arg["uploadErrLog"].(bool)
  258. model.Conf.System.UploadErrLog = uploadErrLog
  259. model.Conf.Save()
  260. util.PushMsg(model.Conf.Language(42), 1000*15)
  261. time.Sleep(time.Second * 3)
  262. }
  263. func setAutoLaunch(c *gin.Context) {
  264. ret := gulu.Ret.NewResult()
  265. defer c.JSON(http.StatusOK, ret)
  266. arg, ok := util.JsonArg(c, ret)
  267. if !ok {
  268. return
  269. }
  270. autoLaunch := arg["autoLaunch"].(bool)
  271. model.Conf.System.AutoLaunch = autoLaunch
  272. model.Conf.Save()
  273. }
  274. func setDownloadInstallPkg(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. downloadInstallPkg := arg["downloadInstallPkg"].(bool)
  282. model.Conf.System.DownloadInstallPkg = downloadInstallPkg
  283. model.Conf.Save()
  284. }
  285. func setNetworkProxy(c *gin.Context) {
  286. ret := gulu.Ret.NewResult()
  287. defer c.JSON(http.StatusOK, ret)
  288. arg, ok := util.JsonArg(c, ret)
  289. if !ok {
  290. return
  291. }
  292. scheme := arg["scheme"].(string)
  293. host := arg["host"].(string)
  294. port := arg["port"].(string)
  295. model.Conf.System.NetworkProxy = &conf.NetworkProxy{
  296. Scheme: scheme,
  297. Host: host,
  298. Port: port,
  299. }
  300. model.Conf.Save()
  301. proxyURL := model.Conf.System.NetworkProxy.String()
  302. util.SetNetworkProxy(proxyURL)
  303. util.PushMsg(model.Conf.Language(102), 3000)
  304. }
  305. func addUIProcess(c *gin.Context) {
  306. pid := c.Query("pid")
  307. util.UIProcessIDs.Store(pid, true)
  308. }
  309. func exit(c *gin.Context) {
  310. ret := gulu.Ret.NewResult()
  311. defer c.JSON(http.StatusOK, ret)
  312. arg, ok := util.JsonArg(c, ret)
  313. if !ok {
  314. return
  315. }
  316. forceArg := arg["force"]
  317. var force bool
  318. if nil != forceArg {
  319. force = forceArg.(bool)
  320. }
  321. execInstallPkgArg := arg["execInstallPkg"] // 0:默认检查新版本,1:不执行新版本安装,2:执行新版本安装
  322. execInstallPkg := 0
  323. if nil != execInstallPkgArg {
  324. execInstallPkg = int(execInstallPkgArg.(float64))
  325. }
  326. exitCode := model.Close(force, execInstallPkg)
  327. ret.Code = exitCode
  328. switch exitCode {
  329. case 0:
  330. case 1: // 同步执行失败
  331. ret.Msg = model.Conf.Language(96) + "<div class=\"fn__space\"></div><button class=\"b3-button b3-button--white\">" + model.Conf.Language(97) + "</button>"
  332. ret.Data = map[string]interface{}{"closeTimeout": 0}
  333. case 2: // 提示新安装包
  334. ret.Msg = model.Conf.Language(61)
  335. ret.Data = map[string]interface{}{"closeTimeout": 0}
  336. }
  337. }