system.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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/filelock"
  27. "github.com/siyuan-note/logging"
  28. "github.com/siyuan-note/siyuan/kernel/conf"
  29. "github.com/siyuan-note/siyuan/kernel/model"
  30. "github.com/siyuan-note/siyuan/kernel/util"
  31. )
  32. func getEmojiConf(c *gin.Context) {
  33. ret := gulu.Ret.NewResult()
  34. defer c.JSON(http.StatusOK, ret)
  35. builtConfPath := filepath.Join(util.AppearancePath, "emojis", "conf.json")
  36. data, err := os.ReadFile(builtConfPath)
  37. if nil != err {
  38. logging.LogErrorf("read emojis conf.json failed: %s", err)
  39. ret.Code = -1
  40. ret.Msg = err.Error()
  41. return
  42. }
  43. var conf []map[string]interface{}
  44. if err = gulu.JSON.UnmarshalJSON(data, &conf); nil != err {
  45. logging.LogErrorf("unmarshal emojis conf.json failed: %s", err)
  46. ret.Code = -1
  47. ret.Msg = err.Error()
  48. return
  49. }
  50. customConfDir := filepath.Join(util.DataDir, "emojis")
  51. custom := map[string]interface{}{
  52. "id": "custom",
  53. "title": "Custom",
  54. "title_zh_cn": "自定义",
  55. }
  56. items := []map[string]interface{}{}
  57. custom["items"] = items
  58. if gulu.File.IsDir(customConfDir) {
  59. model.CustomEmojis = sync.Map{}
  60. customEmojis, err := os.ReadDir(customConfDir)
  61. if nil != err {
  62. logging.LogErrorf("read custom emojis failed: %s", err)
  63. } else {
  64. for _, customEmoji := range customEmojis {
  65. name := customEmoji.Name()
  66. if strings.HasPrefix(name, ".") {
  67. continue
  68. }
  69. if customEmoji.IsDir() {
  70. // 子级
  71. subCustomEmojis, err := os.ReadDir(filepath.Join(customConfDir, name))
  72. if nil != err {
  73. logging.LogErrorf("read custom emojis failed: %s", err)
  74. continue
  75. }
  76. for _, subCustomEmoji := range subCustomEmojis {
  77. name = subCustomEmoji.Name()
  78. if strings.HasPrefix(name, ".") {
  79. continue
  80. }
  81. addCustomEmoji(customEmoji.Name()+"/"+name, &items)
  82. }
  83. continue
  84. }
  85. addCustomEmoji(name, &items)
  86. }
  87. }
  88. }
  89. custom["items"] = items
  90. conf = append([]map[string]interface{}{custom}, conf...)
  91. ret.Data = conf
  92. return
  93. }
  94. func addCustomEmoji(name string, items *[]map[string]interface{}) {
  95. ext := filepath.Ext(name)
  96. nameWithoutExt := strings.TrimSuffix(name, ext)
  97. emoji := map[string]interface{}{
  98. "unicode": name,
  99. "description": nameWithoutExt,
  100. "description_zh_cn": nameWithoutExt,
  101. "keywords": nameWithoutExt,
  102. }
  103. *items = append(*items, emoji)
  104. imgSrc := "/emojis/" + name
  105. model.CustomEmojis.Store(nameWithoutExt, imgSrc)
  106. }
  107. func checkUpdate(c *gin.Context) {
  108. ret := gulu.Ret.NewResult()
  109. defer c.JSON(http.StatusOK, ret)
  110. arg, ok := util.JsonArg(c, ret)
  111. if !ok {
  112. return
  113. }
  114. showMsg := arg["showMsg"].(bool)
  115. model.CheckUpdate(showMsg)
  116. }
  117. func exportLog(c *gin.Context) {
  118. ret := gulu.Ret.NewResult()
  119. defer c.JSON(http.StatusOK, ret)
  120. zipPath := model.ExportSystemLog()
  121. ret.Data = map[string]interface{}{
  122. "zip": zipPath,
  123. }
  124. }
  125. var start = true // 是否是启动
  126. func getConf(c *gin.Context) {
  127. ret := gulu.Ret.NewResult()
  128. defer c.JSON(http.StatusOK, ret)
  129. maskedConf, err := model.GetMaskedConf()
  130. if nil != err {
  131. ret.Code = -1
  132. ret.Msg = "get conf failed: " + err.Error()
  133. return
  134. }
  135. ret.Data = map[string]interface{}{
  136. "conf": maskedConf,
  137. "start": start,
  138. }
  139. if start {
  140. start = false
  141. }
  142. }
  143. func getLocalStorage(c *gin.Context) {
  144. ret := gulu.Ret.NewResult()
  145. defer c.JSON(http.StatusOK, ret)
  146. arg, ok := util.JsonArg(c, ret)
  147. if !ok {
  148. return
  149. }
  150. key := arg["key"].(string)
  151. lsPath := filepath.Join(util.DataDir, "storage/local.json")
  152. if !gulu.File.IsExist(lsPath) {
  153. return
  154. }
  155. data, err := filelock.ReadFile(lsPath)
  156. if nil != err {
  157. ret.Code = -1
  158. ret.Msg = err.Error()
  159. return
  160. }
  161. ls := map[string]interface{}{}
  162. if err = gulu.JSON.UnmarshalJSON(data, &ls); nil != err {
  163. ret.Code = -1
  164. ret.Msg = err.Error()
  165. return
  166. }
  167. value := ls[key]
  168. ret.Data = value
  169. }
  170. func setLocalStorage(c *gin.Context) {
  171. ret := gulu.Ret.NewResult()
  172. defer c.JSON(http.StatusOK, ret)
  173. arg, ok := util.JsonArg(c, ret)
  174. if !ok {
  175. return
  176. }
  177. val := arg["val"].(interface{})
  178. dirPath := filepath.Join(util.DataDir, "storage")
  179. if err := os.MkdirAll(dirPath, 0755); nil != err {
  180. ret.Code = -1
  181. ret.Msg = err.Error()
  182. return
  183. }
  184. data, err := gulu.JSON.MarshalJSON(val)
  185. if nil != err {
  186. ret.Code = -1
  187. ret.Msg = err.Error()
  188. return
  189. }
  190. lsPath := filepath.Join(dirPath, "local.json")
  191. err = filelock.WriteFile(lsPath, data)
  192. if nil != err {
  193. ret.Code = -1
  194. ret.Msg = err.Error()
  195. return
  196. }
  197. }
  198. func setUILayout(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. param, err := gulu.JSON.MarshalJSON(arg["layout"])
  206. if nil != err {
  207. ret.Code = -1
  208. ret.Msg = err.Error()
  209. return
  210. }
  211. uiLayout := &conf.UILayout{}
  212. if err = gulu.JSON.UnmarshalJSON(param, uiLayout); nil != err {
  213. ret.Code = -1
  214. ret.Msg = err.Error()
  215. return
  216. }
  217. model.Conf.UILayout = uiLayout
  218. model.Conf.Save()
  219. }
  220. func setAccessAuthCode(c *gin.Context) {
  221. ret := gulu.Ret.NewResult()
  222. defer c.JSON(http.StatusOK, ret)
  223. arg, ok := util.JsonArg(c, ret)
  224. if !ok {
  225. return
  226. }
  227. aac := arg["accessAuthCode"].(string)
  228. if model.MaskedAccessAuthCode == aac {
  229. aac = model.Conf.AccessAuthCode
  230. }
  231. model.Conf.AccessAuthCode = aac
  232. model.Conf.Save()
  233. session := util.GetSession(c)
  234. session.AccessAuthCode = aac
  235. session.Save(c)
  236. go func() {
  237. time.Sleep(200 * time.Millisecond)
  238. util.ReloadUI()
  239. }()
  240. return
  241. }
  242. func getSysFonts(c *gin.Context) {
  243. ret := gulu.Ret.NewResult()
  244. defer c.JSON(http.StatusOK, ret)
  245. ret.Data = util.GetSysFonts(model.Conf.Lang)
  246. }
  247. func version(c *gin.Context) {
  248. ret := gulu.Ret.NewResult()
  249. defer c.JSON(http.StatusOK, ret)
  250. ret.Data = util.Ver
  251. }
  252. func currentTime(c *gin.Context) {
  253. ret := gulu.Ret.NewResult()
  254. defer c.JSON(http.StatusOK, ret)
  255. ret.Data = util.CurrentTimeMillis()
  256. }
  257. func bootProgress(c *gin.Context) {
  258. ret := gulu.Ret.NewResult()
  259. defer c.JSON(http.StatusOK, ret)
  260. progress, details := util.GetBootProgressDetails()
  261. ret.Data = map[string]interface{}{"progress": progress, "details": details}
  262. }
  263. func setAppearanceMode(c *gin.Context) {
  264. ret := gulu.Ret.NewResult()
  265. defer c.JSON(http.StatusOK, ret)
  266. arg, ok := util.JsonArg(c, ret)
  267. if !ok {
  268. return
  269. }
  270. mode := int(arg["mode"].(float64))
  271. model.Conf.Appearance.Mode = mode
  272. if 0 == mode {
  273. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeLight, "theme.js"))
  274. } else {
  275. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeDark, "theme.js"))
  276. }
  277. model.Conf.Save()
  278. ret.Data = map[string]interface{}{
  279. "appearance": model.Conf.Appearance,
  280. }
  281. }
  282. func setNetworkServe(c *gin.Context) {
  283. ret := gulu.Ret.NewResult()
  284. defer c.JSON(http.StatusOK, ret)
  285. arg, ok := util.JsonArg(c, ret)
  286. if !ok {
  287. return
  288. }
  289. networkServe := arg["networkServe"].(bool)
  290. model.Conf.System.NetworkServe = networkServe
  291. model.Conf.Save()
  292. util.PushMsg(model.Conf.Language(42), 1000*15)
  293. time.Sleep(time.Second * 3)
  294. }
  295. func setFixedPort(c *gin.Context) {
  296. ret := gulu.Ret.NewResult()
  297. defer c.JSON(http.StatusOK, ret)
  298. arg, ok := util.JsonArg(c, ret)
  299. if !ok {
  300. return
  301. }
  302. fixedPort := arg["fixedPort"].(bool)
  303. model.Conf.System.FixedPort = fixedPort
  304. model.Conf.Save()
  305. util.PushMsg(model.Conf.Language(42), 1000*15)
  306. time.Sleep(time.Second * 3)
  307. }
  308. func setGoogleAnalytics(c *gin.Context) {
  309. ret := gulu.Ret.NewResult()
  310. defer c.JSON(http.StatusOK, ret)
  311. arg, ok := util.JsonArg(c, ret)
  312. if !ok {
  313. return
  314. }
  315. googleAnalytics := arg["googleAnalytics"].(bool)
  316. model.Conf.System.DisableGoogleAnalytics = !googleAnalytics
  317. model.Conf.Save()
  318. }
  319. func setUploadErrLog(c *gin.Context) {
  320. ret := gulu.Ret.NewResult()
  321. defer c.JSON(http.StatusOK, ret)
  322. arg, ok := util.JsonArg(c, ret)
  323. if !ok {
  324. return
  325. }
  326. uploadErrLog := arg["uploadErrLog"].(bool)
  327. model.Conf.System.UploadErrLog = uploadErrLog
  328. model.Conf.Save()
  329. util.PushMsg(model.Conf.Language(42), 1000*15)
  330. time.Sleep(time.Second * 3)
  331. }
  332. func setDownloadInstallPkg(c *gin.Context) {
  333. ret := gulu.Ret.NewResult()
  334. defer c.JSON(http.StatusOK, ret)
  335. arg, ok := util.JsonArg(c, ret)
  336. if !ok {
  337. return
  338. }
  339. downloadInstallPkg := arg["downloadInstallPkg"].(bool)
  340. model.Conf.System.DownloadInstallPkg = downloadInstallPkg
  341. model.Conf.Save()
  342. }
  343. func setNetworkProxy(c *gin.Context) {
  344. ret := gulu.Ret.NewResult()
  345. defer c.JSON(http.StatusOK, ret)
  346. arg, ok := util.JsonArg(c, ret)
  347. if !ok {
  348. return
  349. }
  350. scheme := arg["scheme"].(string)
  351. host := arg["host"].(string)
  352. port := arg["port"].(string)
  353. model.Conf.System.NetworkProxy = &conf.NetworkProxy{
  354. Scheme: scheme,
  355. Host: host,
  356. Port: port,
  357. }
  358. model.Conf.Save()
  359. util.PushMsg(model.Conf.Language(42), 1000*15)
  360. time.Sleep(time.Second * 3)
  361. }
  362. func addUIProcess(c *gin.Context) {
  363. pid := c.Query("pid")
  364. util.UIProcessIDs.Store(pid, true)
  365. }
  366. func exit(c *gin.Context) {
  367. ret := gulu.Ret.NewResult()
  368. defer c.JSON(http.StatusOK, ret)
  369. arg, ok := util.JsonArg(c, ret)
  370. if !ok {
  371. return
  372. }
  373. forceArg := arg["force"]
  374. var force bool
  375. if nil != forceArg {
  376. force = forceArg.(bool)
  377. }
  378. execInstallPkgArg := arg["execInstallPkg"] // 0:默认检查新版本,1:不执行新版本安装,2:执行新版本安装
  379. execInstallPkg := 0
  380. if nil != execInstallPkgArg {
  381. execInstallPkg = int(execInstallPkgArg.(float64))
  382. }
  383. exitCode := model.Close(force, execInstallPkg)
  384. ret.Code = exitCode
  385. switch exitCode {
  386. case 0:
  387. case 1: // 同步执行失败
  388. ret.Msg = model.Conf.Language(96) + "<div class=\"fn__space\"></div><button class=\"b3-button b3-button--white\">" + model.Conf.Language(97) + "</button>"
  389. ret.Data = map[string]interface{}{"closeTimeout": 0}
  390. case 2: // 提示新安装包
  391. ret.Msg = model.Conf.Language(61)
  392. ret.Data = map[string]interface{}{"closeTimeout": 0}
  393. }
  394. }