setting.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 setCriterion(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["criterion"])
  189. if nil != err {
  190. ret.Code = -1
  191. ret.Msg = err.Error()
  192. return
  193. }
  194. criterion := &conf.Criterion{}
  195. if err = gulu.JSON.UnmarshalJSON(param, criterion); nil != err {
  196. ret.Code = -1
  197. ret.Msg = err.Error()
  198. return
  199. }
  200. update := false
  201. for i, criteria := range model.Conf.Criteria {
  202. if criteria.Name == criterion.Name {
  203. model.Conf.Criteria[i] = criterion
  204. update = true
  205. break
  206. }
  207. }
  208. if !update {
  209. model.Conf.Criteria = append(model.Conf.Criteria, criterion)
  210. }
  211. model.Conf.Save()
  212. }
  213. func setKeymap(c *gin.Context) {
  214. ret := gulu.Ret.NewResult()
  215. defer c.JSON(http.StatusOK, ret)
  216. arg, ok := util.JsonArg(c, ret)
  217. if !ok {
  218. return
  219. }
  220. param, err := gulu.JSON.MarshalJSON(arg["data"])
  221. if nil != err {
  222. ret.Code = -1
  223. ret.Msg = err.Error()
  224. return
  225. }
  226. keymap := &conf.Keymap{}
  227. if err = gulu.JSON.UnmarshalJSON(param, keymap); nil != err {
  228. ret.Code = -1
  229. ret.Msg = err.Error()
  230. return
  231. }
  232. model.Conf.Keymap = keymap
  233. model.Conf.Save()
  234. }
  235. func setAppearance(c *gin.Context) {
  236. ret := gulu.Ret.NewResult()
  237. defer c.JSON(http.StatusOK, ret)
  238. arg, ok := util.JsonArg(c, ret)
  239. if !ok {
  240. return
  241. }
  242. param, err := gulu.JSON.MarshalJSON(arg)
  243. if nil != err {
  244. ret.Code = -1
  245. ret.Msg = err.Error()
  246. return
  247. }
  248. appearance := &conf.Appearance{}
  249. if err = gulu.JSON.UnmarshalJSON(param, appearance); nil != err {
  250. ret.Code = -1
  251. ret.Msg = err.Error()
  252. return
  253. }
  254. model.Conf.Appearance = appearance
  255. model.Conf.Lang = appearance.Lang
  256. model.Conf.Save()
  257. model.InitAppearance()
  258. ret.Data = model.Conf.Appearance
  259. }
  260. func getCloudUser(c *gin.Context) {
  261. ret := gulu.Ret.NewResult()
  262. defer c.JSON(http.StatusOK, ret)
  263. arg, ok := util.JsonArg(c, ret)
  264. if !ok {
  265. return
  266. }
  267. t := arg["token"]
  268. var token string
  269. if nil != t {
  270. token = t.(string)
  271. }
  272. if err := model.RefreshUser(token); nil != err {
  273. ret.Code = 1
  274. ret.Msg = err.Error()
  275. return
  276. }
  277. ret.Data = model.Conf.User
  278. }
  279. func logoutCloudUser(c *gin.Context) {
  280. ret := gulu.Ret.NewResult()
  281. defer c.JSON(http.StatusOK, ret)
  282. model.LogoutUser()
  283. }
  284. func login2faCloudUser(c *gin.Context) {
  285. ret := gulu.Ret.NewResult()
  286. defer c.JSON(http.StatusOK, ret)
  287. arg, ok := util.JsonArg(c, ret)
  288. if !ok {
  289. return
  290. }
  291. token := arg["token"].(string)
  292. code := arg["code"].(string)
  293. data, err := model.Login2fa(token, code)
  294. if nil != err {
  295. ret.Code = -1
  296. ret.Msg = err.Error()
  297. return
  298. }
  299. ret.Data = data
  300. }
  301. func getCustomCSS(c *gin.Context) {
  302. ret := gulu.Ret.NewResult()
  303. defer c.JSON(http.StatusOK, ret)
  304. arg, ok := util.JsonArg(c, ret)
  305. if !ok {
  306. return
  307. }
  308. themeName := arg["theme"].(string)
  309. customCSS, err := model.ReadCustomCSS(themeName)
  310. if nil != err {
  311. ret.Code = -1
  312. ret.Msg = err.Error()
  313. return
  314. }
  315. ret.Data = customCSS
  316. }
  317. func setCustomCSS(c *gin.Context) {
  318. ret := gulu.Ret.NewResult()
  319. defer c.JSON(http.StatusOK, ret)
  320. arg, ok := util.JsonArg(c, ret)
  321. if !ok {
  322. return
  323. }
  324. themeName := arg["theme"].(string)
  325. css := arg["css"].(map[string]interface{})
  326. if err := model.WriteCustomCSS(themeName, css); nil != err {
  327. ret.Code = -1
  328. ret.Msg = err.Error()
  329. return
  330. }
  331. }
  332. func setEmoji(c *gin.Context) {
  333. ret := gulu.Ret.NewResult()
  334. defer c.JSON(http.StatusOK, ret)
  335. arg, ok := util.JsonArg(c, ret)
  336. if !ok {
  337. return
  338. }
  339. argEmoji := arg["emoji"].([]interface{})
  340. var emoji []string
  341. for _, ae := range argEmoji {
  342. emoji = append(emoji, ae.(string))
  343. }
  344. model.Conf.Editor.Emoji = emoji
  345. }