notebook.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // SiYuan - Refactor your thinking
  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. box := model.Conf.Box(id)
  129. if nil == box {
  130. ret.Code = -1
  131. ret.Msg = "opened notebook [" + id + "] not found"
  132. return
  133. }
  134. ret.Data = map[string]interface{}{
  135. "notebook": box,
  136. }
  137. evt := util.NewCmdResult("createnotebook", 0, util.PushModeBroadcast)
  138. evt.Data = map[string]interface{}{
  139. "box": box,
  140. "existed": existed,
  141. }
  142. util.PushEvent(evt)
  143. }
  144. func openNotebook(c *gin.Context) {
  145. ret := gulu.Ret.NewResult()
  146. defer c.JSON(http.StatusOK, ret)
  147. arg, ok := util.JsonArg(c, ret)
  148. if !ok {
  149. return
  150. }
  151. notebook := arg["notebook"].(string)
  152. if util.InvalidIDPattern(notebook, ret) {
  153. return
  154. }
  155. if util.ReadOnly && !model.IsUserGuide(notebook) {
  156. result := util.NewResult()
  157. result.Code = -1
  158. result.Msg = model.Conf.Language(34)
  159. result.Data = map[string]interface{}{"closeTimeout": 5000}
  160. c.JSON(200, result)
  161. return
  162. }
  163. msgId := util.PushMsg(model.Conf.Language(45), 1000*60*15)
  164. defer util.PushClearMsg(msgId)
  165. existed, err := model.Mount(notebook)
  166. if nil != err {
  167. ret.Code = -1
  168. ret.Msg = err.Error()
  169. return
  170. }
  171. box := model.Conf.Box(notebook)
  172. if nil == box {
  173. ret.Code = -1
  174. ret.Msg = "opened notebook [" + notebook + "] not found"
  175. return
  176. }
  177. evt := util.NewCmdResult("mount", 0, util.PushModeBroadcast)
  178. evt.Data = map[string]interface{}{
  179. "box": box,
  180. "existed": existed,
  181. }
  182. evt.Callback = arg["callback"]
  183. util.PushEvent(evt)
  184. }
  185. func closeNotebook(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. notebook := arg["notebook"].(string)
  193. if util.InvalidIDPattern(notebook, ret) {
  194. return
  195. }
  196. model.Unmount(notebook)
  197. }
  198. func getNotebookConf(c *gin.Context) {
  199. ret := gulu.Ret.NewResult()
  200. defer c.JSON(http.StatusOK, ret)
  201. arg, ok := util.JsonArg(c, ret)
  202. if !ok {
  203. return
  204. }
  205. notebook := arg["notebook"].(string)
  206. if util.InvalidIDPattern(notebook, ret) {
  207. return
  208. }
  209. box := model.Conf.GetBox(notebook)
  210. if nil == box {
  211. ret.Code = -1
  212. ret.Msg = "notebook [" + notebook + "] not found"
  213. return
  214. }
  215. ret.Data = map[string]interface{}{
  216. "box": box.ID,
  217. "name": box.Name,
  218. "conf": box.GetConf(),
  219. }
  220. }
  221. func setNotebookConf(c *gin.Context) {
  222. ret := gulu.Ret.NewResult()
  223. defer c.JSON(http.StatusOK, ret)
  224. arg, ok := util.JsonArg(c, ret)
  225. if !ok {
  226. return
  227. }
  228. notebook := arg["notebook"].(string)
  229. if util.InvalidIDPattern(notebook, ret) {
  230. return
  231. }
  232. box := model.Conf.GetBox(notebook)
  233. if nil == box {
  234. ret.Code = -1
  235. ret.Msg = "notebook [" + notebook + "] not found"
  236. return
  237. }
  238. param, err := gulu.JSON.MarshalJSON(arg["conf"])
  239. if nil != err {
  240. ret.Code = -1
  241. ret.Msg = err.Error()
  242. return
  243. }
  244. boxConf := box.GetConf()
  245. if err = gulu.JSON.UnmarshalJSON(param, boxConf); nil != err {
  246. ret.Code = -1
  247. ret.Msg = err.Error()
  248. return
  249. }
  250. boxConf.RefCreateSavePath = strings.TrimSpace(boxConf.RefCreateSavePath)
  251. if "" != boxConf.RefCreateSavePath {
  252. if !strings.HasSuffix(boxConf.RefCreateSavePath, "/") {
  253. boxConf.RefCreateSavePath += "/"
  254. }
  255. }
  256. boxConf.DailyNoteSavePath = strings.TrimSpace(boxConf.DailyNoteSavePath)
  257. if "" != boxConf.DailyNoteSavePath {
  258. if !strings.HasPrefix(boxConf.DailyNoteSavePath, "/") {
  259. boxConf.DailyNoteSavePath = "/" + boxConf.DailyNoteSavePath
  260. }
  261. }
  262. if "/" == boxConf.DailyNoteSavePath {
  263. ret.Code = -1
  264. ret.Msg = model.Conf.Language(49)
  265. return
  266. }
  267. boxConf.DailyNoteTemplatePath = strings.TrimSpace(boxConf.DailyNoteTemplatePath)
  268. if "" != boxConf.DailyNoteTemplatePath {
  269. if !strings.HasSuffix(boxConf.DailyNoteTemplatePath, ".md") {
  270. boxConf.DailyNoteTemplatePath += ".md"
  271. }
  272. if !strings.HasPrefix(boxConf.DailyNoteTemplatePath, "/") {
  273. boxConf.DailyNoteTemplatePath = "/" + boxConf.DailyNoteTemplatePath
  274. }
  275. }
  276. boxConf.DocCreateSavePath = strings.TrimSpace(boxConf.DocCreateSavePath)
  277. if "../" == boxConf.DocCreateSavePath {
  278. boxConf.DocCreateSavePath = "../Untitled"
  279. }
  280. if "/" == boxConf.DocCreateSavePath {
  281. boxConf.DocCreateSavePath = "/Untitled"
  282. }
  283. box.SaveConf(boxConf)
  284. ret.Data = boxConf
  285. }
  286. func lsNotebooks(c *gin.Context) {
  287. ret := gulu.Ret.NewResult()
  288. defer c.JSON(http.StatusOK, ret)
  289. flashcard := false
  290. // 兼容旧版接口,不能直接使用 util.JsonArg()
  291. arg := map[string]interface{}{}
  292. if err := c.ShouldBindJSON(&arg); nil == err {
  293. if arg["flashcard"] != nil {
  294. flashcard = arg["flashcard"].(bool)
  295. }
  296. }
  297. var notebooks []*model.Box
  298. if flashcard {
  299. notebooks = model.GetFlashcardNotebooks()
  300. } else {
  301. var err error
  302. notebooks, err = model.ListNotebooks()
  303. if nil != err {
  304. return
  305. }
  306. }
  307. ret.Data = map[string]interface{}{
  308. "notebooks": notebooks,
  309. }
  310. }