filetree.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 getHPathByID(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. id := arg["id"].(string)
  177. hPath, err := model.GetHPathByID(id)
  178. if nil != err {
  179. ret.Code = -1
  180. ret.Msg = err.Error()
  181. return
  182. }
  183. ret.Data = hPath
  184. }
  185. func getFullHPathByID(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. if nil == arg["id"] {
  193. return
  194. }
  195. id := arg["id"].(string)
  196. hPath, err := model.GetFullHPathByID(id)
  197. if nil != err {
  198. ret.Code = -1
  199. ret.Msg = err.Error()
  200. return
  201. }
  202. ret.Data = hPath
  203. }
  204. func moveDoc(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. fromNotebook := arg["fromNotebook"].(string)
  212. toNotebook := arg["toNotebook"].(string)
  213. fromPath := arg["fromPath"].(string)
  214. toPath := arg["toPath"].(string)
  215. newPath, err := model.MoveDoc(fromNotebook, fromPath, toNotebook, toPath)
  216. if nil != err {
  217. ret.Code = -1
  218. ret.Msg = err.Error()
  219. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  220. return
  221. }
  222. evt := util.NewCmdResult("moveDoc", 0, util.PushModeBroadcast, util.PushModeNone)
  223. evt.Data = map[string]interface{}{
  224. "fromNotebook": fromNotebook,
  225. "toNotebook": toNotebook,
  226. "fromPath": fromPath,
  227. "toPath": toPath,
  228. "newPath": newPath,
  229. }
  230. util.PushEvent(evt)
  231. }
  232. func removeDoc(c *gin.Context) {
  233. ret := gulu.Ret.NewResult()
  234. defer c.JSON(http.StatusOK, ret)
  235. arg, ok := util.JsonArg(c, ret)
  236. if !ok {
  237. return
  238. }
  239. notebook := arg["notebook"].(string)
  240. p := arg["path"].(string)
  241. err := model.RemoveDoc(notebook, p)
  242. if nil != err {
  243. ret.Code = -1
  244. ret.Msg = err.Error()
  245. return
  246. }
  247. evt := util.NewCmdResult("remove", 0, util.PushModeBroadcast, util.PushModeNone)
  248. evt.Data = map[string]interface{}{
  249. "box": notebook,
  250. "path": p,
  251. }
  252. util.PushEvent(evt)
  253. }
  254. func renameDoc(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. notebook := arg["notebook"].(string)
  262. p := arg["path"].(string)
  263. title := arg["title"].(string)
  264. err := model.RenameDoc(notebook, p, title)
  265. if nil != err {
  266. ret.Code = -1
  267. ret.Msg = err.Error()
  268. return
  269. }
  270. return
  271. }
  272. func duplicateDoc(c *gin.Context) {
  273. ret := gulu.Ret.NewResult()
  274. defer c.JSON(http.StatusOK, ret)
  275. arg, ok := util.JsonArg(c, ret)
  276. if !ok {
  277. return
  278. }
  279. id := arg["id"].(string)
  280. newTree, err := model.DuplicateDoc(id)
  281. if nil != err {
  282. ret.Code = -1
  283. ret.Msg = err.Error()
  284. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  285. return
  286. }
  287. block, _ := model.GetBlock(id)
  288. p := block.Path
  289. notebook := block.Box
  290. box := model.Conf.Box(notebook)
  291. tree, err := model.LoadTree(box.ID, p)
  292. if nil != err {
  293. ret.Code = -1
  294. ret.Msg = err.Error()
  295. return
  296. }
  297. pushCreate(box, p, tree.Root.ID, arg)
  298. ret.Data = map[string]interface{}{
  299. "id": newTree.Root.ID,
  300. "notebook": notebook,
  301. "path": newTree.Path,
  302. "hPath": newTree.HPath,
  303. }
  304. }
  305. func createDoc(c *gin.Context) {
  306. ret := gulu.Ret.NewResult()
  307. defer c.JSON(http.StatusOK, ret)
  308. arg, ok := util.JsonArg(c, ret)
  309. if !ok {
  310. return
  311. }
  312. notebook := arg["notebook"].(string)
  313. p := arg["path"].(string)
  314. title := arg["title"].(string)
  315. md := arg["md"].(string)
  316. sortsArg := arg["sorts"]
  317. var sorts []string
  318. if nil != sortsArg {
  319. for _, sort := range sortsArg.([]interface{}) {
  320. sorts = append(sorts, sort.(string))
  321. }
  322. }
  323. err := model.CreateDocByMd(notebook, p, title, md, sorts)
  324. if nil != err {
  325. ret.Code = -1
  326. ret.Msg = err.Error()
  327. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  328. return
  329. }
  330. box := model.Conf.Box(notebook)
  331. tree, err := model.LoadTree(box.ID, p)
  332. if nil != err {
  333. ret.Code = -1
  334. ret.Msg = err.Error()
  335. return
  336. }
  337. pushCreate(box, p, tree.Root.ID, arg)
  338. }
  339. func createDailyNote(c *gin.Context) {
  340. ret := gulu.Ret.NewResult()
  341. defer c.JSON(http.StatusOK, ret)
  342. arg, ok := util.JsonArg(c, ret)
  343. if !ok {
  344. return
  345. }
  346. notebook := arg["notebook"].(string)
  347. p, err := model.CreateDailyNote(notebook)
  348. if nil != err {
  349. if model.ErrBoxNotFound == err {
  350. ret.Code = 1
  351. } else {
  352. ret.Code = -1
  353. }
  354. ret.Msg = err.Error()
  355. return
  356. }
  357. box := model.Conf.Box(notebook)
  358. model.WaitForWritingFiles()
  359. tree, err := model.LoadTree(box.ID, p)
  360. if nil != err {
  361. ret.Code = -1
  362. ret.Msg = err.Error()
  363. return
  364. }
  365. evt := util.NewCmdResult("createdailynote", 0, util.PushModeBroadcast, util.PushModeNone)
  366. name := path.Base(p)
  367. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), model.Conf.FileTree.Sort)
  368. evt.Data = map[string]interface{}{
  369. "box": box,
  370. "path": p,
  371. "files": files,
  372. "name": name,
  373. "id": tree.Root.ID,
  374. }
  375. evt.Callback = arg["callback"]
  376. util.PushEvent(evt)
  377. }
  378. func createDocWithMd(c *gin.Context) {
  379. ret := gulu.Ret.NewResult()
  380. defer c.JSON(http.StatusOK, ret)
  381. arg, ok := util.JsonArg(c, ret)
  382. if !ok {
  383. return
  384. }
  385. notebook := arg["notebook"].(string)
  386. hPath := arg["path"].(string)
  387. markdown := arg["markdown"].(string)
  388. baseName := path.Base(hPath)
  389. dir := path.Dir(hPath)
  390. r, _ := regexp.Compile("\r\n|\r|\n|\u2028|\u2029|\t|/")
  391. baseName = r.ReplaceAllString(baseName, "")
  392. if 512 < utf8.RuneCountInString(baseName) {
  393. baseName = gulu.Str.SubStr(baseName, 512)
  394. }
  395. hPath = path.Join(dir, baseName)
  396. if !strings.HasPrefix(hPath, "/") {
  397. hPath = "/" + hPath
  398. }
  399. id, err := model.CreateWithMarkdown(notebook, hPath, markdown)
  400. if nil != err {
  401. ret.Code = -1
  402. ret.Msg = err.Error()
  403. return
  404. }
  405. ret.Data = id
  406. box := model.Conf.Box(notebook)
  407. b, _ := model.GetBlock(id)
  408. p := b.Path
  409. pushCreate(box, p, id, arg)
  410. }
  411. func lockFile(c *gin.Context) {
  412. ret := gulu.Ret.NewResult()
  413. defer c.JSON(http.StatusOK, ret)
  414. arg, ok := util.JsonArg(c, ret)
  415. if !ok {
  416. return
  417. }
  418. id := arg["id"].(string)
  419. locked := model.TryAccessFileByBlockID(id)
  420. if !locked {
  421. ret.Code = -1
  422. ret.Msg = fmt.Sprintf(model.Conf.Language(75))
  423. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  424. }
  425. }
  426. func getDocNameTemplate(c *gin.Context) {
  427. ret := gulu.Ret.NewResult()
  428. defer c.JSON(http.StatusOK, ret)
  429. arg, ok := util.JsonArg(c, ret)
  430. if !ok {
  431. return
  432. }
  433. notebook := arg["notebook"].(string)
  434. box := model.Conf.Box(notebook)
  435. nameTemplate := model.Conf.FileTree.CreateDocNameTemplate
  436. if nil != box {
  437. nameTemplate = box.GetConf().CreateDocNameTemplate
  438. }
  439. if "" == nameTemplate {
  440. nameTemplate = model.Conf.FileTree.CreateDocNameTemplate
  441. }
  442. name, err := model.RenderCreateDocNameTemplate(nameTemplate)
  443. if nil != err {
  444. ret.Code = -1
  445. ret.Msg = err.Error()
  446. return
  447. }
  448. ret.Data = map[string]interface{}{
  449. "name": name,
  450. }
  451. }
  452. func changeSort(c *gin.Context) {
  453. ret := gulu.Ret.NewResult()
  454. defer c.JSON(http.StatusOK, ret)
  455. arg, ok := util.JsonArg(c, ret)
  456. if !ok {
  457. return
  458. }
  459. notebook := arg["notebook"].(string)
  460. pathsArg := arg["paths"].([]interface{})
  461. var paths []string
  462. for _, p := range pathsArg {
  463. paths = append(paths, p.(string))
  464. }
  465. model.ChangeFileTreeSort(notebook, paths)
  466. }
  467. func searchDocs(c *gin.Context) {
  468. ret := gulu.Ret.NewResult()
  469. defer c.JSON(http.StatusOK, ret)
  470. arg, ok := util.JsonArg(c, ret)
  471. if !ok {
  472. return
  473. }
  474. k := arg["k"].(string)
  475. ret.Data = model.SearchDocsByKeyword(k)
  476. }
  477. func listDocsByPath(c *gin.Context) {
  478. ret := gulu.Ret.NewResult()
  479. defer c.JSON(http.StatusOK, ret)
  480. arg, ok := util.JsonArg(c, ret)
  481. if !ok {
  482. return
  483. }
  484. notebook := arg["notebook"].(string)
  485. p := arg["path"].(string)
  486. sortParam := arg["sort"]
  487. sortMode := model.Conf.FileTree.Sort
  488. if nil != sortParam {
  489. sortMode = int(sortParam.(float64))
  490. }
  491. files, totals, err := model.ListDocTree(notebook, p, sortMode)
  492. if nil != err {
  493. ret.Code = -1
  494. ret.Msg = err.Error()
  495. return
  496. }
  497. if model.Conf.FileTree.MaxListCount < totals {
  498. util.PushMsg(fmt.Sprintf(model.Conf.Language(48), len(files)), 7000)
  499. }
  500. ret.Data = map[string]interface{}{
  501. "box": notebook,
  502. "path": p,
  503. "files": files,
  504. }
  505. // 持久化文档面板排序
  506. model.Conf.FileTree.Sort = sortMode
  507. model.Conf.Save()
  508. }
  509. func getDoc(c *gin.Context) {
  510. ret := gulu.Ret.NewResult()
  511. defer c.JSON(http.StatusOK, ret)
  512. arg, ok := util.JsonArg(c, ret)
  513. if !ok {
  514. return
  515. }
  516. id := arg["id"].(string)
  517. idx := arg["index"]
  518. index := 0
  519. if nil != idx {
  520. index = int(idx.(float64))
  521. }
  522. k := arg["k"]
  523. var keyword string
  524. if nil != k {
  525. keyword = k.(string)
  526. }
  527. m := arg["mode"] // 0: 仅当前 ID,1:向上 2:向下,3:上下都加载,4:加载末尾
  528. mode := 0
  529. if nil != m {
  530. mode = int(m.(float64))
  531. }
  532. s := arg["size"]
  533. size := 102400 // 默认最大加载块数
  534. if nil != s {
  535. size = int(s.(float64))
  536. }
  537. startID := ""
  538. endID := ""
  539. startIDArg := arg["startID"]
  540. endIDArg := arg["endID"]
  541. if nil != startIDArg && nil != endIDArg {
  542. startID = startIDArg.(string)
  543. endID = endIDArg.(string)
  544. size = 36
  545. }
  546. blockCount, childBlockCount, content, parentID, parent2ID, rootID, typ, eof, boxID, docPath, err := model.GetDoc(startID, endID, id, index, keyword, mode, size)
  547. if errors.Is(err, filelock.ErrUnableAccessFile) {
  548. ret.Code = 2
  549. ret.Data = id
  550. return
  551. }
  552. if model.ErrBlockNotFound == err {
  553. ret.Code = 3
  554. return
  555. }
  556. if nil != err {
  557. ret.Code = 1
  558. ret.Msg = err.Error()
  559. return
  560. }
  561. ret.Data = map[string]interface{}{
  562. "id": id,
  563. "mode": mode,
  564. "parentID": parentID,
  565. "parent2ID": parent2ID,
  566. "rootID": rootID,
  567. "type": typ,
  568. "content": content,
  569. "blockCount": blockCount,
  570. "childBlockCount": childBlockCount,
  571. "eof": eof,
  572. "box": boxID,
  573. "path": docPath,
  574. }
  575. }
  576. func pushCreate(box *model.Box, p, treeID string, arg map[string]interface{}) {
  577. evt := util.NewCmdResult("create", 0, util.PushModeBroadcast, util.PushModeNone)
  578. name := path.Base(p)
  579. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), model.Conf.FileTree.Sort)
  580. evt.Data = map[string]interface{}{
  581. "box": box,
  582. "path": p,
  583. "files": files,
  584. "name": name,
  585. "id": treeID,
  586. }
  587. evt.Callback = arg["callback"]
  588. util.PushEvent(evt)
  589. }