2023-06-24 20:39:55 +08:00
|
|
|
|
// SiYuan - Refactor your thinking
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// 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"
|
2023-04-14 12:02:55 +08:00
|
|
|
|
"math"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"net/http"
|
2024-03-05 20:24:23 +08:00
|
|
|
|
"os"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"path"
|
2024-03-05 20:24:23 +08:00
|
|
|
|
"path/filepath"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
|
|
"github.com/88250/gulu"
|
2023-09-28 00:15:46 +08:00
|
|
|
|
"github.com/88250/lute/ast"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"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"
|
|
|
|
|
)
|
|
|
|
|
|
2025-03-26 11:51:37 +08:00
|
|
|
|
func moveLocalShorthands(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)
|
|
|
|
|
if util.InvalidIDPattern(notebook, ret) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var parentID string
|
|
|
|
|
parentIDArg := arg["parentID"]
|
|
|
|
|
if nil != parentIDArg {
|
|
|
|
|
parentID = parentIDArg.(string)
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-28 21:32:09 +08:00
|
|
|
|
var hPath string
|
|
|
|
|
hPathArg := arg["path"]
|
|
|
|
|
if nil != hPathArg {
|
|
|
|
|
hPath = arg["path"].(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)
|
2025-03-26 11:51:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-03-29 11:47:15 +08:00
|
|
|
|
// TODO: 改造旧方案,去掉 hPath, parentID,改为使用文档树配置项 闪念速记存放位置,参考创建日记实现
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/14414
|
2025-03-28 21:32:09 +08:00
|
|
|
|
ids, err := model.MoveLocalShorthands(notebook, hPath, parentID)
|
2025-03-26 11:51:37 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.FlushTxQueue()
|
|
|
|
|
box := model.Conf.Box(notebook)
|
2025-03-28 21:32:09 +08:00
|
|
|
|
for _, id := range ids {
|
|
|
|
|
b, _ := model.GetBlock(id, nil)
|
|
|
|
|
pushCreate(box, b.Path, arg)
|
|
|
|
|
}
|
2025-03-26 11:51:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 20:24:23 +08:00
|
|
|
|
func listDocTree(c *gin.Context) {
|
|
|
|
|
// Add kernel API `/api/filetree/listDocTree` https://github.com/siyuan-note/siyuan/issues/10482
|
|
|
|
|
|
|
|
|
|
ret := gulu.Ret.NewResult()
|
|
|
|
|
defer c.JSON(http.StatusOK, ret)
|
|
|
|
|
|
|
|
|
|
arg, ok := util.JsonArg(c, ret)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notebook := arg["notebook"].(string)
|
|
|
|
|
if util.InvalidIDPattern(notebook, ret) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p := arg["path"].(string)
|
|
|
|
|
var doctree []*DocFile
|
|
|
|
|
root := filepath.Join(util.WorkspaceDir, "data", notebook, p)
|
|
|
|
|
dir, err := os.ReadDir(root)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2024-03-05 20:24:23 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ids := map[string]bool{}
|
|
|
|
|
for _, entry := range dir {
|
|
|
|
|
if entry.IsDir() {
|
|
|
|
|
if strings.HasPrefix(entry.Name(), ".") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !ast.IsNodeIDPattern(entry.Name()) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parent := &DocFile{ID: entry.Name()}
|
|
|
|
|
ids[parent.ID] = true
|
|
|
|
|
doctree = append(doctree, parent)
|
|
|
|
|
|
|
|
|
|
subPath := filepath.Join(root, entry.Name())
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = walkDocTree(subPath, parent, &ids); err != nil {
|
2024-03-05 20:24:23 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
doc := &DocFile{ID: strings.TrimSuffix(entry.Name(), ".sy")}
|
|
|
|
|
if !ids[doc.ID] {
|
|
|
|
|
doctree = append(doctree, doc)
|
|
|
|
|
}
|
|
|
|
|
ids[doc.ID] = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret.Data = map[string]interface{}{
|
|
|
|
|
"tree": doctree,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DocFile struct {
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
Children []*DocFile `json:"children,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func walkDocTree(p string, docFile *DocFile, ids *map[string]bool) (err error) {
|
|
|
|
|
dir, err := os.ReadDir(p)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2024-03-05 20:24:23 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, entry := range dir {
|
|
|
|
|
if entry.IsDir() {
|
|
|
|
|
if strings.HasPrefix(entry.Name(), ".") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !ast.IsNodeIDPattern(entry.Name()) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parent := &DocFile{ID: entry.Name()}
|
|
|
|
|
(*ids)[parent.ID] = true
|
|
|
|
|
docFile.Children = append(docFile.Children, parent)
|
|
|
|
|
|
|
|
|
|
subPath := filepath.Join(p, entry.Name())
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = walkDocTree(subPath, parent, ids); err != nil {
|
2024-03-05 20:24:23 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
doc := &DocFile{ID: strings.TrimSuffix(entry.Name(), ".sy")}
|
|
|
|
|
if !(*ids)[doc.ID] {
|
|
|
|
|
docFile.Children = append(docFile.Children, doc)
|
|
|
|
|
}
|
|
|
|
|
(*ids)[doc.ID] = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 20:54:46 +08:00
|
|
|
|
func upsertIndexes(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))
|
|
|
|
|
}
|
|
|
|
|
model.UpsertIndexes(paths)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func removeIndexes(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))
|
|
|
|
|
}
|
|
|
|
|
model.RemoveIndexes(paths)
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
2024-11-22 22:36:19 +08:00
|
|
|
|
var targetPath string
|
|
|
|
|
if arg["targetPath"] != nil {
|
|
|
|
|
targetPath = arg["targetPath"].(string)
|
|
|
|
|
}
|
|
|
|
|
var previousPath string
|
|
|
|
|
if arg["previousPath"] != nil {
|
|
|
|
|
previousPath = arg["previousPath"].(string)
|
|
|
|
|
}
|
|
|
|
|
srcRootBlockID, targetPath, err := model.Heading2Doc(srcHeadingID, targetNotebook, targetPath, previousPath)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
ret.Data = map[string]interface{}{"closeTimeout": 5000}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 19:20:44 +08:00
|
|
|
|
model.FlushTxQueue()
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
|
box := model.Conf.Box(targetNotebook)
|
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,
|
|
|
|
|
"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)
|
2024-11-22 22:36:19 +08:00
|
|
|
|
var targetPath string
|
|
|
|
|
if arg["targetPath"] != nil {
|
|
|
|
|
targetPath = arg["targetPath"].(string)
|
|
|
|
|
}
|
|
|
|
|
var previousPath string
|
|
|
|
|
if arg["previousPath"] != nil {
|
|
|
|
|
previousPath = arg["previousPath"].(string)
|
|
|
|
|
}
|
|
|
|
|
srcRootBlockID, targetPath, err := model.ListItem2Doc(srcListItemID, targetNotebook, targetPath, previousPath)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
ret.Data = map[string]interface{}{"closeTimeout": 5000}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 19:20:44 +08:00
|
|
|
|
model.FlushTxQueue()
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
|
box := model.Conf.Box(targetNotebook)
|
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,
|
|
|
|
|
"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)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-11-04 21:37:10 +08:00
|
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ret.Data = hPath
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 17:45:09 +08:00
|
|
|
|
func getPathByID(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)
|
|
|
|
|
if util.InvalidIDPattern(id, ret) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-04 20:22:48 +08:00
|
|
|
|
p, notebook, err := model.GetPathByID(id)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2024-08-28 17:45:09 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-03-04 20:22:48 +08:00
|
|
|
|
ret.Data = map[string]interface{}{
|
|
|
|
|
"path": p,
|
|
|
|
|
"notebook": notebook,
|
|
|
|
|
}
|
2024-08-28 17:45:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ret.Data = hPath
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-15 09:08:41 +08:00
|
|
|
|
func getIDsByHPath(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["path"] {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if nil == arg["notebook"] {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notebook := arg["notebook"].(string)
|
|
|
|
|
if util.InvalidIDPattern(notebook, ret) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p := arg["path"].(string)
|
|
|
|
|
ids, err := model.GetIDsByHPath(p, notebook)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2023-11-15 09:08:41 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ret.Data = ids
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
2023-03-27 15:22:33 +08:00
|
|
|
|
callback := arg["callback"]
|
|
|
|
|
err := model.MoveDocs(fromPaths, toNotebook, toPath, callback)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-11-03 14:27:17 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
ret.Data = map[string]interface{}{"closeTimeout": 7000}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-05 10:49:54 +08:00
|
|
|
|
func moveDocsByID(c *gin.Context) {
|
|
|
|
|
ret := gulu.Ret.NewResult()
|
|
|
|
|
defer c.JSON(http.StatusOK, ret)
|
|
|
|
|
|
|
|
|
|
arg, ok := util.JsonArg(c, ret)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fromIDsArg := arg["fromIDs"].([]any)
|
|
|
|
|
var fromIDs []string
|
|
|
|
|
for _, fromIDArg := range fromIDsArg {
|
|
|
|
|
fromID := fromIDArg.(string)
|
|
|
|
|
if util.InvalidIDPattern(fromID, ret) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fromIDs = append(fromIDs, fromID)
|
|
|
|
|
}
|
|
|
|
|
toID := arg["toID"].(string)
|
|
|
|
|
if util.InvalidIDPattern(toID, ret) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var fromPaths []string
|
|
|
|
|
for _, fromID := range fromIDs {
|
|
|
|
|
tree, err := model.LoadTreeByBlockID(fromID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
ret.Data = map[string]interface{}{"closeTimeout": 7000}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fromPaths = append(fromPaths, tree.Path)
|
|
|
|
|
}
|
|
|
|
|
fromPaths = gulu.Str.RemoveDuplicatedElem(fromPaths)
|
|
|
|
|
|
|
|
|
|
toTree, err := model.LoadTreeByBlockID(toID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
ret.Data = map[string]interface{}{"closeTimeout": 7000}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
toNotebook := toTree.Box
|
|
|
|
|
toPath := toTree.Path
|
|
|
|
|
callback := arg["callback"]
|
|
|
|
|
err = model.MoveDocs(fromPaths, toNotebook, toPath, callback)
|
|
|
|
|
if err != nil {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 09:35:07 +08:00
|
|
|
|
func removeDocByID(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)
|
|
|
|
|
if util.InvalidIDPattern(id, ret) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tree, err := model.LoadTreeByBlockID(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
ret.Data = map[string]interface{}{"closeTimeout": 7000}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.RemoveDoc(tree.Box, tree.Path)
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 11:37:42 +08:00
|
|
|
|
func renameDocByID(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)
|
2024-11-27 09:35:07 +08:00
|
|
|
|
if util.InvalidIDPattern(id, ret) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 11:37:42 +08:00
|
|
|
|
title := arg["title"].(string)
|
|
|
|
|
|
|
|
|
|
tree, err := model.LoadTreeByBlockID(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
ret.Data = map[string]interface{}{"closeTimeout": 7000}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = model.RenameDoc(tree.Box, tree.Path, title)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
2024-03-10 23:27:13 +08:00
|
|
|
|
tree, err := model.LoadTreeByBlockID(id)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
ret.Data = map[string]interface{}{"closeTimeout": 7000}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-27 18:07:06 +08:00
|
|
|
|
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)
|
2025-02-17 16:03:43 +08:00
|
|
|
|
arg["listDocTree"] = true
|
2025-02-09 11:33:43 +08:00
|
|
|
|
pushCreate(box, tree.Path, 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)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
ret.Data = map[string]interface{}{"closeTimeout": 7000}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 19:20:44 +08:00
|
|
|
|
model.FlushTxQueue()
|
2025-02-09 12:27:58 +08:00
|
|
|
|
box := model.Conf.Box(notebook)
|
|
|
|
|
pushCreate(box, p, arg)
|
2023-11-29 10:51:17 +08:00
|
|
|
|
|
|
|
|
|
ret.Data = map[string]interface{}{
|
|
|
|
|
"id": tree.Root.ID,
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if model.ErrBoxNotFound == err {
|
|
|
|
|
ret.Code = 1
|
|
|
|
|
} else {
|
|
|
|
|
ret.Code = -1
|
|
|
|
|
}
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 19:20:44 +08:00
|
|
|
|
model.FlushTxQueue()
|
2023-12-19 16:40:36 +08:00
|
|
|
|
box := model.Conf.Box(notebook)
|
2023-02-10 14:28:10 +08:00
|
|
|
|
luteEngine := util.NewLute()
|
|
|
|
|
tree, err := filesys.LoadTree(box.ID, p, luteEngine)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-23 22:17:41 +08:00
|
|
|
|
if !existed {
|
|
|
|
|
// 只有创建的情况才推送,已经存在的情况不推送
|
2023-12-23 22:17:53 +08:00
|
|
|
|
// Creating a dailynote existed no longer expands the doc tree https://github.com/siyuan-note/siyuan/issues/9959
|
2023-12-23 22:17:41 +08:00
|
|
|
|
appArg := arg["app"]
|
|
|
|
|
app := ""
|
|
|
|
|
if nil != appArg {
|
|
|
|
|
app = appArg.(string)
|
|
|
|
|
}
|
|
|
|
|
evt := util.NewCmdResult("createdailynote", 0, util.PushModeBroadcast)
|
|
|
|
|
evt.AppId = app
|
|
|
|
|
evt.Data = map[string]interface{}{
|
2025-02-09 12:28:56 +08:00
|
|
|
|
"box": box,
|
|
|
|
|
"path": p,
|
2023-12-23 22:17:41 +08:00
|
|
|
|
}
|
|
|
|
|
evt.Callback = arg["callback"]
|
|
|
|
|
util.PushEvent(evt)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2023-11-17 12:06:31 +08:00
|
|
|
|
|
|
|
|
|
ret.Data = map[string]interface{}{
|
|
|
|
|
"id": tree.Root.ID,
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-20 11:49:07 +08:00
|
|
|
|
tagsArg := arg["tags"]
|
|
|
|
|
var tags string
|
|
|
|
|
if nil != tagsArg {
|
|
|
|
|
tags = tagsArg.(string)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-01 12:35:17 +08:00
|
|
|
|
var parentID string
|
|
|
|
|
parentIDArg := arg["parentID"]
|
|
|
|
|
if nil != parentIDArg {
|
|
|
|
|
parentID = parentIDArg.(string)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-28 00:15:46 +08:00
|
|
|
|
id := ast.NewNodeID()
|
|
|
|
|
idArg := arg["id"]
|
|
|
|
|
if nil != idArg {
|
|
|
|
|
id = idArg.(string)
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-21 00:04:56 +08:00
|
|
|
|
withMath := false
|
|
|
|
|
withMathArg := arg["withMath"]
|
|
|
|
|
if nil != withMathArg {
|
|
|
|
|
withMath = withMathArg.(bool)
|
|
|
|
|
}
|
2024-11-12 10:12:32 +08:00
|
|
|
|
clippingHref := ""
|
|
|
|
|
clippingHrefArg := arg["clippingHref"]
|
|
|
|
|
if nil != clippingHrefArg {
|
|
|
|
|
clippingHref = clippingHrefArg.(string)
|
|
|
|
|
}
|
2024-05-21 00:04:56 +08:00
|
|
|
|
|
2024-11-12 10:12:32 +08:00
|
|
|
|
id, err := model.CreateWithMarkdown(tags, notebook, hPath, markdown, parentID, id, withMath, clippingHref)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ret.Data = id
|
|
|
|
|
|
2024-10-22 19:20:44 +08:00
|
|
|
|
model.FlushTxQueue()
|
2025-02-09 12:27:58 +08:00
|
|
|
|
box := model.Conf.Box(notebook)
|
|
|
|
|
b, _ := model.GetBlock(id, nil)
|
|
|
|
|
pushCreate(box, b.Path, arg)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
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)
|
2024-04-25 11:18:34 +08:00
|
|
|
|
var docCreateSaveBox string
|
2023-02-03 20:47:43 +08:00
|
|
|
|
docCreateSavePathTpl := model.Conf.FileTree.DocCreateSavePath
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if nil != box {
|
2024-04-25 22:32:30 +08:00
|
|
|
|
boxConf := box.GetConf()
|
|
|
|
|
docCreateSaveBox = boxConf.DocCreateSaveBox
|
|
|
|
|
docCreateSavePathTpl = boxConf.DocCreateSavePath
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2024-04-30 17:23:00 +08:00
|
|
|
|
if "" == docCreateSaveBox && "" == docCreateSavePathTpl {
|
2024-04-25 11:18:34 +08:00
|
|
|
|
docCreateSaveBox = model.Conf.FileTree.DocCreateSaveBox
|
|
|
|
|
}
|
2024-04-25 22:32:30 +08:00
|
|
|
|
if "" != docCreateSaveBox {
|
|
|
|
|
if nil == model.Conf.Box(docCreateSaveBox) {
|
2024-04-25 23:03:33 +08:00
|
|
|
|
// 如果配置的笔记本未打开或者不存在,则使用当前笔记本
|
2024-04-25 22:32:30 +08:00
|
|
|
|
docCreateSaveBox = notebook
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-25 11:18:34 +08:00
|
|
|
|
if "" == docCreateSaveBox {
|
|
|
|
|
docCreateSaveBox = notebook
|
|
|
|
|
}
|
2023-02-03 20:47:43 +08:00
|
|
|
|
if "" == docCreateSavePathTpl {
|
|
|
|
|
docCreateSavePathTpl = model.Conf.FileTree.DocCreateSavePath
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2023-05-12 16:34:09 +08:00
|
|
|
|
docCreateSavePathTpl = strings.TrimSpace(docCreateSavePathTpl)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
2024-04-25 23:03:33 +08:00
|
|
|
|
if docCreateSaveBox != notebook {
|
|
|
|
|
if "" != docCreateSavePathTpl && !strings.HasPrefix(docCreateSavePathTpl, "/") {
|
|
|
|
|
// 如果配置的笔记本不是当前笔记本,则将相对路径转换为绝对路径
|
|
|
|
|
docCreateSavePathTpl = "/" + docCreateSavePathTpl
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-25 11:18:34 +08:00
|
|
|
|
docCreateSavePath, err := model.RenderGoTemplate(docCreateSavePathTpl)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-04-25 11:18:34 +08:00
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ret.Data = map[string]interface{}{
|
2024-04-25 11:18:34 +08:00
|
|
|
|
"box": docCreateSaveBox,
|
|
|
|
|
"path": docCreateSavePath,
|
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)
|
2024-04-25 11:18:34 +08:00
|
|
|
|
var refCreateSaveBox string
|
|
|
|
|
refCreateSavePathTpl := model.Conf.FileTree.RefCreateSavePath
|
2022-12-12 10:18:22 +08:00
|
|
|
|
if nil != box {
|
2024-04-25 22:32:30 +08:00
|
|
|
|
boxConf := box.GetConf()
|
|
|
|
|
refCreateSaveBox = boxConf.RefCreateSaveBox
|
|
|
|
|
refCreateSavePathTpl = boxConf.RefCreateSavePath
|
2024-04-25 11:18:34 +08:00
|
|
|
|
}
|
2024-04-30 17:23:00 +08:00
|
|
|
|
if "" == refCreateSaveBox && "" == refCreateSavePathTpl {
|
2024-04-25 11:18:34 +08:00
|
|
|
|
refCreateSaveBox = model.Conf.FileTree.RefCreateSaveBox
|
|
|
|
|
}
|
2024-04-25 22:32:30 +08:00
|
|
|
|
if "" != refCreateSaveBox {
|
|
|
|
|
if nil == model.Conf.Box(refCreateSaveBox) {
|
2024-04-25 23:03:33 +08:00
|
|
|
|
// 如果配置的笔记本未打开或者不存在,则使用当前笔记本
|
2024-04-25 22:32:30 +08:00
|
|
|
|
refCreateSaveBox = notebook
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-25 11:18:34 +08:00
|
|
|
|
if "" == refCreateSaveBox {
|
|
|
|
|
refCreateSaveBox = notebook
|
2022-12-12 10:18:22 +08:00
|
|
|
|
}
|
2024-04-25 11:18:34 +08:00
|
|
|
|
if "" == refCreateSavePathTpl {
|
|
|
|
|
refCreateSavePathTpl = model.Conf.FileTree.RefCreateSavePath
|
2022-12-12 10:18:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-25 23:03:33 +08:00
|
|
|
|
if refCreateSaveBox != notebook {
|
|
|
|
|
if "" != refCreateSavePathTpl && !strings.HasPrefix(refCreateSavePathTpl, "/") {
|
|
|
|
|
// 如果配置的笔记本不是当前笔记本,则将相对路径转换为绝对路径
|
|
|
|
|
refCreateSavePathTpl = "/" + refCreateSavePathTpl
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-25 11:18:34 +08:00
|
|
|
|
refCreateSavePath, err := model.RenderGoTemplate(refCreateSavePathTpl)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-12 10:18:22 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ret.Data = map[string]interface{}{
|
2024-04-25 11:18:34 +08:00
|
|
|
|
"box": refCreateSaveBox,
|
|
|
|
|
"path": refCreateSavePath,
|
2022-12-12 10:18:22 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-13 19:07:09 +08:00
|
|
|
|
flashcard := false
|
|
|
|
|
if arg["flashcard"] != nil {
|
|
|
|
|
flashcard = arg["flashcard"].(bool)
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
k := arg["k"].(string)
|
2023-04-13 19:07:09 +08:00
|
|
|
|
ret.Data = model.SearchDocsByKeyword(k, flashcard)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"]
|
2023-05-02 23:22:40 +08:00
|
|
|
|
sortMode := util.SortModeUnassigned
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if nil != sortParam {
|
|
|
|
|
sortMode = int(sortParam.(float64))
|
|
|
|
|
}
|
2023-04-13 19:07:09 +08:00
|
|
|
|
flashcard := false
|
|
|
|
|
if arg["flashcard"] != nil {
|
|
|
|
|
flashcard = arg["flashcard"].(bool)
|
|
|
|
|
}
|
2023-04-14 12:00:11 +08:00
|
|
|
|
maxListCount := model.Conf.FileTree.MaxListCount
|
|
|
|
|
if arg["maxListCount"] != nil {
|
2023-04-14 12:02:55 +08:00
|
|
|
|
// API `listDocsByPath` add an optional parameter `maxListCount` https://github.com/siyuan-note/siyuan/issues/7993
|
2023-04-14 12:00:11 +08:00
|
|
|
|
maxListCount = int(arg["maxListCount"].(float64))
|
2023-04-14 12:03:08 +08:00
|
|
|
|
if 0 >= maxListCount {
|
2023-04-14 12:02:55 +08:00
|
|
|
|
maxListCount = math.MaxInt
|
|
|
|
|
}
|
2023-04-14 12:00:11 +08:00
|
|
|
|
}
|
2023-09-05 16:45:37 +08:00
|
|
|
|
showHidden := false
|
2023-09-05 16:42:15 +08:00
|
|
|
|
if arg["showHidden"] != nil {
|
|
|
|
|
showHidden = arg["showHidden"].(bool)
|
|
|
|
|
}
|
2023-04-13 19:07:09 +08:00
|
|
|
|
|
2023-09-05 16:42:15 +08:00
|
|
|
|
files, totals, err := model.ListDocTree(notebook, p, sortMode, flashcard, showHidden, maxListCount)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ret.Code = -1
|
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-14 12:00:11 +08:00
|
|
|
|
if maxListCount < totals {
|
2024-02-01 17:46:21 +08:00
|
|
|
|
// API `listDocsByPath` add an optional parameter `ignoreMaxListHint` https://github.com/siyuan-note/siyuan/issues/10290
|
|
|
|
|
ignoreMaxListHintArg := arg["ignoreMaxListHint"]
|
|
|
|
|
if nil == ignoreMaxListHintArg || !ignoreMaxListHintArg.(bool) {
|
|
|
|
|
util.PushMsg(fmt.Sprintf(model.Conf.Language(48), len(files)), 7000)
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
}
|
2023-05-19 10:18:25 +08:00
|
|
|
|
|
|
|
|
|
var query string
|
|
|
|
|
if queryArg := arg["query"]; nil != queryArg {
|
|
|
|
|
query = queryArg.(string)
|
|
|
|
|
}
|
|
|
|
|
var queryMethod int
|
|
|
|
|
if queryMethodArg := arg["queryMethod"]; nil != queryMethodArg {
|
|
|
|
|
queryMethod = int(queryMethodArg.(float64))
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2023-05-19 10:18:25 +08:00
|
|
|
|
var queryTypes map[string]bool
|
|
|
|
|
if queryTypesArg := arg["queryTypes"]; nil != queryTypesArg {
|
|
|
|
|
typesArg := queryTypesArg.(map[string]interface{})
|
|
|
|
|
queryTypes = map[string]bool{}
|
|
|
|
|
for t, b := range typesArg {
|
|
|
|
|
queryTypes[t] = b.(bool)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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)
|
2023-05-19 21:55:11 +08:00
|
|
|
|
size = model.Conf.Editor.DynamicLoadBlocks
|
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)
|
|
|
|
|
}
|
2025-01-12 01:07:55 +08:00
|
|
|
|
originalRefBlockIDsArg := arg["originalRefBlockIDs"]
|
|
|
|
|
originalRefBlockIDs := map[string]string{}
|
|
|
|
|
if nil != originalRefBlockIDsArg {
|
|
|
|
|
m := originalRefBlockIDsArg.(map[string]interface{})
|
|
|
|
|
for k, v := range m {
|
|
|
|
|
originalRefBlockIDs[k] = v.(string)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-05 10:11:07 +08:00
|
|
|
|
highlightArg := arg["highlight"]
|
|
|
|
|
highlight := true
|
|
|
|
|
if nil != highlightArg {
|
|
|
|
|
highlight = highlightArg.(bool)
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
2024-12-07 11:54:30 +08:00
|
|
|
|
blockCount, content, parentID, parent2ID, rootID, typ, eof, scroll, boxID, docPath, isBacklinkExpand, keywords, err :=
|
2025-01-12 01:07:55 +08:00
|
|
|
|
model.GetDoc(startID, endID, id, index, query, queryTypes, queryMethod, mode, size, isBacklink, originalRefBlockIDs, highlight)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if model.ErrBlockNotFound == err {
|
|
|
|
|
ret.Code = 3
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
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,
|
2024-12-07 11:54:30 +08:00
|
|
|
|
"keywords": keywords,
|
2024-12-08 16:46:41 +08:00
|
|
|
|
"reqId": arg["reqId"],
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-09 11:33:43 +08:00
|
|
|
|
func pushCreate(box *model.Box, p string, arg map[string]interface{}) {
|
2023-01-01 14:09:36 +08:00
|
|
|
|
evt := util.NewCmdResult("create", 0, util.PushModeBroadcast)
|
2025-02-09 12:27:58 +08:00
|
|
|
|
listDocTree := false
|
|
|
|
|
listDocTreeArg := arg["listDocTree"]
|
|
|
|
|
if nil != listDocTreeArg {
|
|
|
|
|
listDocTree = listDocTreeArg.(bool)
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
evt.Data = map[string]interface{}{
|
2025-02-09 12:27:58 +08:00
|
|
|
|
"box": box,
|
|
|
|
|
"path": p,
|
|
|
|
|
"listDocTree": listDocTree,
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
evt.Callback = arg["callback"]
|
|
|
|
|
util.PushEvent(evt)
|
|
|
|
|
}
|