system.go 9.4 KB

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