setting.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. model.Conf.Editor = editor
  78. model.Conf.Save()
  79. if oldGenerateHistoryInterval != model.Conf.Editor.GenerateHistoryInterval {
  80. model.ChangeHistoryTick(editor.GenerateHistoryInterval)
  81. }
  82. ret.Data = model.Conf.Editor
  83. }
  84. func setExport(c *gin.Context) {
  85. ret := gulu.Ret.NewResult()
  86. defer c.JSON(http.StatusOK, ret)
  87. arg, ok := util.JsonArg(c, ret)
  88. if !ok {
  89. return
  90. }
  91. param, err := gulu.JSON.MarshalJSON(arg)
  92. if nil != err {
  93. ret.Code = -1
  94. ret.Msg = err.Error()
  95. return
  96. }
  97. export := &conf.Export{}
  98. if err = gulu.JSON.UnmarshalJSON(param, export); nil != err {
  99. ret.Code = -1
  100. ret.Msg = err.Error()
  101. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  102. return
  103. }
  104. if "" != export.PandocBin {
  105. if !util.IsValidPandocBin(export.PandocBin) {
  106. ret.Code = -1
  107. ret.Msg = fmt.Sprintf(model.Conf.Language(117), export.PandocBin)
  108. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  109. return
  110. }
  111. }
  112. model.Conf.Export = export
  113. model.Conf.Save()
  114. ret.Data = model.Conf.Export
  115. }
  116. func setFiletree(c *gin.Context) {
  117. ret := gulu.Ret.NewResult()
  118. defer c.JSON(http.StatusOK, ret)
  119. arg, ok := util.JsonArg(c, ret)
  120. if !ok {
  121. return
  122. }
  123. param, err := gulu.JSON.MarshalJSON(arg)
  124. if nil != err {
  125. ret.Code = -1
  126. ret.Msg = err.Error()
  127. return
  128. }
  129. fileTree := conf.NewFileTree()
  130. if err = gulu.JSON.UnmarshalJSON(param, fileTree); nil != err {
  131. ret.Code = -1
  132. ret.Msg = err.Error()
  133. return
  134. }
  135. fileTree.RefCreateSavePath = strings.TrimSpace(fileTree.RefCreateSavePath)
  136. if "" != fileTree.RefCreateSavePath {
  137. if !strings.HasSuffix(fileTree.RefCreateSavePath, "/") {
  138. fileTree.RefCreateSavePath += "/"
  139. }
  140. }
  141. if 1 > fileTree.MaxOpenTabCount {
  142. fileTree.MaxOpenTabCount = 8
  143. }
  144. if 32 < fileTree.MaxOpenTabCount {
  145. fileTree.MaxOpenTabCount = 32
  146. }
  147. model.Conf.FileTree = fileTree
  148. model.Conf.Save()
  149. ret.Data = model.Conf.FileTree
  150. }
  151. func setSearch(c *gin.Context) {
  152. ret := gulu.Ret.NewResult()
  153. defer c.JSON(http.StatusOK, ret)
  154. arg, ok := util.JsonArg(c, ret)
  155. if !ok {
  156. return
  157. }
  158. param, err := gulu.JSON.MarshalJSON(arg)
  159. if nil != err {
  160. ret.Code = -1
  161. ret.Msg = err.Error()
  162. return
  163. }
  164. s := &conf.Search{}
  165. if err = gulu.JSON.UnmarshalJSON(param, s); nil != err {
  166. ret.Code = -1
  167. ret.Msg = err.Error()
  168. return
  169. }
  170. if 1 > s.Limit {
  171. s.Limit = 32
  172. }
  173. model.Conf.Search = s
  174. model.Conf.Save()
  175. sql.SetCaseSensitive(s.CaseSensitive)
  176. sql.ClearVirtualRefKeywords()
  177. ret.Data = s
  178. }
  179. func setKeymap(c *gin.Context) {
  180. ret := gulu.Ret.NewResult()
  181. defer c.JSON(http.StatusOK, ret)
  182. arg, ok := util.JsonArg(c, ret)
  183. if !ok {
  184. return
  185. }
  186. param, err := gulu.JSON.MarshalJSON(arg["data"])
  187. if nil != err {
  188. ret.Code = -1
  189. ret.Msg = err.Error()
  190. return
  191. }
  192. keymap := &conf.Keymap{}
  193. if err = gulu.JSON.UnmarshalJSON(param, keymap); nil != err {
  194. ret.Code = -1
  195. ret.Msg = err.Error()
  196. return
  197. }
  198. model.Conf.Keymap = keymap
  199. model.Conf.Save()
  200. }
  201. func setAppearance(c *gin.Context) {
  202. ret := gulu.Ret.NewResult()
  203. defer c.JSON(http.StatusOK, ret)
  204. arg, ok := util.JsonArg(c, ret)
  205. if !ok {
  206. return
  207. }
  208. param, err := gulu.JSON.MarshalJSON(arg)
  209. if nil != err {
  210. ret.Code = -1
  211. ret.Msg = err.Error()
  212. return
  213. }
  214. appearance := &conf.Appearance{}
  215. if err = gulu.JSON.UnmarshalJSON(param, appearance); nil != err {
  216. ret.Code = -1
  217. ret.Msg = err.Error()
  218. return
  219. }
  220. model.Conf.Appearance = appearance
  221. model.Conf.Lang = appearance.Lang
  222. model.Conf.Save()
  223. model.InitAppearance()
  224. ret.Data = model.Conf.Appearance
  225. }
  226. func getCloudUser(c *gin.Context) {
  227. ret := gulu.Ret.NewResult()
  228. defer c.JSON(http.StatusOK, ret)
  229. arg, ok := util.JsonArg(c, ret)
  230. if !ok {
  231. return
  232. }
  233. t := arg["token"]
  234. var token string
  235. if nil != t {
  236. token = t.(string)
  237. }
  238. if err := model.RefreshUser(token); nil != err {
  239. ret.Code = 1
  240. ret.Msg = err.Error()
  241. return
  242. }
  243. ret.Data = model.Conf.User
  244. }
  245. func logoutCloudUser(c *gin.Context) {
  246. ret := gulu.Ret.NewResult()
  247. defer c.JSON(http.StatusOK, ret)
  248. model.LogoutUser()
  249. }
  250. func login2faCloudUser(c *gin.Context) {
  251. ret := gulu.Ret.NewResult()
  252. defer c.JSON(http.StatusOK, ret)
  253. arg, ok := util.JsonArg(c, ret)
  254. if !ok {
  255. return
  256. }
  257. token := arg["token"].(string)
  258. code := arg["code"].(string)
  259. data, err := model.Login2fa(token, code)
  260. if nil != err {
  261. ret.Code = -1
  262. ret.Msg = err.Error()
  263. return
  264. }
  265. ret.Data = data
  266. }
  267. func getCustomCSS(c *gin.Context) {
  268. ret := gulu.Ret.NewResult()
  269. defer c.JSON(http.StatusOK, ret)
  270. arg, ok := util.JsonArg(c, ret)
  271. if !ok {
  272. return
  273. }
  274. themeName := arg["theme"].(string)
  275. customCSS, err := model.ReadCustomCSS(themeName)
  276. if nil != err {
  277. ret.Code = -1
  278. ret.Msg = err.Error()
  279. return
  280. }
  281. ret.Data = customCSS
  282. }
  283. func setCustomCSS(c *gin.Context) {
  284. ret := gulu.Ret.NewResult()
  285. defer c.JSON(http.StatusOK, ret)
  286. arg, ok := util.JsonArg(c, ret)
  287. if !ok {
  288. return
  289. }
  290. themeName := arg["theme"].(string)
  291. css := arg["css"].(map[string]interface{})
  292. if err := model.WriteCustomCSS(themeName, css); nil != err {
  293. ret.Code = -1
  294. ret.Msg = err.Error()
  295. return
  296. }
  297. }
  298. func setEmoji(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. argEmoji := arg["emoji"].([]interface{})
  306. var emoji []string
  307. for _, ae := range argEmoji {
  308. emoji = append(emoji, ae.(string))
  309. }
  310. model.Conf.Editor.Emoji = emoji
  311. }
  312. func setSearchCaseSensitive(c *gin.Context) {
  313. ret := gulu.Ret.NewResult()
  314. defer c.JSON(http.StatusOK, ret)
  315. arg, ok := util.JsonArg(c, ret)
  316. if !ok {
  317. return
  318. }
  319. caseSensitive := arg["caseSensitive"].(bool)
  320. model.Conf.Search.CaseSensitive = caseSensitive
  321. model.Conf.Save()
  322. sql.SetCaseSensitive(caseSensitive)
  323. }