2022-05-26 15:18:53 +08:00
|
|
|
|
// 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 (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"path"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
|
|
"github.com/88250/gulu"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-02-10 14:28:10 +08:00
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/filesys"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/model"
|
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func refreshFiletree(c *gin.Context) {
|
|
|
|
|
ret := gulu.Ret.NewResult()
|
|
|
|
|
defer c.JSON(http.StatusOK, ret)
|
|
|
|
|
|
2022-09-16 19:45:46 +08:00
|
|
|
|
model.FullReindex()
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
2023-02-10 14:28:10 +08:00
|
|
|
|
luteEngine := util.NewLute()
|
|
|
|
|
tree, err := filesys.LoadTree(targetNotebook, targetPath, luteEngine)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
2023-01-01 14:09:36 +08:00
|
|
|
|
evt := util.NewCmdResult("heading2doc", 0, util.PushModeBroadcast)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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()
|
2023-02-10 14:28:10 +08:00
|
|
|
|
luteEngine := util.NewLute()
|
|
|
|
|
tree, err := filesys.LoadTree(targetNotebook, targetPath, luteEngine)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
2023-01-01 14:09:36 +08:00
|
|
|
|
evt := util.NewCmdResult("li2doc", 0, util.PushModeBroadcast)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
2023-02-02 11:06:29 +08:00
|
|
|
|
if util.InvalidIDPattern(notebook, ret) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
p := arg["path"].(string)
|
|
|
|
|
|
|
|
|
|
hPath, err := model.GetHPathByPath(notebook, p)
|
|
|
|
|
if nil != err {
|
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ret.Data = hPath
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-04 21:37:10 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
2023-02-02 11:06:29 +08:00
|
|
|
|
if util.InvalidIDPattern(id, ret) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
2022-06-07 18:18:03 +08:00
|
|
|
|
if nil == arg["id"] {
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
|
id := arg["id"].(string)
|
|
|
|
|
hPath, err := model.GetFullHPathByID(id)
|
|
|
|
|
if nil != err {
|
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ret.Data = hPath
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 14:27:17 +08:00
|
|
|
|
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)
|
2022-11-04 19:25:25 +08:00
|
|
|
|
toNotebook := arg["toNotebook"].(string)
|
2023-02-02 11:06:29 +08:00
|
|
|
|
if util.InvalidIDPattern(toNotebook, ret) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-11-03 14:27:17 +08:00
|
|
|
|
|
2023-03-27 15:22:33 +08:00
|
|
|
|
callback := arg["callback"]
|
|
|
|
|
|
|
|
|
|
err := model.MoveDocs(fromPaths, toNotebook, toPath, callback)
|
2022-11-03 14:27:17 +08:00
|
|
|
|
if nil != err {
|
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
ret.Data = map[string]interface{}{"closeTimeout": 7000}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
2023-02-02 11:06:29 +08:00
|
|
|
|
if util.InvalidIDPattern(notebook, ret) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
p := arg["path"].(string)
|
2023-01-19 21:36:56 +08:00
|
|
|
|
model.RemoveDoc(notebook, p)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 14:27:17 +08:00
|
|
|
|
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))
|
|
|
|
|
}
|
2023-01-19 21:36:56 +08:00
|
|
|
|
model.RemoveDocs(paths)
|
2022-11-03 14:27:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
2023-02-02 11:06:29 +08:00
|
|
|
|
if util.InvalidIDPattern(notebook, ret) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
2023-01-27 18:07:06 +08:00
|
|
|
|
tree, err := model.LoadTreeByID(id)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if nil != err {
|
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
ret.Data = map[string]interface{}{"closeTimeout": 7000}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-27 18:07:06 +08:00
|
|
|
|
p := tree.Path
|
|
|
|
|
notebook := tree.Box
|
2022-05-26 15:18:53 +08:00
|
|
|
|
box := model.Conf.Box(notebook)
|
2023-01-27 18:07:06 +08:00
|
|
|
|
model.DuplicateDoc(tree)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
pushCreate(box, p, tree.Root.ID, arg)
|
2022-08-19 16:26:57 +08:00
|
|
|
|
|
|
|
|
|
ret.Data = map[string]interface{}{
|
2023-01-27 18:07:06 +08:00
|
|
|
|
"id": tree.Root.ID,
|
2022-08-19 16:26:57 +08:00
|
|
|
|
"notebook": notebook,
|
2023-01-27 18:07:06 +08:00
|
|
|
|
"path": tree.Path,
|
|
|
|
|
"hPath": tree.HPath,
|
2022-08-19 16:26:57 +08:00
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
2022-07-01 22:41:28 +08:00
|
|
|
|
sortsArg := arg["sorts"]
|
|
|
|
|
var sorts []string
|
|
|
|
|
if nil != sortsArg {
|
|
|
|
|
for _, sort := range sortsArg.([]interface{}) {
|
|
|
|
|
sorts = append(sorts, sort.(string))
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
2023-01-27 16:48:42 +08:00
|
|
|
|
tree, err := model.CreateDocByMd(notebook, p, title, md, sorts)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if nil != err {
|
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
ret.Data = map[string]interface{}{"closeTimeout": 7000}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
box := model.Conf.Box(notebook)
|
|
|
|
|
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)
|
2022-10-21 11:35:02 +08:00
|
|
|
|
p, existed, err := model.CreateDailyNote(notebook)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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()
|
2023-02-10 14:28:10 +08:00
|
|
|
|
luteEngine := util.NewLute()
|
|
|
|
|
tree, err := filesys.LoadTree(box.ID, p, luteEngine)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if nil != err {
|
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 11:35:02 +08:00
|
|
|
|
appArg := arg["app"]
|
|
|
|
|
app := ""
|
|
|
|
|
if nil != appArg {
|
|
|
|
|
app = appArg.(string)
|
|
|
|
|
}
|
|
|
|
|
pushMode := util.PushModeBroadcast
|
|
|
|
|
if existed && "" != app {
|
|
|
|
|
pushMode = util.PushModeBroadcastApp
|
|
|
|
|
}
|
2023-01-01 14:09:36 +08:00
|
|
|
|
evt := util.NewCmdResult("createdailynote", 0, pushMode)
|
2022-10-21 11:35:02 +08:00
|
|
|
|
evt.AppId = app
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
2023-02-02 11:06:29 +08:00
|
|
|
|
if util.InvalidIDPattern(notebook, ret) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
2023-01-27 18:07:06 +08:00
|
|
|
|
b, _ := model.GetBlock(id, nil)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
p := b.Path
|
|
|
|
|
pushCreate(box, p, id, arg)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 20:47:43 +08:00
|
|
|
|
func getDocCreateSavePath(c *gin.Context) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
2023-02-03 20:47:43 +08:00
|
|
|
|
docCreateSavePathTpl := model.Conf.FileTree.DocCreateSavePath
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if nil != box {
|
2023-02-03 20:47:43 +08:00
|
|
|
|
docCreateSavePathTpl = box.GetConf().DocCreateSavePath
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2023-02-03 20:47:43 +08:00
|
|
|
|
if "" == docCreateSavePathTpl {
|
|
|
|
|
docCreateSavePathTpl = model.Conf.FileTree.DocCreateSavePath
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 21:09:48 +08:00
|
|
|
|
p, err := model.RenderGoTemplate(docCreateSavePathTpl)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if nil != err {
|
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ret.Data = map[string]interface{}{
|
2023-02-03 21:09:48 +08:00
|
|
|
|
"path": p,
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 10:18:22 +08:00
|
|
|
|
func getRefCreateSavePath(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)
|
|
|
|
|
refCreateSavePath := model.Conf.FileTree.RefCreateSavePath
|
|
|
|
|
if nil != box {
|
|
|
|
|
refCreateSavePath = box.GetConf().RefCreateSavePath
|
|
|
|
|
}
|
|
|
|
|
if "" == refCreateSavePath {
|
|
|
|
|
refCreateSavePath = model.Conf.FileTree.RefCreateSavePath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p, err := model.RenderGoTemplate(refCreateSavePath)
|
|
|
|
|
if nil != err {
|
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ret.Data = map[string]interface{}{
|
|
|
|
|
"path": p,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
}
|
2022-08-06 00:00:23 +08:00
|
|
|
|
startID := ""
|
|
|
|
|
endID := ""
|
|
|
|
|
startIDArg := arg["startID"]
|
|
|
|
|
endIDArg := arg["endID"]
|
|
|
|
|
if nil != startIDArg && nil != endIDArg {
|
|
|
|
|
startID = startIDArg.(string)
|
|
|
|
|
endID = endIDArg.(string)
|
2022-08-18 11:20:33 +08:00
|
|
|
|
size = 36
|
2022-08-06 00:00:23 +08:00
|
|
|
|
}
|
2022-12-24 12:05:55 +08:00
|
|
|
|
isBacklinkArg := arg["isBacklink"]
|
|
|
|
|
isBacklink := false
|
|
|
|
|
if nil != isBacklinkArg {
|
|
|
|
|
isBacklink = isBacklinkArg.(bool)
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
2023-03-18 10:38:22 +08:00
|
|
|
|
blockCount, content, parentID, parent2ID, rootID, typ, eof, scroll, boxID, docPath, isBacklinkExpand, err := model.GetDoc(startID, endID, id, index, keyword, mode, size, isBacklink)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if model.ErrBlockNotFound == err {
|
|
|
|
|
ret.Code = 3
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if nil != err {
|
|
|
|
|
ret.Code = 1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-22 11:55:12 +08:00
|
|
|
|
// 判断是否正在同步中 https://github.com/siyuan-note/siyuan/issues/6290
|
|
|
|
|
isSyncing := model.IsSyncingFile(rootID)
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ret.Data = map[string]interface{}{
|
2022-12-24 12:05:55 +08:00
|
|
|
|
"id": id,
|
|
|
|
|
"mode": mode,
|
|
|
|
|
"parentID": parentID,
|
|
|
|
|
"parent2ID": parent2ID,
|
|
|
|
|
"rootID": rootID,
|
|
|
|
|
"type": typ,
|
|
|
|
|
"content": content,
|
|
|
|
|
"blockCount": blockCount,
|
|
|
|
|
"eof": eof,
|
2023-03-18 10:38:22 +08:00
|
|
|
|
"scroll": scroll,
|
2022-12-24 12:05:55 +08:00
|
|
|
|
"box": boxID,
|
|
|
|
|
"path": docPath,
|
|
|
|
|
"isSyncing": isSyncing,
|
|
|
|
|
"isBacklinkExpand": isBacklinkExpand,
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func pushCreate(box *model.Box, p, treeID string, arg map[string]interface{}) {
|
2023-01-01 14:09:36 +08:00
|
|
|
|
evt := util.NewCmdResult("create", 0, util.PushModeBroadcast)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
|
|
|
|
}
|