filetree.go 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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. var targetPath string
  184. if arg["targetPath"] != nil {
  185. targetPath = arg["targetPath"].(string)
  186. }
  187. var previousPath string
  188. if arg["previousPath"] != nil {
  189. previousPath = arg["previousPath"].(string)
  190. }
  191. srcRootBlockID, targetPath, err := model.Heading2Doc(srcHeadingID, targetNotebook, targetPath, previousPath)
  192. if err != nil {
  193. ret.Code = -1
  194. ret.Msg = err.Error()
  195. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  196. return
  197. }
  198. model.FlushTxQueue()
  199. luteEngine := util.NewLute()
  200. tree, err := filesys.LoadTree(targetNotebook, targetPath, luteEngine)
  201. if err != nil {
  202. ret.Code = -1
  203. ret.Msg = err.Error()
  204. return
  205. }
  206. name := path.Base(targetPath)
  207. box := model.Conf.Box(targetNotebook)
  208. files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  209. evt := util.NewCmdResult("heading2doc", 0, util.PushModeBroadcast)
  210. evt.Data = map[string]interface{}{
  211. "box": box,
  212. "path": targetPath,
  213. "files": files,
  214. "name": name,
  215. "id": tree.Root.ID,
  216. "srcRootBlockID": srcRootBlockID,
  217. }
  218. evt.Callback = arg["callback"]
  219. util.PushEvent(evt)
  220. }
  221. func li2Doc(c *gin.Context) {
  222. ret := gulu.Ret.NewResult()
  223. defer c.JSON(http.StatusOK, ret)
  224. arg, ok := util.JsonArg(c, ret)
  225. if !ok {
  226. return
  227. }
  228. srcListItemID := arg["srcListItemID"].(string)
  229. targetNotebook := arg["targetNoteBook"].(string)
  230. var targetPath string
  231. if arg["targetPath"] != nil {
  232. targetPath = arg["targetPath"].(string)
  233. }
  234. var previousPath string
  235. if arg["previousPath"] != nil {
  236. previousPath = arg["previousPath"].(string)
  237. }
  238. srcRootBlockID, targetPath, err := model.ListItem2Doc(srcListItemID, targetNotebook, targetPath, previousPath)
  239. if err != nil {
  240. ret.Code = -1
  241. ret.Msg = err.Error()
  242. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  243. return
  244. }
  245. model.FlushTxQueue()
  246. luteEngine := util.NewLute()
  247. tree, err := filesys.LoadTree(targetNotebook, targetPath, luteEngine)
  248. if err != nil {
  249. ret.Code = -1
  250. ret.Msg = err.Error()
  251. return
  252. }
  253. name := path.Base(targetPath)
  254. box := model.Conf.Box(targetNotebook)
  255. files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  256. evt := util.NewCmdResult("li2doc", 0, util.PushModeBroadcast)
  257. evt.Data = map[string]interface{}{
  258. "box": box,
  259. "path": targetPath,
  260. "files": files,
  261. "name": name,
  262. "id": tree.Root.ID,
  263. "srcRootBlockID": srcRootBlockID,
  264. }
  265. evt.Callback = arg["callback"]
  266. util.PushEvent(evt)
  267. }
  268. func getHPathByPath(c *gin.Context) {
  269. ret := gulu.Ret.NewResult()
  270. defer c.JSON(http.StatusOK, ret)
  271. arg, ok := util.JsonArg(c, ret)
  272. if !ok {
  273. return
  274. }
  275. notebook := arg["notebook"].(string)
  276. if util.InvalidIDPattern(notebook, ret) {
  277. return
  278. }
  279. p := arg["path"].(string)
  280. hPath, err := model.GetHPathByPath(notebook, p)
  281. if err != nil {
  282. ret.Code = -1
  283. ret.Msg = err.Error()
  284. return
  285. }
  286. ret.Data = hPath
  287. }
  288. func getHPathsByPaths(c *gin.Context) {
  289. ret := gulu.Ret.NewResult()
  290. defer c.JSON(http.StatusOK, ret)
  291. arg, ok := util.JsonArg(c, ret)
  292. if !ok {
  293. return
  294. }
  295. pathsArg := arg["paths"].([]interface{})
  296. var paths []string
  297. for _, p := range pathsArg {
  298. paths = append(paths, p.(string))
  299. }
  300. hPath, err := model.GetHPathsByPaths(paths)
  301. if err != nil {
  302. ret.Code = -1
  303. ret.Msg = err.Error()
  304. return
  305. }
  306. ret.Data = hPath
  307. }
  308. func getHPathByID(c *gin.Context) {
  309. ret := gulu.Ret.NewResult()
  310. defer c.JSON(http.StatusOK, ret)
  311. arg, ok := util.JsonArg(c, ret)
  312. if !ok {
  313. return
  314. }
  315. id := arg["id"].(string)
  316. if util.InvalidIDPattern(id, ret) {
  317. return
  318. }
  319. hPath, err := model.GetHPathByID(id)
  320. if err != nil {
  321. ret.Code = -1
  322. ret.Msg = err.Error()
  323. return
  324. }
  325. ret.Data = hPath
  326. }
  327. func getPathByID(c *gin.Context) {
  328. ret := gulu.Ret.NewResult()
  329. defer c.JSON(http.StatusOK, ret)
  330. arg, ok := util.JsonArg(c, ret)
  331. if !ok {
  332. return
  333. }
  334. id := arg["id"].(string)
  335. if util.InvalidIDPattern(id, ret) {
  336. return
  337. }
  338. _path, err := model.GetPathByID(id)
  339. if err != nil {
  340. ret.Code = -1
  341. ret.Msg = err.Error()
  342. return
  343. }
  344. ret.Data = _path
  345. }
  346. func getFullHPathByID(c *gin.Context) {
  347. ret := gulu.Ret.NewResult()
  348. defer c.JSON(http.StatusOK, ret)
  349. arg, ok := util.JsonArg(c, ret)
  350. if !ok {
  351. return
  352. }
  353. if nil == arg["id"] {
  354. return
  355. }
  356. id := arg["id"].(string)
  357. hPath, err := model.GetFullHPathByID(id)
  358. if err != nil {
  359. ret.Code = -1
  360. ret.Msg = err.Error()
  361. return
  362. }
  363. ret.Data = hPath
  364. }
  365. func getIDsByHPath(c *gin.Context) {
  366. ret := gulu.Ret.NewResult()
  367. defer c.JSON(http.StatusOK, ret)
  368. arg, ok := util.JsonArg(c, ret)
  369. if !ok {
  370. return
  371. }
  372. if nil == arg["path"] {
  373. return
  374. }
  375. if nil == arg["notebook"] {
  376. return
  377. }
  378. notebook := arg["notebook"].(string)
  379. if util.InvalidIDPattern(notebook, ret) {
  380. return
  381. }
  382. p := arg["path"].(string)
  383. ids, err := model.GetIDsByHPath(p, notebook)
  384. if err != nil {
  385. ret.Code = -1
  386. ret.Msg = err.Error()
  387. return
  388. }
  389. ret.Data = ids
  390. }
  391. func moveDocs(c *gin.Context) {
  392. ret := gulu.Ret.NewResult()
  393. defer c.JSON(http.StatusOK, ret)
  394. arg, ok := util.JsonArg(c, ret)
  395. if !ok {
  396. return
  397. }
  398. var fromPaths []string
  399. fromPathsArg := arg["fromPaths"].([]interface{})
  400. for _, fromPath := range fromPathsArg {
  401. fromPaths = append(fromPaths, fromPath.(string))
  402. }
  403. toPath := arg["toPath"].(string)
  404. toNotebook := arg["toNotebook"].(string)
  405. if util.InvalidIDPattern(toNotebook, ret) {
  406. return
  407. }
  408. callback := arg["callback"]
  409. err := model.MoveDocs(fromPaths, toNotebook, toPath, callback)
  410. if err != nil {
  411. ret.Code = -1
  412. ret.Msg = err.Error()
  413. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  414. return
  415. }
  416. }
  417. func moveDocsByID(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. fromIDsArg := arg["fromIDs"].([]any)
  425. var fromIDs []string
  426. for _, fromIDArg := range fromIDsArg {
  427. fromID := fromIDArg.(string)
  428. if util.InvalidIDPattern(fromID, ret) {
  429. return
  430. }
  431. fromIDs = append(fromIDs, fromID)
  432. }
  433. toID := arg["toID"].(string)
  434. if util.InvalidIDPattern(toID, ret) {
  435. return
  436. }
  437. var fromPaths []string
  438. for _, fromID := range fromIDs {
  439. tree, err := model.LoadTreeByBlockID(fromID)
  440. if err != nil {
  441. ret.Code = -1
  442. ret.Msg = err.Error()
  443. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  444. return
  445. }
  446. fromPaths = append(fromPaths, tree.Path)
  447. }
  448. fromPaths = gulu.Str.RemoveDuplicatedElem(fromPaths)
  449. toTree, err := model.LoadTreeByBlockID(toID)
  450. if err != nil {
  451. ret.Code = -1
  452. ret.Msg = err.Error()
  453. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  454. return
  455. }
  456. toNotebook := toTree.Box
  457. toPath := toTree.Path
  458. callback := arg["callback"]
  459. err = model.MoveDocs(fromPaths, toNotebook, toPath, callback)
  460. if err != nil {
  461. ret.Code = -1
  462. ret.Msg = err.Error()
  463. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  464. return
  465. }
  466. }
  467. func removeDoc(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. notebook := arg["notebook"].(string)
  475. if util.InvalidIDPattern(notebook, ret) {
  476. return
  477. }
  478. p := arg["path"].(string)
  479. model.RemoveDoc(notebook, p)
  480. }
  481. func removeDocByID(c *gin.Context) {
  482. ret := gulu.Ret.NewResult()
  483. defer c.JSON(http.StatusOK, ret)
  484. arg, ok := util.JsonArg(c, ret)
  485. if !ok {
  486. return
  487. }
  488. id := arg["id"].(string)
  489. if util.InvalidIDPattern(id, ret) {
  490. return
  491. }
  492. tree, err := model.LoadTreeByBlockID(id)
  493. if err != nil {
  494. ret.Code = -1
  495. ret.Msg = err.Error()
  496. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  497. return
  498. }
  499. model.RemoveDoc(tree.Box, tree.Path)
  500. }
  501. func removeDocs(c *gin.Context) {
  502. ret := gulu.Ret.NewResult()
  503. defer c.JSON(http.StatusOK, ret)
  504. arg, ok := util.JsonArg(c, ret)
  505. if !ok {
  506. return
  507. }
  508. pathsArg := arg["paths"].([]interface{})
  509. var paths []string
  510. for _, path := range pathsArg {
  511. paths = append(paths, path.(string))
  512. }
  513. model.RemoveDocs(paths)
  514. }
  515. func renameDoc(c *gin.Context) {
  516. ret := gulu.Ret.NewResult()
  517. defer c.JSON(http.StatusOK, ret)
  518. arg, ok := util.JsonArg(c, ret)
  519. if !ok {
  520. return
  521. }
  522. notebook := arg["notebook"].(string)
  523. if util.InvalidIDPattern(notebook, ret) {
  524. return
  525. }
  526. p := arg["path"].(string)
  527. title := arg["title"].(string)
  528. err := model.RenameDoc(notebook, p, title)
  529. if err != nil {
  530. ret.Code = -1
  531. ret.Msg = err.Error()
  532. return
  533. }
  534. return
  535. }
  536. func renameDocByID(c *gin.Context) {
  537. ret := gulu.Ret.NewResult()
  538. defer c.JSON(http.StatusOK, ret)
  539. arg, ok := util.JsonArg(c, ret)
  540. if !ok {
  541. return
  542. }
  543. if nil == arg["id"] {
  544. return
  545. }
  546. id := arg["id"].(string)
  547. if util.InvalidIDPattern(id, ret) {
  548. return
  549. }
  550. title := arg["title"].(string)
  551. tree, err := model.LoadTreeByBlockID(id)
  552. if err != nil {
  553. ret.Code = -1
  554. ret.Msg = err.Error()
  555. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  556. return
  557. }
  558. err = model.RenameDoc(tree.Box, tree.Path, title)
  559. if err != nil {
  560. ret.Code = -1
  561. ret.Msg = err.Error()
  562. return
  563. }
  564. }
  565. func duplicateDoc(c *gin.Context) {
  566. ret := gulu.Ret.NewResult()
  567. defer c.JSON(http.StatusOK, ret)
  568. arg, ok := util.JsonArg(c, ret)
  569. if !ok {
  570. return
  571. }
  572. id := arg["id"].(string)
  573. tree, err := model.LoadTreeByBlockID(id)
  574. if err != nil {
  575. ret.Code = -1
  576. ret.Msg = err.Error()
  577. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  578. return
  579. }
  580. notebook := tree.Box
  581. box := model.Conf.Box(notebook)
  582. model.DuplicateDoc(tree)
  583. pushCreate(box, tree.Path, tree.ID, arg)
  584. ret.Data = map[string]interface{}{
  585. "id": tree.Root.ID,
  586. "notebook": notebook,
  587. "path": tree.Path,
  588. "hPath": tree.HPath,
  589. }
  590. }
  591. func createDoc(c *gin.Context) {
  592. ret := gulu.Ret.NewResult()
  593. defer c.JSON(http.StatusOK, ret)
  594. arg, ok := util.JsonArg(c, ret)
  595. if !ok {
  596. return
  597. }
  598. notebook := arg["notebook"].(string)
  599. p := arg["path"].(string)
  600. title := arg["title"].(string)
  601. md := arg["md"].(string)
  602. sortsArg := arg["sorts"]
  603. var sorts []string
  604. if nil != sortsArg {
  605. for _, sort := range sortsArg.([]interface{}) {
  606. sorts = append(sorts, sort.(string))
  607. }
  608. }
  609. tree, err := model.CreateDocByMd(notebook, p, title, md, sorts)
  610. if err != nil {
  611. ret.Code = -1
  612. ret.Msg = err.Error()
  613. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  614. return
  615. }
  616. model.FlushTxQueue()
  617. box := model.Conf.Box(notebook)
  618. pushCreate(box, p, tree.Root.ID, arg)
  619. ret.Data = map[string]interface{}{
  620. "id": tree.Root.ID,
  621. }
  622. }
  623. func createDailyNote(c *gin.Context) {
  624. ret := gulu.Ret.NewResult()
  625. defer c.JSON(http.StatusOK, ret)
  626. arg, ok := util.JsonArg(c, ret)
  627. if !ok {
  628. return
  629. }
  630. notebook := arg["notebook"].(string)
  631. p, existed, err := model.CreateDailyNote(notebook)
  632. if err != nil {
  633. if model.ErrBoxNotFound == err {
  634. ret.Code = 1
  635. } else {
  636. ret.Code = -1
  637. }
  638. ret.Msg = err.Error()
  639. return
  640. }
  641. model.FlushTxQueue()
  642. box := model.Conf.Box(notebook)
  643. luteEngine := util.NewLute()
  644. tree, err := filesys.LoadTree(box.ID, p, luteEngine)
  645. if err != nil {
  646. ret.Code = -1
  647. ret.Msg = err.Error()
  648. return
  649. }
  650. if !existed {
  651. // 只有创建的情况才推送,已经存在的情况不推送
  652. // Creating a dailynote existed no longer expands the doc tree https://github.com/siyuan-note/siyuan/issues/9959
  653. appArg := arg["app"]
  654. app := ""
  655. if nil != appArg {
  656. app = appArg.(string)
  657. }
  658. evt := util.NewCmdResult("createdailynote", 0, util.PushModeBroadcast)
  659. evt.AppId = app
  660. name := path.Base(p)
  661. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  662. evt.Data = map[string]interface{}{
  663. "box": box,
  664. "path": p,
  665. "files": files,
  666. "name": name,
  667. "id": tree.Root.ID,
  668. }
  669. evt.Callback = arg["callback"]
  670. util.PushEvent(evt)
  671. }
  672. ret.Data = map[string]interface{}{
  673. "id": tree.Root.ID,
  674. }
  675. }
  676. func createDocWithMd(c *gin.Context) {
  677. ret := gulu.Ret.NewResult()
  678. defer c.JSON(http.StatusOK, ret)
  679. arg, ok := util.JsonArg(c, ret)
  680. if !ok {
  681. return
  682. }
  683. notebook := arg["notebook"].(string)
  684. if util.InvalidIDPattern(notebook, ret) {
  685. return
  686. }
  687. tagsArg := arg["tags"]
  688. var tags string
  689. if nil != tagsArg {
  690. tags = tagsArg.(string)
  691. }
  692. var parentID string
  693. parentIDArg := arg["parentID"]
  694. if nil != parentIDArg {
  695. parentID = parentIDArg.(string)
  696. }
  697. id := ast.NewNodeID()
  698. idArg := arg["id"]
  699. if nil != idArg {
  700. id = idArg.(string)
  701. }
  702. hPath := arg["path"].(string)
  703. markdown := arg["markdown"].(string)
  704. baseName := path.Base(hPath)
  705. dir := path.Dir(hPath)
  706. r, _ := regexp.Compile("\r\n|\r|\n|\u2028|\u2029|\t|/")
  707. baseName = r.ReplaceAllString(baseName, "")
  708. if 512 < utf8.RuneCountInString(baseName) {
  709. baseName = gulu.Str.SubStr(baseName, 512)
  710. }
  711. hPath = path.Join(dir, baseName)
  712. if !strings.HasPrefix(hPath, "/") {
  713. hPath = "/" + hPath
  714. }
  715. withMath := false
  716. withMathArg := arg["withMath"]
  717. if nil != withMathArg {
  718. withMath = withMathArg.(bool)
  719. }
  720. clippingHref := ""
  721. clippingHrefArg := arg["clippingHref"]
  722. if nil != clippingHrefArg {
  723. clippingHref = clippingHrefArg.(string)
  724. }
  725. id, err := model.CreateWithMarkdown(tags, notebook, hPath, markdown, parentID, id, withMath, clippingHref)
  726. if err != nil {
  727. ret.Code = -1
  728. ret.Msg = err.Error()
  729. return
  730. }
  731. ret.Data = id
  732. model.FlushTxQueue()
  733. box := model.Conf.Box(notebook)
  734. b, _ := model.GetBlock(id, nil)
  735. p := b.Path
  736. pushCreate(box, p, id, arg)
  737. }
  738. func getDocCreateSavePath(c *gin.Context) {
  739. ret := gulu.Ret.NewResult()
  740. defer c.JSON(http.StatusOK, ret)
  741. arg, ok := util.JsonArg(c, ret)
  742. if !ok {
  743. return
  744. }
  745. notebook := arg["notebook"].(string)
  746. box := model.Conf.Box(notebook)
  747. var docCreateSaveBox string
  748. docCreateSavePathTpl := model.Conf.FileTree.DocCreateSavePath
  749. if nil != box {
  750. boxConf := box.GetConf()
  751. docCreateSaveBox = boxConf.DocCreateSaveBox
  752. docCreateSavePathTpl = boxConf.DocCreateSavePath
  753. }
  754. if "" == docCreateSaveBox && "" == docCreateSavePathTpl {
  755. docCreateSaveBox = model.Conf.FileTree.DocCreateSaveBox
  756. }
  757. if "" != docCreateSaveBox {
  758. if nil == model.Conf.Box(docCreateSaveBox) {
  759. // 如果配置的笔记本未打开或者不存在,则使用当前笔记本
  760. docCreateSaveBox = notebook
  761. }
  762. }
  763. if "" == docCreateSaveBox {
  764. docCreateSaveBox = notebook
  765. }
  766. if "" == docCreateSavePathTpl {
  767. docCreateSavePathTpl = model.Conf.FileTree.DocCreateSavePath
  768. }
  769. docCreateSavePathTpl = strings.TrimSpace(docCreateSavePathTpl)
  770. if docCreateSaveBox != notebook {
  771. if "" != docCreateSavePathTpl && !strings.HasPrefix(docCreateSavePathTpl, "/") {
  772. // 如果配置的笔记本不是当前笔记本,则将相对路径转换为绝对路径
  773. docCreateSavePathTpl = "/" + docCreateSavePathTpl
  774. }
  775. }
  776. docCreateSavePath, err := model.RenderGoTemplate(docCreateSavePathTpl)
  777. if err != nil {
  778. ret.Code = -1
  779. ret.Msg = err.Error()
  780. return
  781. }
  782. ret.Data = map[string]interface{}{
  783. "box": docCreateSaveBox,
  784. "path": docCreateSavePath,
  785. }
  786. }
  787. func getRefCreateSavePath(c *gin.Context) {
  788. ret := gulu.Ret.NewResult()
  789. defer c.JSON(http.StatusOK, ret)
  790. arg, ok := util.JsonArg(c, ret)
  791. if !ok {
  792. return
  793. }
  794. notebook := arg["notebook"].(string)
  795. box := model.Conf.Box(notebook)
  796. var refCreateSaveBox string
  797. refCreateSavePathTpl := model.Conf.FileTree.RefCreateSavePath
  798. if nil != box {
  799. boxConf := box.GetConf()
  800. refCreateSaveBox = boxConf.RefCreateSaveBox
  801. refCreateSavePathTpl = boxConf.RefCreateSavePath
  802. }
  803. if "" == refCreateSaveBox && "" == refCreateSavePathTpl {
  804. refCreateSaveBox = model.Conf.FileTree.RefCreateSaveBox
  805. }
  806. if "" != refCreateSaveBox {
  807. if nil == model.Conf.Box(refCreateSaveBox) {
  808. // 如果配置的笔记本未打开或者不存在,则使用当前笔记本
  809. refCreateSaveBox = notebook
  810. }
  811. }
  812. if "" == refCreateSaveBox {
  813. refCreateSaveBox = notebook
  814. }
  815. if "" == refCreateSavePathTpl {
  816. refCreateSavePathTpl = model.Conf.FileTree.RefCreateSavePath
  817. }
  818. if refCreateSaveBox != notebook {
  819. if "" != refCreateSavePathTpl && !strings.HasPrefix(refCreateSavePathTpl, "/") {
  820. // 如果配置的笔记本不是当前笔记本,则将相对路径转换为绝对路径
  821. refCreateSavePathTpl = "/" + refCreateSavePathTpl
  822. }
  823. }
  824. refCreateSavePath, err := model.RenderGoTemplate(refCreateSavePathTpl)
  825. if err != nil {
  826. ret.Code = -1
  827. ret.Msg = err.Error()
  828. return
  829. }
  830. ret.Data = map[string]interface{}{
  831. "box": refCreateSaveBox,
  832. "path": refCreateSavePath,
  833. }
  834. }
  835. func changeSort(c *gin.Context) {
  836. ret := gulu.Ret.NewResult()
  837. defer c.JSON(http.StatusOK, ret)
  838. arg, ok := util.JsonArg(c, ret)
  839. if !ok {
  840. return
  841. }
  842. notebook := arg["notebook"].(string)
  843. pathsArg := arg["paths"].([]interface{})
  844. var paths []string
  845. for _, p := range pathsArg {
  846. paths = append(paths, p.(string))
  847. }
  848. model.ChangeFileTreeSort(notebook, paths)
  849. }
  850. func searchDocs(c *gin.Context) {
  851. ret := gulu.Ret.NewResult()
  852. defer c.JSON(http.StatusOK, ret)
  853. arg, ok := util.JsonArg(c, ret)
  854. if !ok {
  855. return
  856. }
  857. flashcard := false
  858. if arg["flashcard"] != nil {
  859. flashcard = arg["flashcard"].(bool)
  860. }
  861. k := arg["k"].(string)
  862. ret.Data = model.SearchDocsByKeyword(k, flashcard)
  863. }
  864. func listDocsByPath(c *gin.Context) {
  865. ret := gulu.Ret.NewResult()
  866. defer c.JSON(http.StatusOK, ret)
  867. arg, ok := util.JsonArg(c, ret)
  868. if !ok {
  869. return
  870. }
  871. notebook := arg["notebook"].(string)
  872. p := arg["path"].(string)
  873. sortParam := arg["sort"]
  874. sortMode := util.SortModeUnassigned
  875. if nil != sortParam {
  876. sortMode = int(sortParam.(float64))
  877. }
  878. flashcard := false
  879. if arg["flashcard"] != nil {
  880. flashcard = arg["flashcard"].(bool)
  881. }
  882. maxListCount := model.Conf.FileTree.MaxListCount
  883. if arg["maxListCount"] != nil {
  884. // API `listDocsByPath` add an optional parameter `maxListCount` https://github.com/siyuan-note/siyuan/issues/7993
  885. maxListCount = int(arg["maxListCount"].(float64))
  886. if 0 >= maxListCount {
  887. maxListCount = math.MaxInt
  888. }
  889. }
  890. showHidden := false
  891. if arg["showHidden"] != nil {
  892. showHidden = arg["showHidden"].(bool)
  893. }
  894. files, totals, err := model.ListDocTree(notebook, p, sortMode, flashcard, showHidden, maxListCount)
  895. if err != nil {
  896. ret.Code = -1
  897. ret.Msg = err.Error()
  898. return
  899. }
  900. if maxListCount < totals {
  901. // API `listDocsByPath` add an optional parameter `ignoreMaxListHint` https://github.com/siyuan-note/siyuan/issues/10290
  902. ignoreMaxListHintArg := arg["ignoreMaxListHint"]
  903. if nil == ignoreMaxListHintArg || !ignoreMaxListHintArg.(bool) {
  904. util.PushMsg(fmt.Sprintf(model.Conf.Language(48), len(files)), 7000)
  905. }
  906. }
  907. ret.Data = map[string]interface{}{
  908. "box": notebook,
  909. "path": p,
  910. "files": files,
  911. }
  912. }
  913. func getDoc(c *gin.Context) {
  914. ret := gulu.Ret.NewResult()
  915. defer c.JSON(http.StatusOK, ret)
  916. arg, ok := util.JsonArg(c, ret)
  917. if !ok {
  918. return
  919. }
  920. id := arg["id"].(string)
  921. idx := arg["index"]
  922. index := 0
  923. if nil != idx {
  924. index = int(idx.(float64))
  925. }
  926. var query string
  927. if queryArg := arg["query"]; nil != queryArg {
  928. query = queryArg.(string)
  929. }
  930. var queryMethod int
  931. if queryMethodArg := arg["queryMethod"]; nil != queryMethodArg {
  932. queryMethod = int(queryMethodArg.(float64))
  933. }
  934. var queryTypes map[string]bool
  935. if queryTypesArg := arg["queryTypes"]; nil != queryTypesArg {
  936. typesArg := queryTypesArg.(map[string]interface{})
  937. queryTypes = map[string]bool{}
  938. for t, b := range typesArg {
  939. queryTypes[t] = b.(bool)
  940. }
  941. }
  942. m := arg["mode"] // 0: 仅当前 ID,1:向上 2:向下,3:上下都加载,4:加载末尾
  943. mode := 0
  944. if nil != m {
  945. mode = int(m.(float64))
  946. }
  947. s := arg["size"]
  948. size := 102400 // 默认最大加载块数
  949. if nil != s {
  950. size = int(s.(float64))
  951. }
  952. startID := ""
  953. endID := ""
  954. startIDArg := arg["startID"]
  955. endIDArg := arg["endID"]
  956. if nil != startIDArg && nil != endIDArg {
  957. startID = startIDArg.(string)
  958. endID = endIDArg.(string)
  959. size = model.Conf.Editor.DynamicLoadBlocks
  960. }
  961. isBacklinkArg := arg["isBacklink"]
  962. isBacklink := false
  963. if nil != isBacklinkArg {
  964. isBacklink = isBacklinkArg.(bool)
  965. }
  966. highlightArg := arg["highlight"]
  967. highlight := true
  968. if nil != highlightArg {
  969. highlight = highlightArg.(bool)
  970. }
  971. blockCount, content, parentID, parent2ID, rootID, typ, eof, scroll, boxID, docPath, isBacklinkExpand, keywords, err :=
  972. model.GetDoc(startID, endID, id, index, query, queryTypes, queryMethod, mode, size, isBacklink, highlight)
  973. if model.ErrBlockNotFound == err {
  974. ret.Code = 3
  975. return
  976. }
  977. if err != nil {
  978. ret.Code = 1
  979. ret.Msg = err.Error()
  980. return
  981. }
  982. // 判断是否正在同步中 https://github.com/siyuan-note/siyuan/issues/6290
  983. isSyncing := model.IsSyncingFile(rootID)
  984. ret.Data = map[string]interface{}{
  985. "id": id,
  986. "mode": mode,
  987. "parentID": parentID,
  988. "parent2ID": parent2ID,
  989. "rootID": rootID,
  990. "type": typ,
  991. "content": content,
  992. "blockCount": blockCount,
  993. "eof": eof,
  994. "scroll": scroll,
  995. "box": boxID,
  996. "path": docPath,
  997. "isSyncing": isSyncing,
  998. "isBacklinkExpand": isBacklinkExpand,
  999. "keywords": keywords,
  1000. }
  1001. }
  1002. func pushCreate(box *model.Box, p, treeID string, arg map[string]interface{}) {
  1003. evt := util.NewCmdResult("create", 0, util.PushModeBroadcast)
  1004. name := path.Base(p)
  1005. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  1006. evt.Data = map[string]interface{}{
  1007. "box": box,
  1008. "path": p,
  1009. "files": files,
  1010. "name": name,
  1011. "id": treeID,
  1012. }
  1013. evt.Callback = arg["callback"]
  1014. util.PushEvent(evt)
  1015. }