system.go 7.5 KB

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