123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729 |
- // SiYuan - Build Your Eternal Digital Garden
- // Copyright (c) 2020-present, b3log.org
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with this program. If not, see <https://www.gnu.org/licenses/>.
- package api
- import (
- "errors"
- "fmt"
- "net/http"
- "path"
- "regexp"
- "strings"
- "unicode/utf8"
- "github.com/88250/gulu"
- "github.com/gin-gonic/gin"
- "github.com/siyuan-note/filelock"
- "github.com/siyuan-note/siyuan/kernel/model"
- "github.com/siyuan-note/siyuan/kernel/util"
- )
- func reindexTree(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- path := arg["path"].(string)
- err := model.ReindexTree(path)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- ret.Data = map[string]interface{}{"closeTimeout": 5000}
- return
- }
- }
- func refreshFiletree(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- model.FullReindex()
- }
- func doc2Heading(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- srcID := arg["srcID"].(string)
- targetID := arg["targetID"].(string)
- after := arg["after"].(bool)
- srcTreeBox, srcTreePath, err := model.Doc2Heading(srcID, targetID, after)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- ret.Data = map[string]interface{}{"closeTimeout": 5000}
- return
- }
- ret.Data = map[string]interface{}{
- "srcTreeBox": srcTreeBox,
- "srcTreePath": srcTreePath,
- }
- }
- func heading2Doc(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- srcHeadingID := arg["srcHeadingID"].(string)
- targetNotebook := arg["targetNoteBook"].(string)
- targetPath := arg["targetPath"].(string)
- srcRootBlockID, targetPath, err := model.Heading2Doc(srcHeadingID, targetNotebook, targetPath)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- ret.Data = map[string]interface{}{"closeTimeout": 5000}
- return
- }
- model.WaitForWritingFiles()
- tree, err := model.LoadTree(targetNotebook, targetPath)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- name := path.Base(targetPath)
- box := model.Conf.Box(targetNotebook)
- files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), model.Conf.FileTree.Sort)
- evt := util.NewCmdResult("heading2doc", 0, util.PushModeBroadcast, util.PushModeNone)
- evt.Data = map[string]interface{}{
- "box": box,
- "path": targetPath,
- "files": files,
- "name": name,
- "id": tree.Root.ID,
- "srcRootBlockID": srcRootBlockID,
- }
- evt.Callback = arg["callback"]
- util.PushEvent(evt)
- }
- func li2Doc(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- srcListItemID := arg["srcListItemID"].(string)
- targetNotebook := arg["targetNoteBook"].(string)
- targetPath := arg["targetPath"].(string)
- srcRootBlockID, targetPath, err := model.ListItem2Doc(srcListItemID, targetNotebook, targetPath)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- ret.Data = map[string]interface{}{"closeTimeout": 5000}
- return
- }
- model.WaitForWritingFiles()
- tree, err := model.LoadTree(targetNotebook, targetPath)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- name := path.Base(targetPath)
- box := model.Conf.Box(targetNotebook)
- files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), model.Conf.FileTree.Sort)
- evt := util.NewCmdResult("li2doc", 0, util.PushModeBroadcast, util.PushModeNone)
- evt.Data = map[string]interface{}{
- "box": box,
- "path": targetPath,
- "files": files,
- "name": name,
- "id": tree.Root.ID,
- "srcRootBlockID": srcRootBlockID,
- }
- evt.Callback = arg["callback"]
- util.PushEvent(evt)
- }
- func getHPathByPath(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- notebook := arg["notebook"].(string)
- p := arg["path"].(string)
- hPath, err := model.GetHPathByPath(notebook, p)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- ret.Data = hPath
- }
- func getHPathsByPaths(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- pathsArg := arg["paths"].([]interface{})
- var paths []string
- for _, p := range pathsArg {
- paths = append(paths, p.(string))
- }
- hPath, err := model.GetHPathsByPaths(paths)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- ret.Data = hPath
- }
- func getHPathByID(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- id := arg["id"].(string)
- hPath, err := model.GetHPathByID(id)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- ret.Data = hPath
- }
- func getFullHPathByID(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- if nil == arg["id"] {
- return
- }
- id := arg["id"].(string)
- hPath, err := model.GetFullHPathByID(id)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- ret.Data = hPath
- }
- func moveDocs(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- var fromPaths []string
- fromPathsArg := arg["fromPaths"].([]interface{})
- for _, fromPath := range fromPathsArg {
- fromPaths = append(fromPaths, fromPath.(string))
- }
- toPath := arg["toPath"].(string)
- toNotebook := arg["toNotebook"].(string)
- err := model.MoveDocs(fromPaths, toNotebook, toPath)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- ret.Data = map[string]interface{}{"closeTimeout": 7000}
- return
- }
- }
- func removeDoc(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- notebook := arg["notebook"].(string)
- p := arg["path"].(string)
- err := model.RemoveDoc(notebook, p)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- }
- func removeDocs(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- pathsArg := arg["paths"].([]interface{})
- var paths []string
- for _, path := range pathsArg {
- paths = append(paths, path.(string))
- }
- err := model.RemoveDocs(paths)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- }
- func renameDoc(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- notebook := arg["notebook"].(string)
- p := arg["path"].(string)
- title := arg["title"].(string)
- err := model.RenameDoc(notebook, p, title)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- return
- }
- func duplicateDoc(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- id := arg["id"].(string)
- newTree, err := model.DuplicateDoc(id)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- ret.Data = map[string]interface{}{"closeTimeout": 7000}
- return
- }
- block, _ := model.GetBlock(id)
- p := block.Path
- notebook := block.Box
- box := model.Conf.Box(notebook)
- tree, err := model.LoadTree(box.ID, p)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- pushCreate(box, p, tree.Root.ID, arg)
- ret.Data = map[string]interface{}{
- "id": newTree.Root.ID,
- "notebook": notebook,
- "path": newTree.Path,
- "hPath": newTree.HPath,
- }
- }
- func createDoc(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- notebook := arg["notebook"].(string)
- p := arg["path"].(string)
- title := arg["title"].(string)
- md := arg["md"].(string)
- sortsArg := arg["sorts"]
- var sorts []string
- if nil != sortsArg {
- for _, sort := range sortsArg.([]interface{}) {
- sorts = append(sorts, sort.(string))
- }
- }
- err := model.CreateDocByMd(notebook, p, title, md, sorts)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- ret.Data = map[string]interface{}{"closeTimeout": 7000}
- return
- }
- box := model.Conf.Box(notebook)
- tree, err := model.LoadTree(box.ID, p)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- pushCreate(box, p, tree.Root.ID, arg)
- }
- func createDailyNote(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- notebook := arg["notebook"].(string)
- p, existed, err := model.CreateDailyNote(notebook)
- if nil != err {
- if model.ErrBoxNotFound == err {
- ret.Code = 1
- } else {
- ret.Code = -1
- }
- ret.Msg = err.Error()
- return
- }
- box := model.Conf.Box(notebook)
- model.WaitForWritingFiles()
- tree, err := model.LoadTree(box.ID, p)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- appArg := arg["app"]
- app := ""
- if nil != appArg {
- app = appArg.(string)
- }
- pushMode := util.PushModeBroadcast
- if existed && "" != app {
- pushMode = util.PushModeBroadcastApp
- }
- evt := util.NewCmdResult("createdailynote", 0, pushMode, util.PushModeNone)
- evt.AppId = app
- name := path.Base(p)
- files, _, _ := model.ListDocTree(box.ID, path.Dir(p), model.Conf.FileTree.Sort)
- evt.Data = map[string]interface{}{
- "box": box,
- "path": p,
- "files": files,
- "name": name,
- "id": tree.Root.ID,
- }
- evt.Callback = arg["callback"]
- util.PushEvent(evt)
- }
- func createDocWithMd(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- notebook := arg["notebook"].(string)
- hPath := arg["path"].(string)
- markdown := arg["markdown"].(string)
- baseName := path.Base(hPath)
- dir := path.Dir(hPath)
- r, _ := regexp.Compile("\r\n|\r|\n|\u2028|\u2029|\t|/")
- baseName = r.ReplaceAllString(baseName, "")
- if 512 < utf8.RuneCountInString(baseName) {
- baseName = gulu.Str.SubStr(baseName, 512)
- }
- hPath = path.Join(dir, baseName)
- if !strings.HasPrefix(hPath, "/") {
- hPath = "/" + hPath
- }
- id, err := model.CreateWithMarkdown(notebook, hPath, markdown)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- ret.Data = id
- box := model.Conf.Box(notebook)
- b, _ := model.GetBlock(id)
- p := b.Path
- pushCreate(box, p, id, arg)
- }
- func lockFile(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- id := arg["id"].(string)
- locked := model.TryAccessFileByBlockID(id)
- if !locked {
- ret.Code = -1
- ret.Msg = fmt.Sprintf(model.Conf.Language(75))
- ret.Data = map[string]interface{}{"closeTimeout": 5000}
- }
- }
- func getDocNameTemplate(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- notebook := arg["notebook"].(string)
- box := model.Conf.Box(notebook)
- nameTemplate := model.Conf.FileTree.CreateDocNameTemplate
- if nil != box {
- nameTemplate = box.GetConf().CreateDocNameTemplate
- }
- if "" == nameTemplate {
- nameTemplate = model.Conf.FileTree.CreateDocNameTemplate
- }
- name, err := model.RenderCreateDocNameTemplate(nameTemplate)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- ret.Data = map[string]interface{}{
- "name": name,
- }
- }
- func changeSort(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- notebook := arg["notebook"].(string)
- pathsArg := arg["paths"].([]interface{})
- var paths []string
- for _, p := range pathsArg {
- paths = append(paths, p.(string))
- }
- model.ChangeFileTreeSort(notebook, paths)
- }
- func searchDocs(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- k := arg["k"].(string)
- ret.Data = model.SearchDocsByKeyword(k)
- }
- func listDocsByPath(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- notebook := arg["notebook"].(string)
- p := arg["path"].(string)
- sortParam := arg["sort"]
- sortMode := model.Conf.FileTree.Sort
- if nil != sortParam {
- sortMode = int(sortParam.(float64))
- }
- files, totals, err := model.ListDocTree(notebook, p, sortMode)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- if model.Conf.FileTree.MaxListCount < totals {
- util.PushMsg(fmt.Sprintf(model.Conf.Language(48), len(files)), 7000)
- }
- ret.Data = map[string]interface{}{
- "box": notebook,
- "path": p,
- "files": files,
- }
- // 持久化文档面板排序
- model.Conf.FileTree.Sort = sortMode
- model.Conf.Save()
- }
- func getDoc(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- id := arg["id"].(string)
- idx := arg["index"]
- index := 0
- if nil != idx {
- index = int(idx.(float64))
- }
- k := arg["k"]
- var keyword string
- if nil != k {
- keyword = k.(string)
- }
- m := arg["mode"] // 0: 仅当前 ID,1:向上 2:向下,3:上下都加载,4:加载末尾
- mode := 0
- if nil != m {
- mode = int(m.(float64))
- }
- s := arg["size"]
- size := 102400 // 默认最大加载块数
- if nil != s {
- size = int(s.(float64))
- }
- startID := ""
- endID := ""
- startIDArg := arg["startID"]
- endIDArg := arg["endID"]
- if nil != startIDArg && nil != endIDArg {
- startID = startIDArg.(string)
- endID = endIDArg.(string)
- size = 36
- }
- blockCount, childBlockCount, content, parentID, parent2ID, rootID, typ, eof, boxID, docPath, err := model.GetDoc(startID, endID, id, index, keyword, mode, size)
- if errors.Is(err, filelock.ErrUnableAccessFile) {
- ret.Code = 2
- ret.Data = id
- return
- }
- if model.ErrBlockNotFound == err {
- ret.Code = 3
- return
- }
- if nil != err {
- ret.Code = 1
- ret.Msg = err.Error()
- return
- }
- // 判断是否正在同步中 https://github.com/siyuan-note/siyuan/issues/6290
- isSyncing := model.IsSyncingFile(rootID)
- ret.Data = map[string]interface{}{
- "id": id,
- "mode": mode,
- "parentID": parentID,
- "parent2ID": parent2ID,
- "rootID": rootID,
- "type": typ,
- "content": content,
- "blockCount": blockCount,
- "childBlockCount": childBlockCount,
- "eof": eof,
- "box": boxID,
- "path": docPath,
- "isSyncing": isSyncing,
- }
- }
- func pushCreate(box *model.Box, p, treeID string, arg map[string]interface{}) {
- evt := util.NewCmdResult("create", 0, util.PushModeBroadcast, util.PushModeNone)
- name := path.Base(p)
- files, _, _ := model.ListDocTree(box.ID, path.Dir(p), model.Conf.FileTree.Sort)
- evt.Data = map[string]interface{}{
- "box": box,
- "path": p,
- "files": files,
- "name": name,
- "id": treeID,
- }
- evt.Callback = arg["callback"]
- util.PushEvent(evt)
- }
|