filetree.go 15 KB

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