filetree.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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. "fmt"
  19. "math"
  20. "net/http"
  21. "os"
  22. "path"
  23. "path/filepath"
  24. "regexp"
  25. "strings"
  26. "unicode/utf8"
  27. "github.com/88250/gulu"
  28. "github.com/88250/lute/ast"
  29. "github.com/gin-gonic/gin"
  30. "github.com/siyuan-note/siyuan/kernel/filesys"
  31. "github.com/siyuan-note/siyuan/kernel/model"
  32. "github.com/siyuan-note/siyuan/kernel/util"
  33. )
  34. func listDocTree(c *gin.Context) {
  35. // Add kernel API `/api/filetree/listDocTree` https://github.com/siyuan-note/siyuan/issues/10482
  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. notebook := arg["notebook"].(string)
  43. if util.InvalidIDPattern(notebook, ret) {
  44. return
  45. }
  46. p := arg["path"].(string)
  47. var doctree []*DocFile
  48. root := filepath.Join(util.WorkspaceDir, "data", notebook, p)
  49. dir, err := os.ReadDir(root)
  50. if nil != err {
  51. ret.Code = -1
  52. ret.Msg = err.Error()
  53. return
  54. }
  55. ids := map[string]bool{}
  56. for _, entry := range dir {
  57. if entry.IsDir() {
  58. if strings.HasPrefix(entry.Name(), ".") {
  59. continue
  60. }
  61. if !ast.IsNodeIDPattern(entry.Name()) {
  62. continue
  63. }
  64. parent := &DocFile{ID: entry.Name()}
  65. ids[parent.ID] = true
  66. doctree = append(doctree, parent)
  67. subPath := filepath.Join(root, entry.Name())
  68. if err = walkDocTree(subPath, parent, &ids); nil != err {
  69. ret.Code = -1
  70. ret.Msg = err.Error()
  71. return
  72. }
  73. } else {
  74. doc := &DocFile{ID: strings.TrimSuffix(entry.Name(), ".sy")}
  75. if !ids[doc.ID] {
  76. doctree = append(doctree, doc)
  77. }
  78. ids[doc.ID] = true
  79. }
  80. }
  81. ret.Data = map[string]interface{}{
  82. "tree": doctree,
  83. }
  84. }
  85. type DocFile struct {
  86. ID string `json:"id"`
  87. Children []*DocFile `json:"children,omitempty"`
  88. }
  89. func walkDocTree(p string, docFile *DocFile, ids *map[string]bool) (err error) {
  90. dir, err := os.ReadDir(p)
  91. if nil != err {
  92. return
  93. }
  94. for _, entry := range dir {
  95. if entry.IsDir() {
  96. if strings.HasPrefix(entry.Name(), ".") {
  97. continue
  98. }
  99. if !ast.IsNodeIDPattern(entry.Name()) {
  100. continue
  101. }
  102. parent := &DocFile{ID: entry.Name()}
  103. (*ids)[parent.ID] = true
  104. docFile.Children = append(docFile.Children, parent)
  105. subPath := filepath.Join(p, entry.Name())
  106. if err = walkDocTree(subPath, parent, ids); nil != err {
  107. return
  108. }
  109. } else {
  110. doc := &DocFile{ID: strings.TrimSuffix(entry.Name(), ".sy")}
  111. if !(*ids)[doc.ID] {
  112. docFile.Children = append(docFile.Children, doc)
  113. }
  114. (*ids)[doc.ID] = true
  115. }
  116. }
  117. return
  118. }
  119. func upsertIndexes(c *gin.Context) {
  120. ret := gulu.Ret.NewResult()
  121. defer c.JSON(http.StatusOK, ret)
  122. arg, ok := util.JsonArg(c, ret)
  123. if !ok {
  124. return
  125. }
  126. pathsArg := arg["paths"].([]interface{})
  127. var paths []string
  128. for _, p := range pathsArg {
  129. paths = append(paths, p.(string))
  130. }
  131. model.UpsertIndexes(paths)
  132. }
  133. func removeIndexes(c *gin.Context) {
  134. ret := gulu.Ret.NewResult()
  135. defer c.JSON(http.StatusOK, ret)
  136. arg, ok := util.JsonArg(c, ret)
  137. if !ok {
  138. return
  139. }
  140. pathsArg := arg["paths"].([]interface{})
  141. var paths []string
  142. for _, p := range pathsArg {
  143. paths = append(paths, p.(string))
  144. }
  145. model.RemoveIndexes(paths)
  146. }
  147. func refreshFiletree(c *gin.Context) {
  148. ret := gulu.Ret.NewResult()
  149. defer c.JSON(http.StatusOK, ret)
  150. model.FullReindex()
  151. }
  152. func doc2Heading(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. srcID := arg["srcID"].(string)
  160. targetID := arg["targetID"].(string)
  161. after := arg["after"].(bool)
  162. srcTreeBox, srcTreePath, err := model.Doc2Heading(srcID, targetID, after)
  163. if nil != err {
  164. ret.Code = -1
  165. ret.Msg = err.Error()
  166. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  167. return
  168. }
  169. ret.Data = map[string]interface{}{
  170. "srcTreeBox": srcTreeBox,
  171. "srcTreePath": srcTreePath,
  172. }
  173. }
  174. func heading2Doc(c *gin.Context) {
  175. ret := gulu.Ret.NewResult()
  176. defer c.JSON(http.StatusOK, ret)
  177. arg, ok := util.JsonArg(c, ret)
  178. if !ok {
  179. return
  180. }
  181. srcHeadingID := arg["srcHeadingID"].(string)
  182. targetNotebook := arg["targetNoteBook"].(string)
  183. targetPath := arg["targetPath"].(string)
  184. srcRootBlockID, targetPath, err := model.Heading2Doc(srcHeadingID, targetNotebook, targetPath)
  185. if nil != err {
  186. ret.Code = -1
  187. ret.Msg = err.Error()
  188. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  189. return
  190. }
  191. model.WaitForWritingFiles()
  192. luteEngine := util.NewLute()
  193. tree, err := filesys.LoadTree(targetNotebook, targetPath, luteEngine)
  194. if nil != err {
  195. ret.Code = -1
  196. ret.Msg = err.Error()
  197. return
  198. }
  199. name := path.Base(targetPath)
  200. box := model.Conf.Box(targetNotebook)
  201. files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  202. evt := util.NewCmdResult("heading2doc", 0, util.PushModeBroadcast)
  203. evt.Data = map[string]interface{}{
  204. "box": box,
  205. "path": targetPath,
  206. "files": files,
  207. "name": name,
  208. "id": tree.Root.ID,
  209. "srcRootBlockID": srcRootBlockID,
  210. }
  211. evt.Callback = arg["callback"]
  212. util.PushEvent(evt)
  213. }
  214. func li2Doc(c *gin.Context) {
  215. ret := gulu.Ret.NewResult()
  216. defer c.JSON(http.StatusOK, ret)
  217. arg, ok := util.JsonArg(c, ret)
  218. if !ok {
  219. return
  220. }
  221. srcListItemID := arg["srcListItemID"].(string)
  222. targetNotebook := arg["targetNoteBook"].(string)
  223. targetPath := arg["targetPath"].(string)
  224. srcRootBlockID, targetPath, err := model.ListItem2Doc(srcListItemID, targetNotebook, targetPath)
  225. if nil != err {
  226. ret.Code = -1
  227. ret.Msg = err.Error()
  228. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  229. return
  230. }
  231. model.WaitForWritingFiles()
  232. luteEngine := util.NewLute()
  233. tree, err := filesys.LoadTree(targetNotebook, targetPath, luteEngine)
  234. if nil != err {
  235. ret.Code = -1
  236. ret.Msg = err.Error()
  237. return
  238. }
  239. name := path.Base(targetPath)
  240. box := model.Conf.Box(targetNotebook)
  241. files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  242. evt := util.NewCmdResult("li2doc", 0, util.PushModeBroadcast)
  243. evt.Data = map[string]interface{}{
  244. "box": box,
  245. "path": targetPath,
  246. "files": files,
  247. "name": name,
  248. "id": tree.Root.ID,
  249. "srcRootBlockID": srcRootBlockID,
  250. }
  251. evt.Callback = arg["callback"]
  252. util.PushEvent(evt)
  253. }
  254. func getHPathByPath(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. if util.InvalidIDPattern(notebook, ret) {
  263. return
  264. }
  265. p := arg["path"].(string)
  266. hPath, err := model.GetHPathByPath(notebook, p)
  267. if nil != err {
  268. ret.Code = -1
  269. ret.Msg = err.Error()
  270. return
  271. }
  272. ret.Data = hPath
  273. }
  274. func getHPathsByPaths(c *gin.Context) {
  275. ret := gulu.Ret.NewResult()
  276. defer c.JSON(http.StatusOK, ret)
  277. arg, ok := util.JsonArg(c, ret)
  278. if !ok {
  279. return
  280. }
  281. pathsArg := arg["paths"].([]interface{})
  282. var paths []string
  283. for _, p := range pathsArg {
  284. paths = append(paths, p.(string))
  285. }
  286. hPath, err := model.GetHPathsByPaths(paths)
  287. if nil != err {
  288. ret.Code = -1
  289. ret.Msg = err.Error()
  290. return
  291. }
  292. ret.Data = hPath
  293. }
  294. func getHPathByID(c *gin.Context) {
  295. ret := gulu.Ret.NewResult()
  296. defer c.JSON(http.StatusOK, ret)
  297. arg, ok := util.JsonArg(c, ret)
  298. if !ok {
  299. return
  300. }
  301. id := arg["id"].(string)
  302. if util.InvalidIDPattern(id, ret) {
  303. return
  304. }
  305. hPath, err := model.GetHPathByID(id)
  306. if nil != err {
  307. ret.Code = -1
  308. ret.Msg = err.Error()
  309. return
  310. }
  311. ret.Data = hPath
  312. }
  313. func getFullHPathByID(c *gin.Context) {
  314. ret := gulu.Ret.NewResult()
  315. defer c.JSON(http.StatusOK, ret)
  316. arg, ok := util.JsonArg(c, ret)
  317. if !ok {
  318. return
  319. }
  320. if nil == arg["id"] {
  321. return
  322. }
  323. id := arg["id"].(string)
  324. hPath, err := model.GetFullHPathByID(id)
  325. if nil != err {
  326. ret.Code = -1
  327. ret.Msg = err.Error()
  328. return
  329. }
  330. ret.Data = hPath
  331. }
  332. func getIDsByHPath(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. if nil == arg["path"] {
  340. return
  341. }
  342. if nil == arg["notebook"] {
  343. return
  344. }
  345. notebook := arg["notebook"].(string)
  346. if util.InvalidIDPattern(notebook, ret) {
  347. return
  348. }
  349. p := arg["path"].(string)
  350. ids, err := model.GetIDsByHPath(p, notebook)
  351. if nil != err {
  352. ret.Code = -1
  353. ret.Msg = err.Error()
  354. return
  355. }
  356. ret.Data = ids
  357. }
  358. func moveDocs(c *gin.Context) {
  359. ret := gulu.Ret.NewResult()
  360. defer c.JSON(http.StatusOK, ret)
  361. arg, ok := util.JsonArg(c, ret)
  362. if !ok {
  363. return
  364. }
  365. var fromPaths []string
  366. fromPathsArg := arg["fromPaths"].([]interface{})
  367. for _, fromPath := range fromPathsArg {
  368. fromPaths = append(fromPaths, fromPath.(string))
  369. }
  370. toPath := arg["toPath"].(string)
  371. toNotebook := arg["toNotebook"].(string)
  372. if util.InvalidIDPattern(toNotebook, ret) {
  373. return
  374. }
  375. callback := arg["callback"]
  376. err := model.MoveDocs(fromPaths, toNotebook, toPath, callback)
  377. if nil != err {
  378. ret.Code = -1
  379. ret.Msg = err.Error()
  380. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  381. return
  382. }
  383. }
  384. func removeDoc(c *gin.Context) {
  385. ret := gulu.Ret.NewResult()
  386. defer c.JSON(http.StatusOK, ret)
  387. arg, ok := util.JsonArg(c, ret)
  388. if !ok {
  389. return
  390. }
  391. notebook := arg["notebook"].(string)
  392. if util.InvalidIDPattern(notebook, ret) {
  393. return
  394. }
  395. p := arg["path"].(string)
  396. model.RemoveDoc(notebook, p)
  397. }
  398. func removeDocs(c *gin.Context) {
  399. ret := gulu.Ret.NewResult()
  400. defer c.JSON(http.StatusOK, ret)
  401. arg, ok := util.JsonArg(c, ret)
  402. if !ok {
  403. return
  404. }
  405. pathsArg := arg["paths"].([]interface{})
  406. var paths []string
  407. for _, path := range pathsArg {
  408. paths = append(paths, path.(string))
  409. }
  410. model.RemoveDocs(paths)
  411. }
  412. func renameDoc(c *gin.Context) {
  413. ret := gulu.Ret.NewResult()
  414. defer c.JSON(http.StatusOK, ret)
  415. arg, ok := util.JsonArg(c, ret)
  416. if !ok {
  417. return
  418. }
  419. notebook := arg["notebook"].(string)
  420. if util.InvalidIDPattern(notebook, ret) {
  421. return
  422. }
  423. p := arg["path"].(string)
  424. title := arg["title"].(string)
  425. err := model.RenameDoc(notebook, p, title)
  426. if nil != err {
  427. ret.Code = -1
  428. ret.Msg = err.Error()
  429. return
  430. }
  431. return
  432. }
  433. func duplicateDoc(c *gin.Context) {
  434. ret := gulu.Ret.NewResult()
  435. defer c.JSON(http.StatusOK, ret)
  436. arg, ok := util.JsonArg(c, ret)
  437. if !ok {
  438. return
  439. }
  440. id := arg["id"].(string)
  441. tree, err := model.LoadTreeByID(id)
  442. if nil != err {
  443. ret.Code = -1
  444. ret.Msg = err.Error()
  445. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  446. return
  447. }
  448. p := tree.Path
  449. notebook := tree.Box
  450. box := model.Conf.Box(notebook)
  451. model.DuplicateDoc(tree)
  452. pushCreate(box, p, tree.Root.ID, arg)
  453. ret.Data = map[string]interface{}{
  454. "id": tree.Root.ID,
  455. "notebook": notebook,
  456. "path": tree.Path,
  457. "hPath": tree.HPath,
  458. }
  459. }
  460. func createDoc(c *gin.Context) {
  461. ret := gulu.Ret.NewResult()
  462. defer c.JSON(http.StatusOK, ret)
  463. arg, ok := util.JsonArg(c, ret)
  464. if !ok {
  465. return
  466. }
  467. notebook := arg["notebook"].(string)
  468. p := arg["path"].(string)
  469. title := arg["title"].(string)
  470. md := arg["md"].(string)
  471. sortsArg := arg["sorts"]
  472. var sorts []string
  473. if nil != sortsArg {
  474. for _, sort := range sortsArg.([]interface{}) {
  475. sorts = append(sorts, sort.(string))
  476. }
  477. }
  478. tree, err := model.CreateDocByMd(notebook, p, title, md, sorts)
  479. if nil != err {
  480. ret.Code = -1
  481. ret.Msg = err.Error()
  482. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  483. return
  484. }
  485. model.WaitForWritingFiles()
  486. box := model.Conf.Box(notebook)
  487. pushCreate(box, p, tree.Root.ID, arg)
  488. ret.Data = map[string]interface{}{
  489. "id": tree.Root.ID,
  490. }
  491. }
  492. func createDailyNote(c *gin.Context) {
  493. ret := gulu.Ret.NewResult()
  494. defer c.JSON(http.StatusOK, ret)
  495. arg, ok := util.JsonArg(c, ret)
  496. if !ok {
  497. return
  498. }
  499. notebook := arg["notebook"].(string)
  500. p, existed, err := model.CreateDailyNote(notebook)
  501. if nil != err {
  502. if model.ErrBoxNotFound == err {
  503. ret.Code = 1
  504. } else {
  505. ret.Code = -1
  506. }
  507. ret.Msg = err.Error()
  508. return
  509. }
  510. model.WaitForWritingFiles()
  511. box := model.Conf.Box(notebook)
  512. luteEngine := util.NewLute()
  513. tree, err := filesys.LoadTree(box.ID, p, luteEngine)
  514. if nil != err {
  515. ret.Code = -1
  516. ret.Msg = err.Error()
  517. return
  518. }
  519. if !existed {
  520. // 只有创建的情况才推送,已经存在的情况不推送
  521. // Creating a dailynote existed no longer expands the doc tree https://github.com/siyuan-note/siyuan/issues/9959
  522. appArg := arg["app"]
  523. app := ""
  524. if nil != appArg {
  525. app = appArg.(string)
  526. }
  527. evt := util.NewCmdResult("createdailynote", 0, util.PushModeBroadcast)
  528. evt.AppId = app
  529. name := path.Base(p)
  530. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  531. evt.Data = map[string]interface{}{
  532. "box": box,
  533. "path": p,
  534. "files": files,
  535. "name": name,
  536. "id": tree.Root.ID,
  537. }
  538. evt.Callback = arg["callback"]
  539. util.PushEvent(evt)
  540. }
  541. ret.Data = map[string]interface{}{
  542. "id": tree.Root.ID,
  543. }
  544. }
  545. func createDocWithMd(c *gin.Context) {
  546. ret := gulu.Ret.NewResult()
  547. defer c.JSON(http.StatusOK, ret)
  548. arg, ok := util.JsonArg(c, ret)
  549. if !ok {
  550. return
  551. }
  552. notebook := arg["notebook"].(string)
  553. if util.InvalidIDPattern(notebook, ret) {
  554. return
  555. }
  556. var parentID string
  557. parentIDArg := arg["parentID"]
  558. if nil != parentIDArg {
  559. parentID = parentIDArg.(string)
  560. }
  561. id := ast.NewNodeID()
  562. idArg := arg["id"]
  563. if nil != idArg {
  564. id = idArg.(string)
  565. }
  566. hPath := arg["path"].(string)
  567. markdown := arg["markdown"].(string)
  568. baseName := path.Base(hPath)
  569. dir := path.Dir(hPath)
  570. r, _ := regexp.Compile("\r\n|\r|\n|\u2028|\u2029|\t|/")
  571. baseName = r.ReplaceAllString(baseName, "")
  572. if 512 < utf8.RuneCountInString(baseName) {
  573. baseName = gulu.Str.SubStr(baseName, 512)
  574. }
  575. hPath = path.Join(dir, baseName)
  576. if !strings.HasPrefix(hPath, "/") {
  577. hPath = "/" + hPath
  578. }
  579. id, err := model.CreateWithMarkdown(notebook, hPath, markdown, parentID, id)
  580. if nil != err {
  581. ret.Code = -1
  582. ret.Msg = err.Error()
  583. return
  584. }
  585. ret.Data = id
  586. model.WaitForWritingFiles()
  587. box := model.Conf.Box(notebook)
  588. b, _ := model.GetBlock(id, nil)
  589. p := b.Path
  590. pushCreate(box, p, id, arg)
  591. }
  592. func getDocCreateSavePath(c *gin.Context) {
  593. ret := gulu.Ret.NewResult()
  594. defer c.JSON(http.StatusOK, ret)
  595. arg, ok := util.JsonArg(c, ret)
  596. if !ok {
  597. return
  598. }
  599. notebook := arg["notebook"].(string)
  600. box := model.Conf.Box(notebook)
  601. docCreateSavePathTpl := model.Conf.FileTree.DocCreateSavePath
  602. if nil != box {
  603. docCreateSavePathTpl = box.GetConf().DocCreateSavePath
  604. }
  605. if "" == docCreateSavePathTpl {
  606. docCreateSavePathTpl = model.Conf.FileTree.DocCreateSavePath
  607. }
  608. docCreateSavePathTpl = strings.TrimSpace(docCreateSavePathTpl)
  609. if "../" == docCreateSavePathTpl {
  610. docCreateSavePathTpl = "../Untitled"
  611. }
  612. if "/" == docCreateSavePathTpl {
  613. docCreateSavePathTpl = "/Untitled"
  614. }
  615. p, err := model.RenderGoTemplate(docCreateSavePathTpl)
  616. if nil != err {
  617. ret.Code = -1
  618. ret.Msg = err.Error()
  619. return
  620. }
  621. ret.Data = map[string]interface{}{
  622. "path": p,
  623. }
  624. }
  625. func getRefCreateSavePath(c *gin.Context) {
  626. ret := gulu.Ret.NewResult()
  627. defer c.JSON(http.StatusOK, ret)
  628. arg, ok := util.JsonArg(c, ret)
  629. if !ok {
  630. return
  631. }
  632. notebook := arg["notebook"].(string)
  633. box := model.Conf.Box(notebook)
  634. refCreateSavePath := model.Conf.FileTree.RefCreateSavePath
  635. if nil != box {
  636. refCreateSavePath = box.GetConf().RefCreateSavePath
  637. }
  638. if "" == refCreateSavePath {
  639. refCreateSavePath = model.Conf.FileTree.RefCreateSavePath
  640. }
  641. p, err := model.RenderGoTemplate(refCreateSavePath)
  642. if nil != err {
  643. ret.Code = -1
  644. ret.Msg = err.Error()
  645. return
  646. }
  647. ret.Data = map[string]interface{}{
  648. "path": p,
  649. }
  650. }
  651. func changeSort(c *gin.Context) {
  652. ret := gulu.Ret.NewResult()
  653. defer c.JSON(http.StatusOK, ret)
  654. arg, ok := util.JsonArg(c, ret)
  655. if !ok {
  656. return
  657. }
  658. notebook := arg["notebook"].(string)
  659. pathsArg := arg["paths"].([]interface{})
  660. var paths []string
  661. for _, p := range pathsArg {
  662. paths = append(paths, p.(string))
  663. }
  664. model.ChangeFileTreeSort(notebook, paths)
  665. }
  666. func searchDocs(c *gin.Context) {
  667. ret := gulu.Ret.NewResult()
  668. defer c.JSON(http.StatusOK, ret)
  669. arg, ok := util.JsonArg(c, ret)
  670. if !ok {
  671. return
  672. }
  673. flashcard := false
  674. if arg["flashcard"] != nil {
  675. flashcard = arg["flashcard"].(bool)
  676. }
  677. k := arg["k"].(string)
  678. ret.Data = model.SearchDocsByKeyword(k, flashcard)
  679. }
  680. func listDocsByPath(c *gin.Context) {
  681. ret := gulu.Ret.NewResult()
  682. defer c.JSON(http.StatusOK, ret)
  683. arg, ok := util.JsonArg(c, ret)
  684. if !ok {
  685. return
  686. }
  687. notebook := arg["notebook"].(string)
  688. p := arg["path"].(string)
  689. sortParam := arg["sort"]
  690. sortMode := util.SortModeUnassigned
  691. if nil != sortParam {
  692. sortMode = int(sortParam.(float64))
  693. }
  694. flashcard := false
  695. if arg["flashcard"] != nil {
  696. flashcard = arg["flashcard"].(bool)
  697. }
  698. maxListCount := model.Conf.FileTree.MaxListCount
  699. if arg["maxListCount"] != nil {
  700. // API `listDocsByPath` add an optional parameter `maxListCount` https://github.com/siyuan-note/siyuan/issues/7993
  701. maxListCount = int(arg["maxListCount"].(float64))
  702. if 0 >= maxListCount {
  703. maxListCount = math.MaxInt
  704. }
  705. }
  706. showHidden := false
  707. if arg["showHidden"] != nil {
  708. showHidden = arg["showHidden"].(bool)
  709. }
  710. files, totals, err := model.ListDocTree(notebook, p, sortMode, flashcard, showHidden, maxListCount)
  711. if nil != err {
  712. ret.Code = -1
  713. ret.Msg = err.Error()
  714. return
  715. }
  716. if maxListCount < totals {
  717. // API `listDocsByPath` add an optional parameter `ignoreMaxListHint` https://github.com/siyuan-note/siyuan/issues/10290
  718. ignoreMaxListHintArg := arg["ignoreMaxListHint"]
  719. if nil == ignoreMaxListHintArg || !ignoreMaxListHintArg.(bool) {
  720. util.PushMsg(fmt.Sprintf(model.Conf.Language(48), len(files)), 7000)
  721. }
  722. }
  723. ret.Data = map[string]interface{}{
  724. "box": notebook,
  725. "path": p,
  726. "files": files,
  727. }
  728. }
  729. func getDoc(c *gin.Context) {
  730. ret := gulu.Ret.NewResult()
  731. defer c.JSON(http.StatusOK, ret)
  732. arg, ok := util.JsonArg(c, ret)
  733. if !ok {
  734. return
  735. }
  736. id := arg["id"].(string)
  737. idx := arg["index"]
  738. index := 0
  739. if nil != idx {
  740. index = int(idx.(float64))
  741. }
  742. var query string
  743. if queryArg := arg["query"]; nil != queryArg {
  744. query = queryArg.(string)
  745. }
  746. var queryMethod int
  747. if queryMethodArg := arg["queryMethod"]; nil != queryMethodArg {
  748. queryMethod = int(queryMethodArg.(float64))
  749. }
  750. var queryTypes map[string]bool
  751. if queryTypesArg := arg["queryTypes"]; nil != queryTypesArg {
  752. typesArg := queryTypesArg.(map[string]interface{})
  753. queryTypes = map[string]bool{}
  754. for t, b := range typesArg {
  755. queryTypes[t] = b.(bool)
  756. }
  757. }
  758. m := arg["mode"] // 0: 仅当前 ID,1:向上 2:向下,3:上下都加载,4:加载末尾
  759. mode := 0
  760. if nil != m {
  761. mode = int(m.(float64))
  762. }
  763. s := arg["size"]
  764. size := 102400 // 默认最大加载块数
  765. if nil != s {
  766. size = int(s.(float64))
  767. }
  768. startID := ""
  769. endID := ""
  770. startIDArg := arg["startID"]
  771. endIDArg := arg["endID"]
  772. if nil != startIDArg && nil != endIDArg {
  773. startID = startIDArg.(string)
  774. endID = endIDArg.(string)
  775. size = model.Conf.Editor.DynamicLoadBlocks
  776. }
  777. isBacklinkArg := arg["isBacklink"]
  778. isBacklink := false
  779. if nil != isBacklinkArg {
  780. isBacklink = isBacklinkArg.(bool)
  781. }
  782. blockCount, content, parentID, parent2ID, rootID, typ, eof, scroll, boxID, docPath, isBacklinkExpand, err := model.GetDoc(startID, endID, id, index, query, queryTypes, queryMethod, mode, size, isBacklink)
  783. if model.ErrBlockNotFound == err {
  784. ret.Code = 3
  785. return
  786. }
  787. if nil != err {
  788. ret.Code = 1
  789. ret.Msg = err.Error()
  790. return
  791. }
  792. // 判断是否正在同步中 https://github.com/siyuan-note/siyuan/issues/6290
  793. isSyncing := model.IsSyncingFile(rootID)
  794. ret.Data = map[string]interface{}{
  795. "id": id,
  796. "mode": mode,
  797. "parentID": parentID,
  798. "parent2ID": parent2ID,
  799. "rootID": rootID,
  800. "type": typ,
  801. "content": content,
  802. "blockCount": blockCount,
  803. "eof": eof,
  804. "scroll": scroll,
  805. "box": boxID,
  806. "path": docPath,
  807. "isSyncing": isSyncing,
  808. "isBacklinkExpand": isBacklinkExpand,
  809. }
  810. }
  811. func pushCreate(box *model.Box, p, treeID string, arg map[string]interface{}) {
  812. evt := util.NewCmdResult("create", 0, util.PushModeBroadcast)
  813. name := path.Base(p)
  814. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  815. evt.Data = map[string]interface{}{
  816. "box": box,
  817. "path": p,
  818. "files": files,
  819. "name": name,
  820. "id": treeID,
  821. }
  822. evt.Callback = arg["callback"]
  823. util.PushEvent(evt)
  824. }