system.go 7.7 KB

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