setting.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. "fmt"
  19. "net/http"
  20. "strings"
  21. "github.com/88250/gulu"
  22. "github.com/gin-gonic/gin"
  23. "github.com/siyuan-note/siyuan/kernel/conf"
  24. "github.com/siyuan-note/siyuan/kernel/model"
  25. "github.com/siyuan-note/siyuan/kernel/sql"
  26. "github.com/siyuan-note/siyuan/kernel/util"
  27. )
  28. func setAccount(c *gin.Context) {
  29. ret := gulu.Ret.NewResult()
  30. defer c.JSON(http.StatusOK, ret)
  31. arg, ok := util.JsonArg(c, ret)
  32. if !ok {
  33. return
  34. }
  35. param, err := gulu.JSON.MarshalJSON(arg)
  36. if nil != err {
  37. ret.Code = -1
  38. ret.Msg = err.Error()
  39. return
  40. }
  41. account := &conf.Account{}
  42. if err = gulu.JSON.UnmarshalJSON(param, account); nil != err {
  43. ret.Code = -1
  44. ret.Msg = err.Error()
  45. return
  46. }
  47. model.Conf.Account = account
  48. model.Conf.Save()
  49. ret.Data = model.Conf.Account
  50. }
  51. func setEditor(c *gin.Context) {
  52. ret := gulu.Ret.NewResult()
  53. defer c.JSON(http.StatusOK, ret)
  54. arg, ok := util.JsonArg(c, ret)
  55. if !ok {
  56. return
  57. }
  58. param, err := gulu.JSON.MarshalJSON(arg)
  59. if nil != err {
  60. ret.Code = -1
  61. ret.Msg = err.Error()
  62. return
  63. }
  64. oldGenerateHistoryInterval := model.Conf.Editor.GenerateHistoryInterval
  65. editor := conf.NewEditor()
  66. if err = gulu.JSON.UnmarshalJSON(param, editor); nil != err {
  67. ret.Code = -1
  68. ret.Msg = err.Error()
  69. return
  70. }
  71. if "" == editor.PlantUMLServePath {
  72. editor.PlantUMLServePath = "https://www.plantuml.com/plantuml/svg/~1"
  73. }
  74. if "" == editor.KaTexMacros {
  75. editor.KaTexMacros = "{}"
  76. }
  77. oldVirtualBlockRef := model.Conf.Editor.VirtualBlockRef
  78. oldVirtualBlockRefInclude := model.Conf.Editor.VirtualBlockRefInclude
  79. oldVirtualBlockRefExclude := model.Conf.Editor.VirtualBlockRefExclude
  80. oldReadOnly := editor.ReadOnly
  81. model.Conf.Editor = editor
  82. model.Conf.Save()
  83. if oldGenerateHistoryInterval != model.Conf.Editor.GenerateHistoryInterval {
  84. model.ChangeHistoryTick(editor.GenerateHistoryInterval)
  85. }
  86. if oldVirtualBlockRef != model.Conf.Editor.VirtualBlockRef ||
  87. oldVirtualBlockRefInclude != model.Conf.Editor.VirtualBlockRefInclude ||
  88. oldVirtualBlockRefExclude != model.Conf.Editor.VirtualBlockRefExclude {
  89. model.ResetVirtualBlockRefCache()
  90. }
  91. if oldReadOnly != model.Conf.Editor.ReadOnly {
  92. util.BroadcastByType("protyle", "readonly", 0, "", model.Conf.Editor.ReadOnly)
  93. }
  94. ret.Data = model.Conf.Editor
  95. }
  96. func setExport(c *gin.Context) {
  97. ret := gulu.Ret.NewResult()
  98. defer c.JSON(http.StatusOK, ret)
  99. arg, ok := util.JsonArg(c, ret)
  100. if !ok {
  101. return
  102. }
  103. param, err := gulu.JSON.MarshalJSON(arg)
  104. if nil != err {
  105. ret.Code = -1
  106. ret.Msg = err.Error()
  107. return
  108. }
  109. export := &conf.Export{}
  110. if err = gulu.JSON.UnmarshalJSON(param, export); nil != err {
  111. ret.Code = -1
  112. ret.Msg = err.Error()
  113. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  114. return
  115. }
  116. if "" != export.PandocBin {
  117. if !util.IsValidPandocBin(export.PandocBin) {
  118. util.PushErrMsg(fmt.Sprintf(model.Conf.Language(117), export.PandocBin), 5000)
  119. export.PandocBin = util.PandocBinPath
  120. }
  121. }
  122. model.Conf.Export = export
  123. model.Conf.Save()
  124. ret.Data = model.Conf.Export
  125. }
  126. func setFiletree(c *gin.Context) {
  127. ret := gulu.Ret.NewResult()
  128. defer c.JSON(http.StatusOK, ret)
  129. arg, ok := util.JsonArg(c, ret)
  130. if !ok {
  131. return
  132. }
  133. param, err := gulu.JSON.MarshalJSON(arg)
  134. if nil != err {
  135. ret.Code = -1
  136. ret.Msg = err.Error()
  137. return
  138. }
  139. fileTree := conf.NewFileTree()
  140. if err = gulu.JSON.UnmarshalJSON(param, fileTree); nil != err {
  141. ret.Code = -1
  142. ret.Msg = err.Error()
  143. return
  144. }
  145. fileTree.RefCreateSavePath = strings.TrimSpace(fileTree.RefCreateSavePath)
  146. if "" != fileTree.RefCreateSavePath {
  147. if !strings.HasSuffix(fileTree.RefCreateSavePath, "/") {
  148. fileTree.RefCreateSavePath += "/"
  149. }
  150. }
  151. fileTree.DocCreateSavePath = strings.TrimSpace(fileTree.DocCreateSavePath)
  152. for strings.HasSuffix(fileTree.DocCreateSavePath, "/") {
  153. fileTree.DocCreateSavePath = strings.TrimSuffix(fileTree.DocCreateSavePath, "/")
  154. fileTree.DocCreateSavePath = strings.TrimSpace(fileTree.DocCreateSavePath)
  155. }
  156. if 1 > fileTree.MaxOpenTabCount {
  157. fileTree.MaxOpenTabCount = 8
  158. }
  159. if 32 < fileTree.MaxOpenTabCount {
  160. fileTree.MaxOpenTabCount = 32
  161. }
  162. model.Conf.FileTree = fileTree
  163. model.Conf.Save()
  164. ret.Data = model.Conf.FileTree
  165. }
  166. func setSearch(c *gin.Context) {
  167. ret := gulu.Ret.NewResult()
  168. defer c.JSON(http.StatusOK, ret)
  169. arg, ok := util.JsonArg(c, ret)
  170. if !ok {
  171. return
  172. }
  173. param, err := gulu.JSON.MarshalJSON(arg)
  174. if nil != err {
  175. ret.Code = -1
  176. ret.Msg = err.Error()
  177. return
  178. }
  179. s := &conf.Search{}
  180. if err = gulu.JSON.UnmarshalJSON(param, s); nil != err {
  181. ret.Code = -1
  182. ret.Msg = err.Error()
  183. return
  184. }
  185. if 32 > s.Limit {
  186. s.Limit = 32
  187. }
  188. oldCaseSensitive := model.Conf.Search.CaseSensitive
  189. oldVirtualRefName := model.Conf.Search.VirtualRefName
  190. oldVirtualRefAlias := model.Conf.Search.VirtualRefAlias
  191. oldVirtualRefAnchor := model.Conf.Search.VirtualRefAnchor
  192. oldVirtualRefDoc := model.Conf.Search.VirtualRefDoc
  193. oldVirtualRefKeywordsLimit := model.Conf.Search.VirtualRefKeywordsLimit
  194. model.Conf.Search = s
  195. model.Conf.Save()
  196. sql.SetCaseSensitive(s.CaseSensitive)
  197. if s.CaseSensitive != oldCaseSensitive {
  198. model.FullReindex()
  199. }
  200. if oldVirtualRefName != s.VirtualRefName ||
  201. oldVirtualRefAlias != s.VirtualRefAlias ||
  202. oldVirtualRefAnchor != s.VirtualRefAnchor ||
  203. oldVirtualRefDoc != s.VirtualRefDoc ||
  204. oldVirtualRefKeywordsLimit != s.VirtualRefKeywordsLimit {
  205. model.ResetVirtualBlockRefCache()
  206. }
  207. ret.Data = s
  208. }
  209. func setKeymap(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. param, err := gulu.JSON.MarshalJSON(arg["data"])
  217. if nil != err {
  218. ret.Code = -1
  219. ret.Msg = err.Error()
  220. return
  221. }
  222. keymap := &conf.Keymap{}
  223. if err = gulu.JSON.UnmarshalJSON(param, keymap); nil != err {
  224. ret.Code = -1
  225. ret.Msg = err.Error()
  226. return
  227. }
  228. model.Conf.Keymap = keymap
  229. model.Conf.Save()
  230. }
  231. func setAppearance(c *gin.Context) {
  232. ret := gulu.Ret.NewResult()
  233. defer c.JSON(http.StatusOK, ret)
  234. arg, ok := util.JsonArg(c, ret)
  235. if !ok {
  236. return
  237. }
  238. param, err := gulu.JSON.MarshalJSON(arg)
  239. if nil != err {
  240. ret.Code = -1
  241. ret.Msg = err.Error()
  242. return
  243. }
  244. appearance := &conf.Appearance{}
  245. if err = gulu.JSON.UnmarshalJSON(param, appearance); nil != err {
  246. ret.Code = -1
  247. ret.Msg = err.Error()
  248. return
  249. }
  250. model.Conf.Appearance = appearance
  251. model.Conf.Lang = appearance.Lang
  252. util.Lang = model.Conf.Lang
  253. model.Conf.Save()
  254. model.InitAppearance()
  255. ret.Data = model.Conf.Appearance
  256. }
  257. func getCloudUser(c *gin.Context) {
  258. ret := gulu.Ret.NewResult()
  259. defer c.JSON(http.StatusOK, ret)
  260. arg, ok := util.JsonArg(c, ret)
  261. if !ok {
  262. return
  263. }
  264. t := arg["token"]
  265. var token string
  266. if nil != t {
  267. token = t.(string)
  268. }
  269. if err := model.RefreshUser(token); nil != err {
  270. ret.Code = 1
  271. ret.Msg = err.Error()
  272. return
  273. }
  274. ret.Data = model.Conf.User
  275. }
  276. func logoutCloudUser(c *gin.Context) {
  277. ret := gulu.Ret.NewResult()
  278. defer c.JSON(http.StatusOK, ret)
  279. model.LogoutUser()
  280. }
  281. func login2faCloudUser(c *gin.Context) {
  282. ret := gulu.Ret.NewResult()
  283. defer c.JSON(http.StatusOK, ret)
  284. arg, ok := util.JsonArg(c, ret)
  285. if !ok {
  286. return
  287. }
  288. token := arg["token"].(string)
  289. code := arg["code"].(string)
  290. data, err := model.Login2fa(token, code)
  291. if nil != err {
  292. ret.Code = -1
  293. ret.Msg = err.Error()
  294. return
  295. }
  296. ret.Data = data
  297. }
  298. func getCustomCSS(c *gin.Context) {
  299. ret := gulu.Ret.NewResult()
  300. defer c.JSON(http.StatusOK, ret)
  301. arg, ok := util.JsonArg(c, ret)
  302. if !ok {
  303. return
  304. }
  305. themeName := arg["theme"].(string)
  306. customCSS, err := model.ReadCustomCSS(themeName)
  307. if nil != err {
  308. ret.Code = -1
  309. ret.Msg = err.Error()
  310. return
  311. }
  312. ret.Data = customCSS
  313. }
  314. func setCustomCSS(c *gin.Context) {
  315. ret := gulu.Ret.NewResult()
  316. defer c.JSON(http.StatusOK, ret)
  317. arg, ok := util.JsonArg(c, ret)
  318. if !ok {
  319. return
  320. }
  321. themeName := arg["theme"].(string)
  322. css := arg["css"].(map[string]interface{})
  323. if err := model.WriteCustomCSS(themeName, css); nil != err {
  324. ret.Code = -1
  325. ret.Msg = err.Error()
  326. return
  327. }
  328. }
  329. func setEmoji(c *gin.Context) {
  330. ret := gulu.Ret.NewResult()
  331. defer c.JSON(http.StatusOK, ret)
  332. arg, ok := util.JsonArg(c, ret)
  333. if !ok {
  334. return
  335. }
  336. argEmoji := arg["emoji"].([]interface{})
  337. var emoji []string
  338. for _, ae := range argEmoji {
  339. emoji = append(emoji, ae.(string))
  340. }
  341. model.Conf.Editor.Emoji = emoji
  342. }