setting.go 7.4 KB

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