setting.go 7.7 KB

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