system.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // SiYuan - Refactor your thinking
  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. "github.com/jinzhu/copier"
  19. "net/http"
  20. "os"
  21. "path/filepath"
  22. "strings"
  23. "sync"
  24. "time"
  25. "github.com/88250/lute"
  26. "github.com/88250/gulu"
  27. "github.com/gin-gonic/gin"
  28. "github.com/siyuan-note/logging"
  29. "github.com/siyuan-note/siyuan/kernel/conf"
  30. "github.com/siyuan-note/siyuan/kernel/model"
  31. "github.com/siyuan-note/siyuan/kernel/util"
  32. )
  33. func getNetwork(c *gin.Context) {
  34. ret := gulu.Ret.NewResult()
  35. defer c.JSON(http.StatusOK, ret)
  36. maskedConf, err := model.GetMaskedConf()
  37. if err != nil {
  38. ret.Code = -1
  39. ret.Msg = "get conf failed: " + err.Error()
  40. return
  41. }
  42. ret.Data = map[string]interface{}{
  43. "proxy": maskedConf.System.NetworkProxy,
  44. }
  45. }
  46. func getChangelog(c *gin.Context) {
  47. ret := gulu.Ret.NewResult()
  48. defer c.JSON(http.StatusOK, ret)
  49. data := map[string]interface{}{"show": false, "html": ""}
  50. ret.Data = data
  51. changelogsDir := filepath.Join(util.WorkingDir, "changelogs")
  52. if !gulu.File.IsDir(changelogsDir) {
  53. return
  54. }
  55. if !model.Conf.ShowChangelog {
  56. return
  57. }
  58. changelogPath := filepath.Join(changelogsDir, "v"+util.Ver, "v"+util.Ver+"_"+model.Conf.Lang+".md")
  59. if !gulu.File.IsExist(changelogPath) {
  60. changelogPath = filepath.Join(changelogsDir, "v"+util.Ver, "v"+util.Ver+".md")
  61. if !gulu.File.IsExist(changelogPath) {
  62. logging.LogErrorf("changelog not found: %s", changelogPath)
  63. return
  64. }
  65. }
  66. contentData, err := os.ReadFile(changelogPath)
  67. if err != nil {
  68. logging.LogErrorf("read changelog failed: %s", err)
  69. return
  70. }
  71. model.Conf.ShowChangelog = false
  72. luteEngine := lute.New()
  73. htmlContent := luteEngine.MarkdownStr("", string(contentData))
  74. htmlContent = util.LinkTarget(htmlContent, "")
  75. data["show"] = true
  76. data["html"] = htmlContent
  77. ret.Data = data
  78. }
  79. func getEmojiConf(c *gin.Context) {
  80. ret := gulu.Ret.NewResult()
  81. defer c.JSON(http.StatusOK, ret)
  82. builtConfPath := filepath.Join(util.AppearancePath, "emojis", "conf.json")
  83. data, err := os.ReadFile(builtConfPath)
  84. if err != nil {
  85. logging.LogErrorf("read emojis conf.json failed: %s", err)
  86. ret.Code = -1
  87. ret.Msg = err.Error()
  88. return
  89. }
  90. var conf []map[string]interface{}
  91. if err = gulu.JSON.UnmarshalJSON(data, &conf); err != nil {
  92. logging.LogErrorf("unmarshal emojis conf.json failed: %s", err)
  93. ret.Code = -1
  94. ret.Msg = err.Error()
  95. return
  96. }
  97. customConfDir := filepath.Join(util.DataDir, "emojis")
  98. custom := map[string]interface{}{
  99. "id": "custom",
  100. "title": "Custom",
  101. "title_zh_cn": "自定义",
  102. "title_ja_jp": "カスタム",
  103. }
  104. items := []map[string]interface{}{}
  105. custom["items"] = items
  106. if gulu.File.IsDir(customConfDir) {
  107. model.CustomEmojis = sync.Map{}
  108. customEmojis, err := os.ReadDir(customConfDir)
  109. if err != nil {
  110. logging.LogErrorf("read custom emojis failed: %s", err)
  111. } else {
  112. for _, customEmoji := range customEmojis {
  113. name := customEmoji.Name()
  114. if strings.HasPrefix(name, ".") {
  115. continue
  116. }
  117. if customEmoji.IsDir() {
  118. // 子级
  119. subCustomEmojis, err := os.ReadDir(filepath.Join(customConfDir, name))
  120. if err != nil {
  121. logging.LogErrorf("read custom emojis failed: %s", err)
  122. continue
  123. }
  124. for _, subCustomEmoji := range subCustomEmojis {
  125. if subCustomEmoji.IsDir() {
  126. continue
  127. }
  128. name = subCustomEmoji.Name()
  129. if strings.HasPrefix(name, ".") {
  130. continue
  131. }
  132. addCustomEmoji(customEmoji.Name()+"/"+name, &items)
  133. }
  134. continue
  135. }
  136. addCustomEmoji(name, &items)
  137. }
  138. }
  139. }
  140. custom["items"] = items
  141. conf = append([]map[string]interface{}{custom}, conf...)
  142. ret.Data = conf
  143. return
  144. }
  145. func addCustomEmoji(name string, items *[]map[string]interface{}) {
  146. ext := filepath.Ext(name)
  147. nameWithoutExt := strings.TrimSuffix(name, ext)
  148. emoji := map[string]interface{}{
  149. "unicode": name,
  150. "description": nameWithoutExt,
  151. "description_zh_cn": nameWithoutExt,
  152. "description_ja_jp": nameWithoutExt,
  153. "keywords": nameWithoutExt,
  154. }
  155. *items = append(*items, emoji)
  156. imgSrc := "/emojis/" + name
  157. model.CustomEmojis.Store(nameWithoutExt, imgSrc)
  158. }
  159. func checkUpdate(c *gin.Context) {
  160. ret := gulu.Ret.NewResult()
  161. defer c.JSON(http.StatusOK, ret)
  162. arg, ok := util.JsonArg(c, ret)
  163. if !ok {
  164. return
  165. }
  166. showMsg := arg["showMsg"].(bool)
  167. model.CheckUpdate(showMsg)
  168. }
  169. func exportLog(c *gin.Context) {
  170. ret := gulu.Ret.NewResult()
  171. defer c.JSON(http.StatusOK, ret)
  172. zipPath := model.ExportSystemLog()
  173. ret.Data = map[string]interface{}{
  174. "zip": zipPath,
  175. }
  176. }
  177. func exportConf(c *gin.Context) {
  178. ret := gulu.Ret.NewResult()
  179. defer c.JSON(http.StatusOK, ret)
  180. name := "siyuan-conf-" + time.Now().Format("20060102150405") + ".json"
  181. tmpDir := filepath.Join(util.TempDir, "export")
  182. if err := os.MkdirAll(tmpDir, 0755); err != nil {
  183. logging.LogErrorf("export WebDAV provider failed: %s", err)
  184. ret.Code = -1
  185. ret.Msg = err.Error()
  186. return
  187. }
  188. clonedConf := &model.AppConf{}
  189. if err := copier.Copy(clonedConf, model.Conf); err != nil {
  190. logging.LogErrorf("export conf failed: %s", err)
  191. ret.Code = -1
  192. ret.Msg = err.Error()
  193. return
  194. }
  195. if nil != clonedConf.Editor {
  196. clonedConf.Editor.Emoji = []string{}
  197. }
  198. if nil != clonedConf.Export {
  199. clonedConf.Export.PandocBin = ""
  200. }
  201. clonedConf.UserData = ""
  202. clonedConf.Account = nil
  203. clonedConf.AccessAuthCode = ""
  204. if nil != clonedConf.System {
  205. clonedConf.System.ID = ""
  206. clonedConf.System.Name = ""
  207. clonedConf.System.OSPlatform = ""
  208. clonedConf.System.Container = ""
  209. clonedConf.System.IsMicrosoftStore = false
  210. clonedConf.System.IsInsider = false
  211. }
  212. clonedConf.Sync = nil
  213. clonedConf.Stat = nil
  214. clonedConf.Api = nil
  215. clonedConf.Repo = nil
  216. clonedConf.Publish = nil
  217. clonedConf.CloudRegion = 0
  218. clonedConf.DataIndexState = 0
  219. data, err := gulu.JSON.MarshalIndentJSON(clonedConf, "", " ")
  220. if err != nil {
  221. logging.LogErrorf("export conf failed: %s", err)
  222. ret.Code = -1
  223. ret.Msg = err.Error()
  224. return
  225. }
  226. tmp := filepath.Join(tmpDir, name)
  227. if err = os.WriteFile(tmp, data, 0644); err != nil {
  228. logging.LogErrorf("export conf failed: %s", err)
  229. ret.Code = -1
  230. ret.Msg = err.Error()
  231. return
  232. }
  233. zipFile, err := gulu.Zip.Create(tmp + ".zip")
  234. if err != nil {
  235. logging.LogErrorf("export conf failed: %s", err)
  236. ret.Code = -1
  237. ret.Msg = err.Error()
  238. return
  239. }
  240. if err = zipFile.AddEntry(name, tmp); err != nil {
  241. logging.LogErrorf("export conf failed: %s", err)
  242. ret.Code = -1
  243. ret.Msg = err.Error()
  244. return
  245. }
  246. if err = zipFile.Close(); err != nil {
  247. logging.LogErrorf("export conf failed: %s", err)
  248. ret.Code = -1
  249. ret.Msg = err.Error()
  250. return
  251. }
  252. zipPath := "/export/" + name + ".zip"
  253. ret.Data = map[string]interface{}{
  254. "name": name,
  255. "zip": zipPath,
  256. }
  257. }
  258. func getConf(c *gin.Context) {
  259. ret := gulu.Ret.NewResult()
  260. defer c.JSON(http.StatusOK, ret)
  261. maskedConf, err := model.GetMaskedConf()
  262. if err != nil {
  263. ret.Code = -1
  264. ret.Msg = "get conf failed: " + err.Error()
  265. return
  266. }
  267. if !maskedConf.Sync.Enabled || (0 == maskedConf.Sync.Provider && !model.IsSubscriber()) {
  268. maskedConf.Sync.Stat = model.Conf.Language(53)
  269. }
  270. // REF: https://github.com/siyuan-note/siyuan/issues/11364
  271. role := model.GetGinContextRole(c)
  272. if model.IsReadOnlyRole(role) {
  273. maskedConf.ReadOnly = true
  274. }
  275. if !model.IsValidRole(role, []model.Role{
  276. model.RoleAdministrator,
  277. }) {
  278. model.HideConfSecret(maskedConf)
  279. }
  280. ret.Data = map[string]interface{}{
  281. "conf": maskedConf,
  282. "start": !util.IsUILoaded,
  283. }
  284. }
  285. func setUILayout(c *gin.Context) {
  286. ret := gulu.Ret.NewResult()
  287. defer c.JSON(http.StatusOK, ret)
  288. if util.ReadOnly {
  289. return
  290. }
  291. arg, ok := util.JsonArg(c, ret)
  292. if !ok {
  293. return
  294. }
  295. param, err := gulu.JSON.MarshalJSON(arg["layout"])
  296. if err != nil {
  297. ret.Code = -1
  298. ret.Msg = err.Error()
  299. return
  300. }
  301. uiLayout := &conf.UILayout{}
  302. if err = gulu.JSON.UnmarshalJSON(param, uiLayout); err != nil {
  303. ret.Code = -1
  304. ret.Msg = err.Error()
  305. return
  306. }
  307. model.Conf.SetUILayout(uiLayout)
  308. model.Conf.Save()
  309. }
  310. func setAPIToken(c *gin.Context) {
  311. ret := gulu.Ret.NewResult()
  312. defer c.JSON(http.StatusOK, ret)
  313. arg, ok := util.JsonArg(c, ret)
  314. if !ok {
  315. return
  316. }
  317. token := arg["token"].(string)
  318. model.Conf.Api.Token = token
  319. model.Conf.Save()
  320. }
  321. func setAccessAuthCode(c *gin.Context) {
  322. ret := gulu.Ret.NewResult()
  323. defer c.JSON(http.StatusOK, ret)
  324. arg, ok := util.JsonArg(c, ret)
  325. if !ok {
  326. return
  327. }
  328. aac := arg["accessAuthCode"].(string)
  329. if model.MaskedAccessAuthCode == aac {
  330. aac = model.Conf.AccessAuthCode
  331. }
  332. model.Conf.AccessAuthCode = aac
  333. model.Conf.Save()
  334. session := util.GetSession(c)
  335. workspaceSession := util.GetWorkspaceSession(session)
  336. workspaceSession.AccessAuthCode = aac
  337. session.Save(c)
  338. go func() {
  339. time.Sleep(200 * time.Millisecond)
  340. util.ReloadUI()
  341. }()
  342. return
  343. }
  344. func setFollowSystemLockScreen(c *gin.Context) {
  345. ret := gulu.Ret.NewResult()
  346. defer c.JSON(http.StatusOK, ret)
  347. arg, ok := util.JsonArg(c, ret)
  348. if !ok {
  349. return
  350. }
  351. lockScreenMode := int(arg["lockScreenMode"].(float64))
  352. model.Conf.System.LockScreenMode = lockScreenMode
  353. model.Conf.Save()
  354. return
  355. }
  356. func getSysFonts(c *gin.Context) {
  357. ret := gulu.Ret.NewResult()
  358. defer c.JSON(http.StatusOK, ret)
  359. ret.Data = util.GetSysFonts(model.Conf.Lang)
  360. }
  361. func version(c *gin.Context) {
  362. ret := gulu.Ret.NewResult()
  363. defer c.JSON(http.StatusOK, ret)
  364. ret.Data = util.Ver
  365. }
  366. func currentTime(c *gin.Context) {
  367. ret := gulu.Ret.NewResult()
  368. defer c.JSON(http.StatusOK, ret)
  369. ret.Data = util.CurrentTimeMillis()
  370. }
  371. func bootProgress(c *gin.Context) {
  372. ret := gulu.Ret.NewResult()
  373. defer c.JSON(http.StatusOK, ret)
  374. progress, details := util.GetBootProgressDetails()
  375. ret.Data = map[string]interface{}{"progress": progress, "details": details}
  376. }
  377. func setAppearanceMode(c *gin.Context) {
  378. ret := gulu.Ret.NewResult()
  379. defer c.JSON(http.StatusOK, ret)
  380. arg, ok := util.JsonArg(c, ret)
  381. if !ok {
  382. return
  383. }
  384. mode := int(arg["mode"].(float64))
  385. model.Conf.Appearance.Mode = mode
  386. if 0 == mode {
  387. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeLight, "theme.js"))
  388. } else {
  389. model.Conf.Appearance.ThemeJS = gulu.File.IsExist(filepath.Join(util.ThemesPath, model.Conf.Appearance.ThemeDark, "theme.js"))
  390. }
  391. model.Conf.Save()
  392. ret.Data = map[string]interface{}{
  393. "appearance": model.Conf.Appearance,
  394. }
  395. }
  396. func setNetworkServe(c *gin.Context) {
  397. ret := gulu.Ret.NewResult()
  398. defer c.JSON(http.StatusOK, ret)
  399. arg, ok := util.JsonArg(c, ret)
  400. if !ok {
  401. return
  402. }
  403. networkServe := arg["networkServe"].(bool)
  404. model.Conf.System.NetworkServe = networkServe
  405. model.Conf.Save()
  406. util.PushMsg(model.Conf.Language(42), 1000*15)
  407. time.Sleep(time.Second * 3)
  408. }
  409. func setGoogleAnalytics(c *gin.Context) {
  410. ret := gulu.Ret.NewResult()
  411. defer c.JSON(http.StatusOK, ret)
  412. arg, ok := util.JsonArg(c, ret)
  413. if !ok {
  414. return
  415. }
  416. googleAnalytics := arg["googleAnalytics"].(bool)
  417. model.Conf.System.DisableGoogleAnalytics = !googleAnalytics
  418. model.Conf.Save()
  419. }
  420. func setUploadErrLog(c *gin.Context) {
  421. ret := gulu.Ret.NewResult()
  422. defer c.JSON(http.StatusOK, ret)
  423. arg, ok := util.JsonArg(c, ret)
  424. if !ok {
  425. return
  426. }
  427. uploadErrLog := arg["uploadErrLog"].(bool)
  428. model.Conf.System.UploadErrLog = uploadErrLog
  429. model.Conf.Save()
  430. util.PushMsg(model.Conf.Language(42), 1000*15)
  431. time.Sleep(time.Second * 3)
  432. }
  433. func setAutoLaunch(c *gin.Context) {
  434. ret := gulu.Ret.NewResult()
  435. defer c.JSON(http.StatusOK, ret)
  436. arg, ok := util.JsonArg(c, ret)
  437. if !ok {
  438. return
  439. }
  440. autoLaunch := int(arg["autoLaunch"].(float64))
  441. model.Conf.System.AutoLaunch2 = autoLaunch
  442. model.Conf.Save()
  443. }
  444. func setDownloadInstallPkg(c *gin.Context) {
  445. ret := gulu.Ret.NewResult()
  446. defer c.JSON(http.StatusOK, ret)
  447. arg, ok := util.JsonArg(c, ret)
  448. if !ok {
  449. return
  450. }
  451. downloadInstallPkg := arg["downloadInstallPkg"].(bool)
  452. model.Conf.System.DownloadInstallPkg = downloadInstallPkg
  453. model.Conf.Save()
  454. }
  455. func setNetworkProxy(c *gin.Context) {
  456. ret := gulu.Ret.NewResult()
  457. defer c.JSON(http.StatusOK, ret)
  458. arg, ok := util.JsonArg(c, ret)
  459. if !ok {
  460. return
  461. }
  462. scheme := arg["scheme"].(string)
  463. host := arg["host"].(string)
  464. port := arg["port"].(string)
  465. model.Conf.System.NetworkProxy = &conf.NetworkProxy{
  466. Scheme: scheme,
  467. Host: host,
  468. Port: port,
  469. }
  470. model.Conf.Save()
  471. proxyURL := model.Conf.System.NetworkProxy.String()
  472. util.SetNetworkProxy(proxyURL)
  473. util.PushMsg(model.Conf.Language(102), 3000)
  474. }
  475. func addUIProcess(c *gin.Context) {
  476. pid := c.Query("pid")
  477. util.UIProcessIDs.Store(pid, true)
  478. }
  479. func exit(c *gin.Context) {
  480. ret := gulu.Ret.NewResult()
  481. defer c.JSON(http.StatusOK, ret)
  482. arg, ok := util.JsonArg(c, ret)
  483. if !ok {
  484. return
  485. }
  486. forceArg := arg["force"]
  487. var force bool
  488. if nil != forceArg {
  489. force = forceArg.(bool)
  490. }
  491. execInstallPkgArg := arg["execInstallPkg"] // 0:默认检查新版本,1:不执行新版本安装,2:执行新版本安装
  492. execInstallPkg := 0
  493. if nil != execInstallPkgArg {
  494. execInstallPkg = int(execInstallPkgArg.(float64))
  495. }
  496. exitCode := model.Close(force, true, execInstallPkg)
  497. ret.Code = exitCode
  498. switch exitCode {
  499. case 0:
  500. case 1: // 同步执行失败
  501. ret.Msg = model.Conf.Language(96) + "<div class=\"fn__space\"></div><button class=\"b3-button b3-button--white\">" + model.Conf.Language(97) + "</button>"
  502. ret.Data = map[string]interface{}{"closeTimeout": 0}
  503. case 2: // 提示新安装包
  504. ret.Msg = model.Conf.Language(61)
  505. ret.Data = map[string]interface{}{"closeTimeout": 0}
  506. }
  507. }