notebook.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. "net/http"
  19. "strings"
  20. "github.com/88250/gulu"
  21. "github.com/gin-gonic/gin"
  22. "github.com/siyuan-note/siyuan/kernel/model"
  23. "github.com/siyuan-note/siyuan/kernel/util"
  24. )
  25. func setNotebookIcon(c *gin.Context) {
  26. ret := gulu.Ret.NewResult()
  27. defer c.JSON(http.StatusOK, ret)
  28. arg, ok := util.JsonArg(c, ret)
  29. if !ok {
  30. return
  31. }
  32. boxID := arg["notebook"].(string)
  33. icon := arg["icon"].(string)
  34. model.SetBoxIcon(boxID, icon)
  35. }
  36. func changeSortNotebook(c *gin.Context) {
  37. ret := gulu.Ret.NewResult()
  38. defer c.JSON(http.StatusOK, ret)
  39. arg, ok := util.JsonArg(c, ret)
  40. if !ok {
  41. return
  42. }
  43. idsArg := arg["notebooks"].([]interface{})
  44. var ids []string
  45. for _, p := range idsArg {
  46. ids = append(ids, p.(string))
  47. }
  48. model.ChangeBoxSort(ids)
  49. }
  50. func renameNotebook(c *gin.Context) {
  51. ret := gulu.Ret.NewResult()
  52. defer c.JSON(http.StatusOK, ret)
  53. arg, ok := util.JsonArg(c, ret)
  54. if !ok {
  55. return
  56. }
  57. notebook := arg["notebook"].(string)
  58. if util.InvalidIDPattern(notebook, ret) {
  59. return
  60. }
  61. name := arg["name"].(string)
  62. err := model.RenameBox(notebook, name)
  63. if nil != err {
  64. ret.Code = -1
  65. ret.Msg = err.Error()
  66. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  67. return
  68. }
  69. evt := util.NewCmdResult("renamenotebook", 0, util.PushModeBroadcast)
  70. evt.Data = map[string]interface{}{
  71. "box": notebook,
  72. "name": name,
  73. }
  74. util.PushEvent(evt)
  75. }
  76. func removeNotebook(c *gin.Context) {
  77. ret := gulu.Ret.NewResult()
  78. defer c.JSON(http.StatusOK, ret)
  79. arg, ok := util.JsonArg(c, ret)
  80. if !ok {
  81. return
  82. }
  83. notebook := arg["notebook"].(string)
  84. if util.InvalidIDPattern(notebook, ret) {
  85. return
  86. }
  87. if util.ReadOnly && !model.IsUserGuide(notebook) {
  88. result := util.NewResult()
  89. result.Code = -1
  90. result.Msg = model.Conf.Language(34)
  91. result.Data = map[string]interface{}{"closeTimeout": 5000}
  92. c.JSON(200, result)
  93. return
  94. }
  95. err := model.RemoveBox(notebook)
  96. if nil != err {
  97. ret.Code = -1
  98. ret.Msg = err.Error()
  99. return
  100. }
  101. evt := util.NewCmdResult("unmount", 0, util.PushModeBroadcast)
  102. evt.Data = map[string]interface{}{
  103. "box": notebook,
  104. }
  105. evt.Callback = arg["callback"]
  106. util.PushEvent(evt)
  107. }
  108. func createNotebook(c *gin.Context) {
  109. ret := gulu.Ret.NewResult()
  110. defer c.JSON(http.StatusOK, ret)
  111. arg, ok := util.JsonArg(c, ret)
  112. if !ok {
  113. return
  114. }
  115. name := arg["name"].(string)
  116. id, err := model.CreateBox(name)
  117. if nil != err {
  118. ret.Code = -1
  119. ret.Msg = err.Error()
  120. return
  121. }
  122. existed, err := model.Mount(id)
  123. if nil != err {
  124. ret.Code = -1
  125. ret.Msg = err.Error()
  126. return
  127. }
  128. ret.Data = map[string]interface{}{
  129. "notebook": model.Conf.Box(id),
  130. }
  131. evt := util.NewCmdResult("createnotebook", 0, util.PushModeBroadcast)
  132. evt.Data = map[string]interface{}{
  133. "box": model.Conf.Box(id),
  134. "existed": existed,
  135. }
  136. util.PushEvent(evt)
  137. }
  138. func openNotebook(c *gin.Context) {
  139. ret := gulu.Ret.NewResult()
  140. defer c.JSON(http.StatusOK, ret)
  141. arg, ok := util.JsonArg(c, ret)
  142. if !ok {
  143. return
  144. }
  145. notebook := arg["notebook"].(string)
  146. if util.InvalidIDPattern(notebook, ret) {
  147. return
  148. }
  149. if util.ReadOnly && !model.IsUserGuide(notebook) {
  150. result := util.NewResult()
  151. result.Code = -1
  152. result.Msg = model.Conf.Language(34)
  153. result.Data = map[string]interface{}{"closeTimeout": 5000}
  154. c.JSON(200, result)
  155. return
  156. }
  157. msgId := util.PushMsg(model.Conf.Language(45), 1000*60*15)
  158. defer util.PushClearMsg(msgId)
  159. existed, err := model.Mount(notebook)
  160. if nil != err {
  161. ret.Code = -1
  162. ret.Msg = err.Error()
  163. return
  164. }
  165. evt := util.NewCmdResult("mount", 0, util.PushModeBroadcast)
  166. evt.Data = map[string]interface{}{
  167. "box": model.Conf.Box(notebook),
  168. "existed": existed,
  169. }
  170. evt.Callback = arg["callback"]
  171. util.PushEvent(evt)
  172. }
  173. func closeNotebook(c *gin.Context) {
  174. ret := gulu.Ret.NewResult()
  175. defer c.JSON(http.StatusOK, ret)
  176. arg, ok := util.JsonArg(c, ret)
  177. if !ok {
  178. return
  179. }
  180. notebook := arg["notebook"].(string)
  181. if util.InvalidIDPattern(notebook, ret) {
  182. return
  183. }
  184. model.Unmount(notebook)
  185. }
  186. func getNotebookConf(c *gin.Context) {
  187. ret := gulu.Ret.NewResult()
  188. defer c.JSON(http.StatusOK, ret)
  189. arg, ok := util.JsonArg(c, ret)
  190. if !ok {
  191. return
  192. }
  193. notebook := arg["notebook"].(string)
  194. if util.InvalidIDPattern(notebook, ret) {
  195. return
  196. }
  197. box := model.Conf.Box(notebook)
  198. ret.Data = map[string]interface{}{
  199. "box": box.ID,
  200. "name": box.Name,
  201. "conf": box.GetConf(),
  202. }
  203. }
  204. func setNotebookConf(c *gin.Context) {
  205. ret := gulu.Ret.NewResult()
  206. defer c.JSON(http.StatusOK, ret)
  207. arg, ok := util.JsonArg(c, ret)
  208. if !ok {
  209. return
  210. }
  211. notebook := arg["notebook"].(string)
  212. if util.InvalidIDPattern(notebook, ret) {
  213. return
  214. }
  215. box := model.Conf.Box(notebook)
  216. param, err := gulu.JSON.MarshalJSON(arg["conf"])
  217. if nil != err {
  218. ret.Code = -1
  219. ret.Msg = err.Error()
  220. return
  221. }
  222. boxConf := box.GetConf()
  223. if err = gulu.JSON.UnmarshalJSON(param, boxConf); nil != err {
  224. ret.Code = -1
  225. ret.Msg = err.Error()
  226. return
  227. }
  228. boxConf.RefCreateSavePath = strings.TrimSpace(boxConf.RefCreateSavePath)
  229. if "" != boxConf.RefCreateSavePath {
  230. if !strings.HasSuffix(boxConf.RefCreateSavePath, "/") {
  231. boxConf.RefCreateSavePath += "/"
  232. }
  233. }
  234. boxConf.DailyNoteSavePath = strings.TrimSpace(boxConf.DailyNoteSavePath)
  235. if "" != boxConf.DailyNoteSavePath {
  236. if !strings.HasPrefix(boxConf.DailyNoteSavePath, "/") {
  237. boxConf.DailyNoteSavePath = "/" + boxConf.DailyNoteSavePath
  238. }
  239. }
  240. if "/" == boxConf.DailyNoteSavePath {
  241. ret.Code = -1
  242. ret.Msg = model.Conf.Language(49)
  243. return
  244. }
  245. boxConf.DailyNoteTemplatePath = strings.TrimSpace(boxConf.DailyNoteTemplatePath)
  246. if "" != boxConf.DailyNoteTemplatePath {
  247. if !strings.HasSuffix(boxConf.DailyNoteTemplatePath, ".md") {
  248. boxConf.DailyNoteTemplatePath += ".md"
  249. }
  250. if !strings.HasPrefix(boxConf.DailyNoteTemplatePath, "/") {
  251. boxConf.DailyNoteTemplatePath = "/" + boxConf.DailyNoteTemplatePath
  252. }
  253. }
  254. box.SaveConf(boxConf)
  255. ret.Data = boxConf
  256. }
  257. func lsNotebooks(c *gin.Context) {
  258. ret := gulu.Ret.NewResult()
  259. defer c.JSON(http.StatusOK, ret)
  260. flashcard := false
  261. // 兼容旧版接口,不能直接使用 util.JsonArg()
  262. arg := map[string]interface{}{}
  263. if err := c.ShouldBindJSON(&arg); nil == err {
  264. if arg["flashcard"] != nil {
  265. flashcard = arg["flashcard"].(bool)
  266. }
  267. }
  268. var notebooks []*model.Box
  269. if flashcard {
  270. notebooks = model.GetFlashcardNotebooks()
  271. } else {
  272. var err error
  273. notebooks, err = model.ListNotebooks()
  274. if nil != err {
  275. return
  276. }
  277. }
  278. ret.Data = map[string]interface{}{
  279. "notebooks": notebooks,
  280. }
  281. }