filetree.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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 err != nil {
  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); err != nil {
  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 err != nil {
  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); err != nil {
  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 err != nil {
  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 err != nil {
  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 err != nil {
  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 err != nil {
  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 err != nil {
  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 err != nil {
  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 err != nil {
  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 err != nil {
  307. ret.Code = -1
  308. ret.Msg = err.Error()
  309. return
  310. }
  311. ret.Data = hPath
  312. }
  313. func getPathByID(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. id := arg["id"].(string)
  321. if util.InvalidIDPattern(id, ret) {
  322. return
  323. }
  324. _path, err := model.GetPathByID(id)
  325. if err != nil {
  326. ret.Code = -1
  327. ret.Msg = err.Error()
  328. return
  329. }
  330. ret.Data = _path
  331. }
  332. func getFullHPathByID(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["id"] {
  340. return
  341. }
  342. id := arg["id"].(string)
  343. hPath, err := model.GetFullHPathByID(id)
  344. if err != nil {
  345. ret.Code = -1
  346. ret.Msg = err.Error()
  347. return
  348. }
  349. ret.Data = hPath
  350. }
  351. func getIDsByHPath(c *gin.Context) {
  352. ret := gulu.Ret.NewResult()
  353. defer c.JSON(http.StatusOK, ret)
  354. arg, ok := util.JsonArg(c, ret)
  355. if !ok {
  356. return
  357. }
  358. if nil == arg["path"] {
  359. return
  360. }
  361. if nil == arg["notebook"] {
  362. return
  363. }
  364. notebook := arg["notebook"].(string)
  365. if util.InvalidIDPattern(notebook, ret) {
  366. return
  367. }
  368. p := arg["path"].(string)
  369. ids, err := model.GetIDsByHPath(p, notebook)
  370. if err != nil {
  371. ret.Code = -1
  372. ret.Msg = err.Error()
  373. return
  374. }
  375. ret.Data = ids
  376. }
  377. func moveDocs(c *gin.Context) {
  378. ret := gulu.Ret.NewResult()
  379. defer c.JSON(http.StatusOK, ret)
  380. arg, ok := util.JsonArg(c, ret)
  381. if !ok {
  382. return
  383. }
  384. var fromPaths []string
  385. fromPathsArg := arg["fromPaths"].([]interface{})
  386. for _, fromPath := range fromPathsArg {
  387. fromPaths = append(fromPaths, fromPath.(string))
  388. }
  389. toPath := arg["toPath"].(string)
  390. toNotebook := arg["toNotebook"].(string)
  391. if util.InvalidIDPattern(toNotebook, ret) {
  392. return
  393. }
  394. callback := arg["callback"]
  395. err := model.MoveDocs(fromPaths, toNotebook, toPath, callback)
  396. if err != nil {
  397. ret.Code = -1
  398. ret.Msg = err.Error()
  399. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  400. return
  401. }
  402. }
  403. func removeDoc(c *gin.Context) {
  404. ret := gulu.Ret.NewResult()
  405. defer c.JSON(http.StatusOK, ret)
  406. arg, ok := util.JsonArg(c, ret)
  407. if !ok {
  408. return
  409. }
  410. notebook := arg["notebook"].(string)
  411. if util.InvalidIDPattern(notebook, ret) {
  412. return
  413. }
  414. p := arg["path"].(string)
  415. model.RemoveDoc(notebook, p)
  416. }
  417. func removeDocs(c *gin.Context) {
  418. ret := gulu.Ret.NewResult()
  419. defer c.JSON(http.StatusOK, ret)
  420. arg, ok := util.JsonArg(c, ret)
  421. if !ok {
  422. return
  423. }
  424. pathsArg := arg["paths"].([]interface{})
  425. var paths []string
  426. for _, path := range pathsArg {
  427. paths = append(paths, path.(string))
  428. }
  429. model.RemoveDocs(paths)
  430. }
  431. func renameDoc(c *gin.Context) {
  432. ret := gulu.Ret.NewResult()
  433. defer c.JSON(http.StatusOK, ret)
  434. arg, ok := util.JsonArg(c, ret)
  435. if !ok {
  436. return
  437. }
  438. notebook := arg["notebook"].(string)
  439. if util.InvalidIDPattern(notebook, ret) {
  440. return
  441. }
  442. p := arg["path"].(string)
  443. title := arg["title"].(string)
  444. err := model.RenameDoc(notebook, p, title)
  445. if err != nil {
  446. ret.Code = -1
  447. ret.Msg = err.Error()
  448. return
  449. }
  450. return
  451. }
  452. func duplicateDoc(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. id := arg["id"].(string)
  460. tree, err := model.LoadTreeByBlockID(id)
  461. if err != nil {
  462. ret.Code = -1
  463. ret.Msg = err.Error()
  464. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  465. return
  466. }
  467. notebook := tree.Box
  468. box := model.Conf.Box(notebook)
  469. model.DuplicateDoc(tree)
  470. pushCreate(box, tree.Path, tree.ID, arg)
  471. ret.Data = map[string]interface{}{
  472. "id": tree.Root.ID,
  473. "notebook": notebook,
  474. "path": tree.Path,
  475. "hPath": tree.HPath,
  476. }
  477. }
  478. func createDoc(c *gin.Context) {
  479. ret := gulu.Ret.NewResult()
  480. defer c.JSON(http.StatusOK, ret)
  481. arg, ok := util.JsonArg(c, ret)
  482. if !ok {
  483. return
  484. }
  485. notebook := arg["notebook"].(string)
  486. p := arg["path"].(string)
  487. title := arg["title"].(string)
  488. md := arg["md"].(string)
  489. sortsArg := arg["sorts"]
  490. var sorts []string
  491. if nil != sortsArg {
  492. for _, sort := range sortsArg.([]interface{}) {
  493. sorts = append(sorts, sort.(string))
  494. }
  495. }
  496. tree, err := model.CreateDocByMd(notebook, p, title, md, sorts)
  497. if err != nil {
  498. ret.Code = -1
  499. ret.Msg = err.Error()
  500. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  501. return
  502. }
  503. model.WaitForWritingFiles()
  504. box := model.Conf.Box(notebook)
  505. pushCreate(box, p, tree.Root.ID, arg)
  506. ret.Data = map[string]interface{}{
  507. "id": tree.Root.ID,
  508. }
  509. }
  510. func createDailyNote(c *gin.Context) {
  511. ret := gulu.Ret.NewResult()
  512. defer c.JSON(http.StatusOK, ret)
  513. arg, ok := util.JsonArg(c, ret)
  514. if !ok {
  515. return
  516. }
  517. notebook := arg["notebook"].(string)
  518. p, existed, err := model.CreateDailyNote(notebook)
  519. if err != nil {
  520. if model.ErrBoxNotFound == err {
  521. ret.Code = 1
  522. } else {
  523. ret.Code = -1
  524. }
  525. ret.Msg = err.Error()
  526. return
  527. }
  528. model.WaitForWritingFiles()
  529. box := model.Conf.Box(notebook)
  530. luteEngine := util.NewLute()
  531. tree, err := filesys.LoadTree(box.ID, p, luteEngine)
  532. if err != nil {
  533. ret.Code = -1
  534. ret.Msg = err.Error()
  535. return
  536. }
  537. if !existed {
  538. // 只有创建的情况才推送,已经存在的情况不推送
  539. // Creating a dailynote existed no longer expands the doc tree https://github.com/siyuan-note/siyuan/issues/9959
  540. appArg := arg["app"]
  541. app := ""
  542. if nil != appArg {
  543. app = appArg.(string)
  544. }
  545. evt := util.NewCmdResult("createdailynote", 0, util.PushModeBroadcast)
  546. evt.AppId = app
  547. name := path.Base(p)
  548. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  549. evt.Data = map[string]interface{}{
  550. "box": box,
  551. "path": p,
  552. "files": files,
  553. "name": name,
  554. "id": tree.Root.ID,
  555. }
  556. evt.Callback = arg["callback"]
  557. util.PushEvent(evt)
  558. }
  559. ret.Data = map[string]interface{}{
  560. "id": tree.Root.ID,
  561. }
  562. }
  563. func createDocWithMd(c *gin.Context) {
  564. ret := gulu.Ret.NewResult()
  565. defer c.JSON(http.StatusOK, ret)
  566. arg, ok := util.JsonArg(c, ret)
  567. if !ok {
  568. return
  569. }
  570. notebook := arg["notebook"].(string)
  571. if util.InvalidIDPattern(notebook, ret) {
  572. return
  573. }
  574. tagsArg := arg["tags"]
  575. var tags string
  576. if nil != tagsArg {
  577. tags = tagsArg.(string)
  578. }
  579. var parentID string
  580. parentIDArg := arg["parentID"]
  581. if nil != parentIDArg {
  582. parentID = parentIDArg.(string)
  583. }
  584. id := ast.NewNodeID()
  585. idArg := arg["id"]
  586. if nil != idArg {
  587. id = idArg.(string)
  588. }
  589. hPath := arg["path"].(string)
  590. markdown := arg["markdown"].(string)
  591. baseName := path.Base(hPath)
  592. dir := path.Dir(hPath)
  593. r, _ := regexp.Compile("\r\n|\r|\n|\u2028|\u2029|\t|/")
  594. baseName = r.ReplaceAllString(baseName, "")
  595. if 512 < utf8.RuneCountInString(baseName) {
  596. baseName = gulu.Str.SubStr(baseName, 512)
  597. }
  598. hPath = path.Join(dir, baseName)
  599. if !strings.HasPrefix(hPath, "/") {
  600. hPath = "/" + hPath
  601. }
  602. withMath := false
  603. withMathArg := arg["withMath"]
  604. if nil != withMathArg {
  605. withMath = withMathArg.(bool)
  606. }
  607. id, err := model.CreateWithMarkdown(tags, notebook, hPath, markdown, parentID, id, withMath)
  608. if err != nil {
  609. ret.Code = -1
  610. ret.Msg = err.Error()
  611. return
  612. }
  613. ret.Data = id
  614. model.WaitForWritingFiles()
  615. box := model.Conf.Box(notebook)
  616. b, _ := model.GetBlock(id, nil)
  617. p := b.Path
  618. pushCreate(box, p, id, arg)
  619. }
  620. func getDocCreateSavePath(c *gin.Context) {
  621. ret := gulu.Ret.NewResult()
  622. defer c.JSON(http.StatusOK, ret)
  623. arg, ok := util.JsonArg(c, ret)
  624. if !ok {
  625. return
  626. }
  627. notebook := arg["notebook"].(string)
  628. box := model.Conf.Box(notebook)
  629. var docCreateSaveBox string
  630. docCreateSavePathTpl := model.Conf.FileTree.DocCreateSavePath
  631. if nil != box {
  632. boxConf := box.GetConf()
  633. docCreateSaveBox = boxConf.DocCreateSaveBox
  634. docCreateSavePathTpl = boxConf.DocCreateSavePath
  635. }
  636. if "" == docCreateSaveBox && "" == docCreateSavePathTpl {
  637. docCreateSaveBox = model.Conf.FileTree.DocCreateSaveBox
  638. }
  639. if "" != docCreateSaveBox {
  640. if nil == model.Conf.Box(docCreateSaveBox) {
  641. // 如果配置的笔记本未打开或者不存在,则使用当前笔记本
  642. docCreateSaveBox = notebook
  643. }
  644. }
  645. if "" == docCreateSaveBox {
  646. docCreateSaveBox = notebook
  647. }
  648. if "" == docCreateSavePathTpl {
  649. docCreateSavePathTpl = model.Conf.FileTree.DocCreateSavePath
  650. }
  651. docCreateSavePathTpl = strings.TrimSpace(docCreateSavePathTpl)
  652. if docCreateSaveBox != notebook {
  653. if "" != docCreateSavePathTpl && !strings.HasPrefix(docCreateSavePathTpl, "/") {
  654. // 如果配置的笔记本不是当前笔记本,则将相对路径转换为绝对路径
  655. docCreateSavePathTpl = "/" + docCreateSavePathTpl
  656. }
  657. }
  658. docCreateSavePath, err := model.RenderGoTemplate(docCreateSavePathTpl)
  659. if err != nil {
  660. ret.Code = -1
  661. ret.Msg = err.Error()
  662. return
  663. }
  664. ret.Data = map[string]interface{}{
  665. "box": docCreateSaveBox,
  666. "path": docCreateSavePath,
  667. }
  668. }
  669. func getRefCreateSavePath(c *gin.Context) {
  670. ret := gulu.Ret.NewResult()
  671. defer c.JSON(http.StatusOK, ret)
  672. arg, ok := util.JsonArg(c, ret)
  673. if !ok {
  674. return
  675. }
  676. notebook := arg["notebook"].(string)
  677. box := model.Conf.Box(notebook)
  678. var refCreateSaveBox string
  679. refCreateSavePathTpl := model.Conf.FileTree.RefCreateSavePath
  680. if nil != box {
  681. boxConf := box.GetConf()
  682. refCreateSaveBox = boxConf.RefCreateSaveBox
  683. refCreateSavePathTpl = boxConf.RefCreateSavePath
  684. }
  685. if "" == refCreateSaveBox && "" == refCreateSavePathTpl {
  686. refCreateSaveBox = model.Conf.FileTree.RefCreateSaveBox
  687. }
  688. if "" != refCreateSaveBox {
  689. if nil == model.Conf.Box(refCreateSaveBox) {
  690. // 如果配置的笔记本未打开或者不存在,则使用当前笔记本
  691. refCreateSaveBox = notebook
  692. }
  693. }
  694. if "" == refCreateSaveBox {
  695. refCreateSaveBox = notebook
  696. }
  697. if "" == refCreateSavePathTpl {
  698. refCreateSavePathTpl = model.Conf.FileTree.RefCreateSavePath
  699. }
  700. if refCreateSaveBox != notebook {
  701. if "" != refCreateSavePathTpl && !strings.HasPrefix(refCreateSavePathTpl, "/") {
  702. // 如果配置的笔记本不是当前笔记本,则将相对路径转换为绝对路径
  703. refCreateSavePathTpl = "/" + refCreateSavePathTpl
  704. }
  705. }
  706. refCreateSavePath, err := model.RenderGoTemplate(refCreateSavePathTpl)
  707. if err != nil {
  708. ret.Code = -1
  709. ret.Msg = err.Error()
  710. return
  711. }
  712. ret.Data = map[string]interface{}{
  713. "box": refCreateSaveBox,
  714. "path": refCreateSavePath,
  715. }
  716. }
  717. func changeSort(c *gin.Context) {
  718. ret := gulu.Ret.NewResult()
  719. defer c.JSON(http.StatusOK, ret)
  720. arg, ok := util.JsonArg(c, ret)
  721. if !ok {
  722. return
  723. }
  724. notebook := arg["notebook"].(string)
  725. pathsArg := arg["paths"].([]interface{})
  726. var paths []string
  727. for _, p := range pathsArg {
  728. paths = append(paths, p.(string))
  729. }
  730. model.ChangeFileTreeSort(notebook, paths)
  731. }
  732. func searchDocs(c *gin.Context) {
  733. ret := gulu.Ret.NewResult()
  734. defer c.JSON(http.StatusOK, ret)
  735. arg, ok := util.JsonArg(c, ret)
  736. if !ok {
  737. return
  738. }
  739. flashcard := false
  740. if arg["flashcard"] != nil {
  741. flashcard = arg["flashcard"].(bool)
  742. }
  743. k := arg["k"].(string)
  744. ret.Data = model.SearchDocsByKeyword(k, flashcard)
  745. }
  746. func listDocsByPath(c *gin.Context) {
  747. ret := gulu.Ret.NewResult()
  748. defer c.JSON(http.StatusOK, ret)
  749. arg, ok := util.JsonArg(c, ret)
  750. if !ok {
  751. return
  752. }
  753. notebook := arg["notebook"].(string)
  754. p := arg["path"].(string)
  755. sortParam := arg["sort"]
  756. sortMode := util.SortModeUnassigned
  757. if nil != sortParam {
  758. sortMode = int(sortParam.(float64))
  759. }
  760. flashcard := false
  761. if arg["flashcard"] != nil {
  762. flashcard = arg["flashcard"].(bool)
  763. }
  764. maxListCount := model.Conf.FileTree.MaxListCount
  765. if arg["maxListCount"] != nil {
  766. // API `listDocsByPath` add an optional parameter `maxListCount` https://github.com/siyuan-note/siyuan/issues/7993
  767. maxListCount = int(arg["maxListCount"].(float64))
  768. if 0 >= maxListCount {
  769. maxListCount = math.MaxInt
  770. }
  771. }
  772. showHidden := false
  773. if arg["showHidden"] != nil {
  774. showHidden = arg["showHidden"].(bool)
  775. }
  776. files, totals, err := model.ListDocTree(notebook, p, sortMode, flashcard, showHidden, maxListCount)
  777. if err != nil {
  778. ret.Code = -1
  779. ret.Msg = err.Error()
  780. return
  781. }
  782. if maxListCount < totals {
  783. // API `listDocsByPath` add an optional parameter `ignoreMaxListHint` https://github.com/siyuan-note/siyuan/issues/10290
  784. ignoreMaxListHintArg := arg["ignoreMaxListHint"]
  785. if nil == ignoreMaxListHintArg || !ignoreMaxListHintArg.(bool) {
  786. util.PushMsg(fmt.Sprintf(model.Conf.Language(48), len(files)), 7000)
  787. }
  788. }
  789. ret.Data = map[string]interface{}{
  790. "box": notebook,
  791. "path": p,
  792. "files": files,
  793. }
  794. }
  795. func getDoc(c *gin.Context) {
  796. ret := gulu.Ret.NewResult()
  797. defer c.JSON(http.StatusOK, ret)
  798. arg, ok := util.JsonArg(c, ret)
  799. if !ok {
  800. return
  801. }
  802. id := arg["id"].(string)
  803. idx := arg["index"]
  804. index := 0
  805. if nil != idx {
  806. index = int(idx.(float64))
  807. }
  808. var query string
  809. if queryArg := arg["query"]; nil != queryArg {
  810. query = queryArg.(string)
  811. }
  812. var queryMethod int
  813. if queryMethodArg := arg["queryMethod"]; nil != queryMethodArg {
  814. queryMethod = int(queryMethodArg.(float64))
  815. }
  816. var queryTypes map[string]bool
  817. if queryTypesArg := arg["queryTypes"]; nil != queryTypesArg {
  818. typesArg := queryTypesArg.(map[string]interface{})
  819. queryTypes = map[string]bool{}
  820. for t, b := range typesArg {
  821. queryTypes[t] = b.(bool)
  822. }
  823. }
  824. m := arg["mode"] // 0: 仅当前 ID,1:向上 2:向下,3:上下都加载,4:加载末尾
  825. mode := 0
  826. if nil != m {
  827. mode = int(m.(float64))
  828. }
  829. s := arg["size"]
  830. size := 102400 // 默认最大加载块数
  831. if nil != s {
  832. size = int(s.(float64))
  833. }
  834. startID := ""
  835. endID := ""
  836. startIDArg := arg["startID"]
  837. endIDArg := arg["endID"]
  838. if nil != startIDArg && nil != endIDArg {
  839. startID = startIDArg.(string)
  840. endID = endIDArg.(string)
  841. size = model.Conf.Editor.DynamicLoadBlocks
  842. }
  843. isBacklinkArg := arg["isBacklink"]
  844. isBacklink := false
  845. if nil != isBacklinkArg {
  846. isBacklink = isBacklinkArg.(bool)
  847. }
  848. blockCount, content, parentID, parent2ID, rootID, typ, eof, scroll, boxID, docPath, isBacklinkExpand, err := model.GetDoc(startID, endID, id, index, query, queryTypes, queryMethod, mode, size, isBacklink)
  849. if model.ErrBlockNotFound == err {
  850. ret.Code = 3
  851. return
  852. }
  853. if err != nil {
  854. ret.Code = 1
  855. ret.Msg = err.Error()
  856. return
  857. }
  858. // 判断是否正在同步中 https://github.com/siyuan-note/siyuan/issues/6290
  859. isSyncing := model.IsSyncingFile(rootID)
  860. ret.Data = map[string]interface{}{
  861. "id": id,
  862. "mode": mode,
  863. "parentID": parentID,
  864. "parent2ID": parent2ID,
  865. "rootID": rootID,
  866. "type": typ,
  867. "content": content,
  868. "blockCount": blockCount,
  869. "eof": eof,
  870. "scroll": scroll,
  871. "box": boxID,
  872. "path": docPath,
  873. "isSyncing": isSyncing,
  874. "isBacklinkExpand": isBacklinkExpand,
  875. }
  876. }
  877. func pushCreate(box *model.Box, p, treeID string, arg map[string]interface{}) {
  878. evt := util.NewCmdResult("create", 0, util.PushModeBroadcast)
  879. name := path.Base(p)
  880. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  881. evt.Data = map[string]interface{}{
  882. "box": box,
  883. "path": p,
  884. "files": files,
  885. "name": name,
  886. "id": treeID,
  887. }
  888. evt.Callback = arg["callback"]
  889. util.PushEvent(evt)
  890. }