system.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. func getConf(c *gin.Context) {
  116. ret := gulu.Ret.NewResult()
  117. defer c.JSON(http.StatusOK, ret)
  118. ret.Data = model.Conf
  119. }
  120. func setUILayout(c *gin.Context) {
  121. ret := gulu.Ret.NewResult()
  122. defer c.JSON(http.StatusOK, ret)
  123. arg, ok := util.JsonArg(c, ret)
  124. if !ok {
  125. return
  126. }
  127. param, err := gulu.JSON.MarshalJSON(arg["layout"])
  128. if nil != err {
  129. ret.Code = -1
  130. ret.Msg = err.Error()
  131. return
  132. }
  133. uiLayout := &conf.UILayout{}
  134. if err = gulu.JSON.UnmarshalJSON(param, uiLayout); nil != err {
  135. ret.Code = -1
  136. ret.Msg = err.Error()
  137. return
  138. }
  139. model.Conf.UILayout = uiLayout
  140. model.Conf.Save()
  141. }
  142. func setAccessAuthCode(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. aac := arg["accessAuthCode"].(string)
  150. model.Conf.AccessAuthCode = aac
  151. model.Conf.Save()
  152. session := util.GetSession(c)
  153. session.AccessAuthCode = aac
  154. session.Save(c)
  155. go func() {
  156. time.Sleep(200 * time.Millisecond)
  157. util.ReloadUI()
  158. }()
  159. return
  160. }
  161. func getSysFonts(c *gin.Context) {
  162. ret := gulu.Ret.NewResult()
  163. defer c.JSON(http.StatusOK, ret)
  164. ret.Data = util.GetSysFonts(model.Conf.Lang)
  165. }
  166. func version(c *gin.Context) {
  167. ret := gulu.Ret.NewResult()
  168. defer c.JSON(http.StatusOK, ret)
  169. ret.Data = util.Ver
  170. }
  171. func currentTime(c *gin.Context) {
  172. ret := gulu.Ret.NewResult()
  173. defer c.JSON(http.StatusOK, ret)
  174. ret.Data = util.CurrentTimeMillis()
  175. }
  176. func bootProgress(c *gin.Context) {
  177. ret := gulu.Ret.NewResult()
  178. defer c.JSON(http.StatusOK, ret)
  179. progress, details := util.GetBootProgressDetails()
  180. ret.Data = map[string]interface{}{"progress": progress, "details": details}
  181. }
  182. func setAppearanceMode(c *gin.Context) {
  183. ret := gulu.Ret.NewResult()
  184. defer c.JSON(http.StatusOK, ret)
  185. arg, ok := util.JsonArg(c, ret)
  186. if !ok {
  187. return
  188. }
  189. mode := int(arg["mode"].(float64))
  190. model.Conf.Appearance.Mode = mode
  191. if 0 == mode {
  192. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeLight, "theme.js"))
  193. } else {
  194. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeDark, "theme.js"))
  195. }
  196. model.Conf.Save()
  197. ret.Data = map[string]interface{}{
  198. "appearance": model.Conf.Appearance,
  199. }
  200. }
  201. func setNetworkServe(c *gin.Context) {
  202. ret := gulu.Ret.NewResult()
  203. defer c.JSON(http.StatusOK, ret)
  204. arg, ok := util.JsonArg(c, ret)
  205. if !ok {
  206. return
  207. }
  208. networkServe := arg["networkServe"].(bool)
  209. model.Conf.System.NetworkServe = networkServe
  210. model.Conf.Save()
  211. util.PushMsg(model.Conf.Language(42), 1000*15)
  212. time.Sleep(time.Second * 3)
  213. }
  214. func setUploadErrLog(c *gin.Context) {
  215. ret := gulu.Ret.NewResult()
  216. defer c.JSON(http.StatusOK, ret)
  217. arg, ok := util.JsonArg(c, ret)
  218. if !ok {
  219. return
  220. }
  221. uploadErrLog := arg["uploadErrLog"].(bool)
  222. model.Conf.System.UploadErrLog = uploadErrLog
  223. model.Conf.Save()
  224. util.PushMsg(model.Conf.Language(42), 1000*15)
  225. time.Sleep(time.Second * 3)
  226. }
  227. func setNetworkProxy(c *gin.Context) {
  228. ret := gulu.Ret.NewResult()
  229. defer c.JSON(http.StatusOK, ret)
  230. arg, ok := util.JsonArg(c, ret)
  231. if !ok {
  232. return
  233. }
  234. scheme := arg["scheme"].(string)
  235. host := arg["host"].(string)
  236. port := arg["port"].(string)
  237. model.Conf.System.NetworkProxy = &conf.NetworkProxy{
  238. Scheme: scheme,
  239. Host: host,
  240. Port: port,
  241. }
  242. model.Conf.Save()
  243. util.PushMsg(model.Conf.Language(42), 1000*15)
  244. time.Sleep(time.Second * 3)
  245. }
  246. func addUIProcess(c *gin.Context) {
  247. pid := c.Query("pid")
  248. util.UIProcessIDs.Store(pid, true)
  249. }
  250. func exit(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. forceArg := arg["force"]
  258. var force bool
  259. if nil != forceArg {
  260. force = forceArg.(bool)
  261. }
  262. err := model.Close(force)
  263. if nil != err {
  264. ret.Code = 1
  265. ret.Msg = err.Error() + "<div class=\"fn__space\"></div><button class=\"b3-button b3-button--white\">" + model.Conf.Language(97) + "</button>"
  266. ret.Data = map[string]interface{}{"closeTimeout": 0}
  267. return
  268. }
  269. }