filetree.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. "path"
  21. "regexp"
  22. "strings"
  23. "unicode/utf8"
  24. "github.com/88250/gulu"
  25. "github.com/gin-gonic/gin"
  26. "github.com/siyuan-note/siyuan/kernel/filesys"
  27. "github.com/siyuan-note/siyuan/kernel/model"
  28. "github.com/siyuan-note/siyuan/kernel/util"
  29. )
  30. func refreshFiletree(c *gin.Context) {
  31. ret := gulu.Ret.NewResult()
  32. defer c.JSON(http.StatusOK, ret)
  33. model.FullReindex()
  34. }
  35. func doc2Heading(c *gin.Context) {
  36. ret := gulu.Ret.NewResult()
  37. defer c.JSON(http.StatusOK, ret)
  38. arg, ok := util.JsonArg(c, ret)
  39. if !ok {
  40. return
  41. }
  42. srcID := arg["srcID"].(string)
  43. targetID := arg["targetID"].(string)
  44. after := arg["after"].(bool)
  45. srcTreeBox, srcTreePath, err := model.Doc2Heading(srcID, targetID, after)
  46. if nil != err {
  47. ret.Code = -1
  48. ret.Msg = err.Error()
  49. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  50. return
  51. }
  52. ret.Data = map[string]interface{}{
  53. "srcTreeBox": srcTreeBox,
  54. "srcTreePath": srcTreePath,
  55. }
  56. }
  57. func heading2Doc(c *gin.Context) {
  58. ret := gulu.Ret.NewResult()
  59. defer c.JSON(http.StatusOK, ret)
  60. arg, ok := util.JsonArg(c, ret)
  61. if !ok {
  62. return
  63. }
  64. srcHeadingID := arg["srcHeadingID"].(string)
  65. targetNotebook := arg["targetNoteBook"].(string)
  66. targetPath := arg["targetPath"].(string)
  67. srcRootBlockID, targetPath, err := model.Heading2Doc(srcHeadingID, targetNotebook, targetPath)
  68. if nil != err {
  69. ret.Code = -1
  70. ret.Msg = err.Error()
  71. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  72. return
  73. }
  74. model.WaitForWritingFiles()
  75. luteEngine := util.NewLute()
  76. tree, err := filesys.LoadTree(targetNotebook, targetPath, luteEngine)
  77. if nil != err {
  78. ret.Code = -1
  79. ret.Msg = err.Error()
  80. return
  81. }
  82. name := path.Base(targetPath)
  83. box := model.Conf.Box(targetNotebook)
  84. files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), model.Conf.FileTree.Sort)
  85. evt := util.NewCmdResult("heading2doc", 0, util.PushModeBroadcast)
  86. evt.Data = map[string]interface{}{
  87. "box": box,
  88. "path": targetPath,
  89. "files": files,
  90. "name": name,
  91. "id": tree.Root.ID,
  92. "srcRootBlockID": srcRootBlockID,
  93. }
  94. evt.Callback = arg["callback"]
  95. util.PushEvent(evt)
  96. }
  97. func li2Doc(c *gin.Context) {
  98. ret := gulu.Ret.NewResult()
  99. defer c.JSON(http.StatusOK, ret)
  100. arg, ok := util.JsonArg(c, ret)
  101. if !ok {
  102. return
  103. }
  104. srcListItemID := arg["srcListItemID"].(string)
  105. targetNotebook := arg["targetNoteBook"].(string)
  106. targetPath := arg["targetPath"].(string)
  107. srcRootBlockID, targetPath, err := model.ListItem2Doc(srcListItemID, targetNotebook, targetPath)
  108. if nil != err {
  109. ret.Code = -1
  110. ret.Msg = err.Error()
  111. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  112. return
  113. }
  114. model.WaitForWritingFiles()
  115. luteEngine := util.NewLute()
  116. tree, err := filesys.LoadTree(targetNotebook, targetPath, luteEngine)
  117. if nil != err {
  118. ret.Code = -1
  119. ret.Msg = err.Error()
  120. return
  121. }
  122. name := path.Base(targetPath)
  123. box := model.Conf.Box(targetNotebook)
  124. files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), model.Conf.FileTree.Sort)
  125. evt := util.NewCmdResult("li2doc", 0, util.PushModeBroadcast)
  126. evt.Data = map[string]interface{}{
  127. "box": box,
  128. "path": targetPath,
  129. "files": files,
  130. "name": name,
  131. "id": tree.Root.ID,
  132. "srcRootBlockID": srcRootBlockID,
  133. }
  134. evt.Callback = arg["callback"]
  135. util.PushEvent(evt)
  136. }
  137. func getHPathByPath(c *gin.Context) {
  138. ret := gulu.Ret.NewResult()
  139. defer c.JSON(http.StatusOK, ret)
  140. arg, ok := util.JsonArg(c, ret)
  141. if !ok {
  142. return
  143. }
  144. notebook := arg["notebook"].(string)
  145. if util.InvalidIDPattern(notebook, ret) {
  146. return
  147. }
  148. p := arg["path"].(string)
  149. hPath, err := model.GetHPathByPath(notebook, p)
  150. if nil != err {
  151. ret.Code = -1
  152. ret.Msg = err.Error()
  153. return
  154. }
  155. ret.Data = hPath
  156. }
  157. func getHPathsByPaths(c *gin.Context) {
  158. ret := gulu.Ret.NewResult()
  159. defer c.JSON(http.StatusOK, ret)
  160. arg, ok := util.JsonArg(c, ret)
  161. if !ok {
  162. return
  163. }
  164. pathsArg := arg["paths"].([]interface{})
  165. var paths []string
  166. for _, p := range pathsArg {
  167. paths = append(paths, p.(string))
  168. }
  169. hPath, err := model.GetHPathsByPaths(paths)
  170. if nil != err {
  171. ret.Code = -1
  172. ret.Msg = err.Error()
  173. return
  174. }
  175. ret.Data = hPath
  176. }
  177. func getHPathByID(c *gin.Context) {
  178. ret := gulu.Ret.NewResult()
  179. defer c.JSON(http.StatusOK, ret)
  180. arg, ok := util.JsonArg(c, ret)
  181. if !ok {
  182. return
  183. }
  184. id := arg["id"].(string)
  185. if util.InvalidIDPattern(id, ret) {
  186. return
  187. }
  188. hPath, err := model.GetHPathByID(id)
  189. if nil != err {
  190. ret.Code = -1
  191. ret.Msg = err.Error()
  192. return
  193. }
  194. ret.Data = hPath
  195. }
  196. func getFullHPathByID(c *gin.Context) {
  197. ret := gulu.Ret.NewResult()
  198. defer c.JSON(http.StatusOK, ret)
  199. arg, ok := util.JsonArg(c, ret)
  200. if !ok {
  201. return
  202. }
  203. if nil == arg["id"] {
  204. return
  205. }
  206. id := arg["id"].(string)
  207. hPath, err := model.GetFullHPathByID(id)
  208. if nil != err {
  209. ret.Code = -1
  210. ret.Msg = err.Error()
  211. return
  212. }
  213. ret.Data = hPath
  214. }
  215. func moveDocs(c *gin.Context) {
  216. ret := gulu.Ret.NewResult()
  217. defer c.JSON(http.StatusOK, ret)
  218. arg, ok := util.JsonArg(c, ret)
  219. if !ok {
  220. return
  221. }
  222. var fromPaths []string
  223. fromPathsArg := arg["fromPaths"].([]interface{})
  224. for _, fromPath := range fromPathsArg {
  225. fromPaths = append(fromPaths, fromPath.(string))
  226. }
  227. toPath := arg["toPath"].(string)
  228. toNotebook := arg["toNotebook"].(string)
  229. if util.InvalidIDPattern(toNotebook, ret) {
  230. return
  231. }
  232. err := model.MoveDocs(fromPaths, toNotebook, toPath)
  233. if nil != err {
  234. ret.Code = -1
  235. ret.Msg = err.Error()
  236. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  237. return
  238. }
  239. }
  240. func removeDoc(c *gin.Context) {
  241. ret := gulu.Ret.NewResult()
  242. defer c.JSON(http.StatusOK, ret)
  243. arg, ok := util.JsonArg(c, ret)
  244. if !ok {
  245. return
  246. }
  247. notebook := arg["notebook"].(string)
  248. if util.InvalidIDPattern(notebook, ret) {
  249. return
  250. }
  251. p := arg["path"].(string)
  252. model.RemoveDoc(notebook, p)
  253. }
  254. func removeDocs(c *gin.Context) {
  255. ret := gulu.Ret.NewResult()
  256. defer c.JSON(http.StatusOK, ret)
  257. arg, ok := util.JsonArg(c, ret)
  258. if !ok {
  259. return
  260. }
  261. pathsArg := arg["paths"].([]interface{})
  262. var paths []string
  263. for _, path := range pathsArg {
  264. paths = append(paths, path.(string))
  265. }
  266. model.RemoveDocs(paths)
  267. }
  268. func renameDoc(c *gin.Context) {
  269. ret := gulu.Ret.NewResult()
  270. defer c.JSON(http.StatusOK, ret)
  271. arg, ok := util.JsonArg(c, ret)
  272. if !ok {
  273. return
  274. }
  275. notebook := arg["notebook"].(string)
  276. if util.InvalidIDPattern(notebook, ret) {
  277. return
  278. }
  279. p := arg["path"].(string)
  280. title := arg["title"].(string)
  281. err := model.RenameDoc(notebook, p, title)
  282. if nil != err {
  283. ret.Code = -1
  284. ret.Msg = err.Error()
  285. return
  286. }
  287. return
  288. }
  289. func duplicateDoc(c *gin.Context) {
  290. ret := gulu.Ret.NewResult()
  291. defer c.JSON(http.StatusOK, ret)
  292. arg, ok := util.JsonArg(c, ret)
  293. if !ok {
  294. return
  295. }
  296. id := arg["id"].(string)
  297. tree, err := model.LoadTreeByID(id)
  298. if nil != err {
  299. ret.Code = -1
  300. ret.Msg = err.Error()
  301. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  302. return
  303. }
  304. p := tree.Path
  305. notebook := tree.Box
  306. box := model.Conf.Box(notebook)
  307. model.DuplicateDoc(tree)
  308. pushCreate(box, p, tree.Root.ID, arg)
  309. ret.Data = map[string]interface{}{
  310. "id": tree.Root.ID,
  311. "notebook": notebook,
  312. "path": tree.Path,
  313. "hPath": tree.HPath,
  314. }
  315. }
  316. func createDoc(c *gin.Context) {
  317. ret := gulu.Ret.NewResult()
  318. defer c.JSON(http.StatusOK, ret)
  319. arg, ok := util.JsonArg(c, ret)
  320. if !ok {
  321. return
  322. }
  323. notebook := arg["notebook"].(string)
  324. p := arg["path"].(string)
  325. title := arg["title"].(string)
  326. md := arg["md"].(string)
  327. sortsArg := arg["sorts"]
  328. var sorts []string
  329. if nil != sortsArg {
  330. for _, sort := range sortsArg.([]interface{}) {
  331. sorts = append(sorts, sort.(string))
  332. }
  333. }
  334. tree, err := model.CreateDocByMd(notebook, p, title, md, sorts)
  335. if nil != err {
  336. ret.Code = -1
  337. ret.Msg = err.Error()
  338. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  339. return
  340. }
  341. box := model.Conf.Box(notebook)
  342. pushCreate(box, p, tree.Root.ID, arg)
  343. }
  344. func createDailyNote(c *gin.Context) {
  345. ret := gulu.Ret.NewResult()
  346. defer c.JSON(http.StatusOK, ret)
  347. arg, ok := util.JsonArg(c, ret)
  348. if !ok {
  349. return
  350. }
  351. notebook := arg["notebook"].(string)
  352. p, existed, err := model.CreateDailyNote(notebook)
  353. if nil != err {
  354. if model.ErrBoxNotFound == err {
  355. ret.Code = 1
  356. } else {
  357. ret.Code = -1
  358. }
  359. ret.Msg = err.Error()
  360. return
  361. }
  362. box := model.Conf.Box(notebook)
  363. model.WaitForWritingFiles()
  364. luteEngine := util.NewLute()
  365. tree, err := filesys.LoadTree(box.ID, p, luteEngine)
  366. if nil != err {
  367. ret.Code = -1
  368. ret.Msg = err.Error()
  369. return
  370. }
  371. appArg := arg["app"]
  372. app := ""
  373. if nil != appArg {
  374. app = appArg.(string)
  375. }
  376. pushMode := util.PushModeBroadcast
  377. if existed && "" != app {
  378. pushMode = util.PushModeBroadcastApp
  379. }
  380. evt := util.NewCmdResult("createdailynote", 0, pushMode)
  381. evt.AppId = app
  382. name := path.Base(p)
  383. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), model.Conf.FileTree.Sort)
  384. evt.Data = map[string]interface{}{
  385. "box": box,
  386. "path": p,
  387. "files": files,
  388. "name": name,
  389. "id": tree.Root.ID,
  390. }
  391. evt.Callback = arg["callback"]
  392. util.PushEvent(evt)
  393. }
  394. func createDocWithMd(c *gin.Context) {
  395. ret := gulu.Ret.NewResult()
  396. defer c.JSON(http.StatusOK, ret)
  397. arg, ok := util.JsonArg(c, ret)
  398. if !ok {
  399. return
  400. }
  401. notebook := arg["notebook"].(string)
  402. if util.InvalidIDPattern(notebook, ret) {
  403. return
  404. }
  405. hPath := arg["path"].(string)
  406. markdown := arg["markdown"].(string)
  407. baseName := path.Base(hPath)
  408. dir := path.Dir(hPath)
  409. r, _ := regexp.Compile("\r\n|\r|\n|\u2028|\u2029|\t|/")
  410. baseName = r.ReplaceAllString(baseName, "")
  411. if 512 < utf8.RuneCountInString(baseName) {
  412. baseName = gulu.Str.SubStr(baseName, 512)
  413. }
  414. hPath = path.Join(dir, baseName)
  415. if !strings.HasPrefix(hPath, "/") {
  416. hPath = "/" + hPath
  417. }
  418. id, err := model.CreateWithMarkdown(notebook, hPath, markdown)
  419. if nil != err {
  420. ret.Code = -1
  421. ret.Msg = err.Error()
  422. return
  423. }
  424. ret.Data = id
  425. box := model.Conf.Box(notebook)
  426. b, _ := model.GetBlock(id, nil)
  427. p := b.Path
  428. pushCreate(box, p, id, arg)
  429. }
  430. func getDocCreateSavePath(c *gin.Context) {
  431. ret := gulu.Ret.NewResult()
  432. defer c.JSON(http.StatusOK, ret)
  433. arg, ok := util.JsonArg(c, ret)
  434. if !ok {
  435. return
  436. }
  437. notebook := arg["notebook"].(string)
  438. box := model.Conf.Box(notebook)
  439. docCreateSavePathTpl := model.Conf.FileTree.DocCreateSavePath
  440. if nil != box {
  441. docCreateSavePathTpl = box.GetConf().DocCreateSavePath
  442. }
  443. if "" == docCreateSavePathTpl {
  444. docCreateSavePathTpl = model.Conf.FileTree.DocCreateSavePath
  445. }
  446. p, err := model.RenderGoTemplate(docCreateSavePathTpl)
  447. if nil != err {
  448. ret.Code = -1
  449. ret.Msg = err.Error()
  450. return
  451. }
  452. ret.Data = map[string]interface{}{
  453. "path": p,
  454. }
  455. }
  456. func getRefCreateSavePath(c *gin.Context) {
  457. ret := gulu.Ret.NewResult()
  458. defer c.JSON(http.StatusOK, ret)
  459. arg, ok := util.JsonArg(c, ret)
  460. if !ok {
  461. return
  462. }
  463. notebook := arg["notebook"].(string)
  464. box := model.Conf.Box(notebook)
  465. refCreateSavePath := model.Conf.FileTree.RefCreateSavePath
  466. if nil != box {
  467. refCreateSavePath = box.GetConf().RefCreateSavePath
  468. }
  469. if "" == refCreateSavePath {
  470. refCreateSavePath = model.Conf.FileTree.RefCreateSavePath
  471. }
  472. p, err := model.RenderGoTemplate(refCreateSavePath)
  473. if nil != err {
  474. ret.Code = -1
  475. ret.Msg = err.Error()
  476. return
  477. }
  478. ret.Data = map[string]interface{}{
  479. "path": p,
  480. }
  481. }
  482. func changeSort(c *gin.Context) {
  483. ret := gulu.Ret.NewResult()
  484. defer c.JSON(http.StatusOK, ret)
  485. arg, ok := util.JsonArg(c, ret)
  486. if !ok {
  487. return
  488. }
  489. notebook := arg["notebook"].(string)
  490. pathsArg := arg["paths"].([]interface{})
  491. var paths []string
  492. for _, p := range pathsArg {
  493. paths = append(paths, p.(string))
  494. }
  495. model.ChangeFileTreeSort(notebook, paths)
  496. }
  497. func searchDocs(c *gin.Context) {
  498. ret := gulu.Ret.NewResult()
  499. defer c.JSON(http.StatusOK, ret)
  500. arg, ok := util.JsonArg(c, ret)
  501. if !ok {
  502. return
  503. }
  504. k := arg["k"].(string)
  505. ret.Data = model.SearchDocsByKeyword(k)
  506. }
  507. func listDocsByPath(c *gin.Context) {
  508. ret := gulu.Ret.NewResult()
  509. defer c.JSON(http.StatusOK, ret)
  510. arg, ok := util.JsonArg(c, ret)
  511. if !ok {
  512. return
  513. }
  514. notebook := arg["notebook"].(string)
  515. p := arg["path"].(string)
  516. sortParam := arg["sort"]
  517. sortMode := model.Conf.FileTree.Sort
  518. if nil != sortParam {
  519. sortMode = int(sortParam.(float64))
  520. }
  521. files, totals, err := model.ListDocTree(notebook, p, sortMode)
  522. if nil != err {
  523. ret.Code = -1
  524. ret.Msg = err.Error()
  525. return
  526. }
  527. if model.Conf.FileTree.MaxListCount < totals {
  528. util.PushMsg(fmt.Sprintf(model.Conf.Language(48), len(files)), 7000)
  529. }
  530. ret.Data = map[string]interface{}{
  531. "box": notebook,
  532. "path": p,
  533. "files": files,
  534. }
  535. }
  536. func getDoc(c *gin.Context) {
  537. ret := gulu.Ret.NewResult()
  538. defer c.JSON(http.StatusOK, ret)
  539. arg, ok := util.JsonArg(c, ret)
  540. if !ok {
  541. return
  542. }
  543. id := arg["id"].(string)
  544. idx := arg["index"]
  545. index := 0
  546. if nil != idx {
  547. index = int(idx.(float64))
  548. }
  549. k := arg["k"]
  550. var keyword string
  551. if nil != k {
  552. keyword = k.(string)
  553. }
  554. m := arg["mode"] // 0: 仅当前 ID,1:向上 2:向下,3:上下都加载,4:加载末尾
  555. mode := 0
  556. if nil != m {
  557. mode = int(m.(float64))
  558. }
  559. s := arg["size"]
  560. size := 102400 // 默认最大加载块数
  561. if nil != s {
  562. size = int(s.(float64))
  563. }
  564. startID := ""
  565. endID := ""
  566. startIDArg := arg["startID"]
  567. endIDArg := arg["endID"]
  568. if nil != startIDArg && nil != endIDArg {
  569. startID = startIDArg.(string)
  570. endID = endIDArg.(string)
  571. size = 36
  572. }
  573. isBacklinkArg := arg["isBacklink"]
  574. isBacklink := false
  575. if nil != isBacklinkArg {
  576. isBacklink = isBacklinkArg.(bool)
  577. }
  578. blockCount, content, parentID, parent2ID, rootID, typ, eof, scroll, boxID, docPath, isBacklinkExpand, err := model.GetDoc(startID, endID, id, index, keyword, mode, size, isBacklink)
  579. if model.ErrBlockNotFound == err {
  580. ret.Code = 3
  581. return
  582. }
  583. if nil != err {
  584. ret.Code = 1
  585. ret.Msg = err.Error()
  586. return
  587. }
  588. // 判断是否正在同步中 https://github.com/siyuan-note/siyuan/issues/6290
  589. isSyncing := model.IsSyncingFile(rootID)
  590. ret.Data = map[string]interface{}{
  591. "id": id,
  592. "mode": mode,
  593. "parentID": parentID,
  594. "parent2ID": parent2ID,
  595. "rootID": rootID,
  596. "type": typ,
  597. "content": content,
  598. "blockCount": blockCount,
  599. "eof": eof,
  600. "scroll": scroll,
  601. "box": boxID,
  602. "path": docPath,
  603. "isSyncing": isSyncing,
  604. "isBacklinkExpand": isBacklinkExpand,
  605. }
  606. }
  607. func pushCreate(box *model.Box, p, treeID string, arg map[string]interface{}) {
  608. evt := util.NewCmdResult("create", 0, util.PushModeBroadcast)
  609. name := path.Base(p)
  610. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), model.Conf.FileTree.Sort)
  611. evt.Data = map[string]interface{}{
  612. "box": box,
  613. "path": p,
  614. "files": files,
  615. "name": name,
  616. "id": treeID,
  617. }
  618. evt.Callback = arg["callback"]
  619. util.PushEvent(evt)
  620. }