system.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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/siyuan/kernel/conf"
  27. "github.com/siyuan-note/siyuan/kernel/model"
  28. "github.com/siyuan-note/siyuan/kernel/util"
  29. )
  30. func getEmojiConf(c *gin.Context) {
  31. ret := gulu.Ret.NewResult()
  32. defer c.JSON(http.StatusOK, ret)
  33. builtConfPath := filepath.Join(util.AppearancePath, "emojis", "conf.json")
  34. data, err := os.ReadFile(builtConfPath)
  35. if nil != err {
  36. util.LogErrorf("read emojis conf.json failed: %s", err)
  37. ret.Code = -1
  38. ret.Msg = err.Error()
  39. return
  40. }
  41. var conf []map[string]interface{}
  42. if err = gulu.JSON.UnmarshalJSON(data, &conf); nil != err {
  43. util.LogErrorf("unmarshal emojis conf.json failed: %s", err)
  44. ret.Code = -1
  45. ret.Msg = err.Error()
  46. return
  47. }
  48. customConfDir := filepath.Join(util.DataDir, "emojis")
  49. custom := map[string]interface{}{
  50. "id": "custom",
  51. "title": "Custom",
  52. "title_zh_cn": "自定义",
  53. }
  54. items := []map[string]interface{}{}
  55. custom["items"] = items
  56. if gulu.File.IsDir(customConfDir) {
  57. model.CustomEmojis = sync.Map{}
  58. customEmojis, err := os.ReadDir(customConfDir)
  59. if nil != err {
  60. util.LogErrorf("read custom emojis failed: %s", err)
  61. } else {
  62. for _, customEmoji := range customEmojis {
  63. name := customEmoji.Name()
  64. if strings.HasPrefix(name, ".") {
  65. continue
  66. }
  67. if customEmoji.IsDir() {
  68. // 子级
  69. subCustomEmojis, err := os.ReadDir(filepath.Join(customConfDir, name))
  70. if nil != err {
  71. util.LogErrorf("read custom emojis failed: %s", err)
  72. continue
  73. }
  74. for _, subCustomEmoji := range subCustomEmojis {
  75. name = subCustomEmoji.Name()
  76. if strings.HasPrefix(name, ".") {
  77. continue
  78. }
  79. addCustomEmoji(customEmoji.Name()+"/"+name, &items)
  80. }
  81. continue
  82. }
  83. addCustomEmoji(name, &items)
  84. }
  85. }
  86. }
  87. custom["items"] = items
  88. conf = append([]map[string]interface{}{custom}, conf...)
  89. ret.Data = conf
  90. return
  91. }
  92. func addCustomEmoji(name string, items *[]map[string]interface{}) {
  93. ext := filepath.Ext(name)
  94. nameWithoutExt := strings.TrimSuffix(name, ext)
  95. emoji := map[string]interface{}{
  96. "unicode": name,
  97. "description": nameWithoutExt,
  98. "description_zh_cn": nameWithoutExt,
  99. "keywords": nameWithoutExt,
  100. }
  101. *items = append(*items, emoji)
  102. imgSrc := "/emojis/" + name
  103. model.CustomEmojis.Store(nameWithoutExt, imgSrc)
  104. }
  105. func checkUpdate(c *gin.Context) {
  106. ret := gulu.Ret.NewResult()
  107. defer c.JSON(http.StatusOK, ret)
  108. arg, ok := util.JsonArg(c, ret)
  109. if !ok {
  110. return
  111. }
  112. showMsg := arg["showMsg"].(bool)
  113. model.CheckUpdate(showMsg)
  114. }
  115. var start = true // 是否是启动
  116. func getConf(c *gin.Context) {
  117. ret := gulu.Ret.NewResult()
  118. defer c.JSON(http.StatusOK, ret)
  119. ret.Data = map[string]interface{}{
  120. "conf": model.Conf,
  121. "start": start,
  122. }
  123. if start {
  124. start = false
  125. }
  126. }
  127. func setUILayout(c *gin.Context) {
  128. ret := gulu.Ret.NewResult()
  129. defer c.JSON(http.StatusOK, ret)
  130. arg, ok := util.JsonArg(c, ret)
  131. if !ok {
  132. return
  133. }
  134. param, err := gulu.JSON.MarshalJSON(arg["layout"])
  135. if nil != err {
  136. ret.Code = -1
  137. ret.Msg = err.Error()
  138. return
  139. }
  140. uiLayout := &conf.UILayout{}
  141. if err = gulu.JSON.UnmarshalJSON(param, uiLayout); nil != err {
  142. ret.Code = -1
  143. ret.Msg = err.Error()
  144. return
  145. }
  146. model.Conf.UILayout = uiLayout
  147. model.Conf.Save()
  148. }
  149. func setAccessAuthCode(c *gin.Context) {
  150. ret := gulu.Ret.NewResult()
  151. defer c.JSON(http.StatusOK, ret)
  152. arg, ok := util.JsonArg(c, ret)
  153. if !ok {
  154. return
  155. }
  156. aac := arg["accessAuthCode"].(string)
  157. model.Conf.AccessAuthCode = aac
  158. model.Conf.Save()
  159. session := util.GetSession(c)
  160. session.AccessAuthCode = aac
  161. session.Save(c)
  162. go func() {
  163. time.Sleep(200 * time.Millisecond)
  164. util.ReloadUI()
  165. }()
  166. return
  167. }
  168. func getSysFonts(c *gin.Context) {
  169. ret := gulu.Ret.NewResult()
  170. defer c.JSON(http.StatusOK, ret)
  171. ret.Data = util.GetSysFonts(model.Conf.Lang)
  172. }
  173. func version(c *gin.Context) {
  174. ret := gulu.Ret.NewResult()
  175. defer c.JSON(http.StatusOK, ret)
  176. ret.Data = util.Ver
  177. }
  178. func currentTime(c *gin.Context) {
  179. ret := gulu.Ret.NewResult()
  180. defer c.JSON(http.StatusOK, ret)
  181. ret.Data = util.CurrentTimeMillis()
  182. }
  183. func bootProgress(c *gin.Context) {
  184. ret := gulu.Ret.NewResult()
  185. defer c.JSON(http.StatusOK, ret)
  186. progress, details := util.GetBootProgressDetails()
  187. ret.Data = map[string]interface{}{"progress": progress, "details": details}
  188. }
  189. func setAppearanceMode(c *gin.Context) {
  190. ret := gulu.Ret.NewResult()
  191. defer c.JSON(http.StatusOK, ret)
  192. arg, ok := util.JsonArg(c, ret)
  193. if !ok {
  194. return
  195. }
  196. mode := int(arg["mode"].(float64))
  197. model.Conf.Appearance.Mode = mode
  198. if 0 == mode {
  199. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeLight, "theme.js"))
  200. } else {
  201. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeDark, "theme.js"))
  202. }
  203. model.Conf.Save()
  204. ret.Data = map[string]interface{}{
  205. "appearance": model.Conf.Appearance,
  206. }
  207. }
  208. func setNetworkServe(c *gin.Context) {
  209. ret := gulu.Ret.NewResult()
  210. defer c.JSON(http.StatusOK, ret)
  211. arg, ok := util.JsonArg(c, ret)
  212. if !ok {
  213. return
  214. }
  215. networkServe := arg["networkServe"].(bool)
  216. model.Conf.System.NetworkServe = networkServe
  217. model.Conf.Save()
  218. util.PushMsg(model.Conf.Language(42), 1000*15)
  219. time.Sleep(time.Second * 3)
  220. }
  221. func setUploadErrLog(c *gin.Context) {
  222. ret := gulu.Ret.NewResult()
  223. defer c.JSON(http.StatusOK, ret)
  224. arg, ok := util.JsonArg(c, ret)
  225. if !ok {
  226. return
  227. }
  228. uploadErrLog := arg["uploadErrLog"].(bool)
  229. model.Conf.System.UploadErrLog = uploadErrLog
  230. model.Conf.Save()
  231. util.PushMsg(model.Conf.Language(42), 1000*15)
  232. time.Sleep(time.Second * 3)
  233. }
  234. func setNetworkProxy(c *gin.Context) {
  235. ret := gulu.Ret.NewResult()
  236. defer c.JSON(http.StatusOK, ret)
  237. arg, ok := util.JsonArg(c, ret)
  238. if !ok {
  239. return
  240. }
  241. scheme := arg["scheme"].(string)
  242. host := arg["host"].(string)
  243. port := arg["port"].(string)
  244. model.Conf.System.NetworkProxy = &conf.NetworkProxy{
  245. Scheme: scheme,
  246. Host: host,
  247. Port: port,
  248. }
  249. model.Conf.Save()
  250. util.PushMsg(model.Conf.Language(42), 1000*15)
  251. time.Sleep(time.Second * 3)
  252. }
  253. func addUIProcess(c *gin.Context) {
  254. pid := c.Query("pid")
  255. util.UIProcessIDs.Store(pid, true)
  256. }
  257. func exit(c *gin.Context) {
  258. ret := gulu.Ret.NewResult()
  259. defer c.JSON(http.StatusOK, ret)
  260. arg, ok := util.JsonArg(c, ret)
  261. if !ok {
  262. return
  263. }
  264. forceArg := arg["force"]
  265. var force bool
  266. if nil != forceArg {
  267. force = forceArg.(bool)
  268. }
  269. err := model.Close(force)
  270. if nil != err {
  271. ret.Code = 1
  272. ret.Msg = err.Error() + "<div class=\"fn__space\"></div><button class=\"b3-button b3-button--white\">" + model.Conf.Language(97) + "</button>"
  273. ret.Data = map[string]interface{}{"closeTimeout": 0}
  274. return
  275. }
  276. }