diff --git a/kernel/api/filetree.go b/kernel/api/filetree.go index e40ebdcb1..f0b867227 100644 --- a/kernel/api/filetree.go +++ b/kernel/api/filetree.go @@ -35,6 +35,59 @@ import ( "github.com/siyuan-note/siyuan/kernel/util" ) +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) + } + + id := ast.NewNodeID() + idArg := arg["id"] + if nil != idArg { + id = idArg.(string) + } + + 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) + if !strings.HasPrefix(hPath, "/") { + hPath = "/" + hPath + } + + id, err := model.MoveLocalShorthands(notebook, hPath, parentID, id) + if err != nil { + ret.Code = -1 + ret.Msg = err.Error() + return + } + ret.Data = id + + model.FlushTxQueue() + box := model.Conf.Box(notebook) + b, _ := model.GetBlock(id, nil) + pushCreate(box, b.Path, arg) +} + func listDocTree(c *gin.Context) { // Add kernel API `/api/filetree/listDocTree` https://github.com/siyuan-note/siyuan/issues/10482 diff --git a/kernel/job/cron.go b/kernel/job/cron.go index eef5d79dc..7255631f2 100644 --- a/kernel/job/cron.go +++ b/kernel/job/cron.go @@ -44,7 +44,6 @@ func StartCron() { go every(30*time.Second, model.HookDesktopUIProcJob) go every(24*time.Hour, model.AutoPurgeRepoJob) go every(30*time.Minute, model.AutoCheckMicrosoftDefender) - go every(7*time.Second, model.ShortcutsAppendToDailynote) } func every(interval time.Duration, f func()) { diff --git a/kernel/model/shortcuts.go b/kernel/model/shortcuts.go index c61173a25..9f53f75b0 100644 --- a/kernel/model/shortcuts.go +++ b/kernel/model/shortcuts.go @@ -27,15 +27,15 @@ import ( "github.com/siyuan-note/siyuan/kernel/util" ) -func ShortcutsAppendToDailynote() { - dailynotesDir := filepath.Join(util.ShortcutsPath, "dailynotes") - if !gulu.File.IsDir(dailynotesDir) { +func MoveLocalShorthands(boxID, hPath, parentID, id string) (retID string, err error) { + shorthandsDir := filepath.Join(util.ShortcutsPath, "shorthands") + if !gulu.File.IsDir(shorthandsDir) { return } - dir, err := os.ReadDir(dailynotesDir) + dir, err := os.ReadDir(shorthandsDir) if nil != err { - logging.LogErrorf("read dir [%s] failed: %s", dailynotesDir, err) + logging.LogErrorf("read dir [%s] failed: %s", shorthandsDir, err) return } @@ -50,7 +50,7 @@ func ShortcutsAppendToDailynote() { continue } - p := filepath.Join(dailynotesDir, entry.Name()) + p := filepath.Join(shorthandsDir, entry.Name()) data, readErr := os.ReadFile(p) if nil != readErr { logging.LogErrorf("read file [%s] failed: %s", p, readErr) @@ -59,22 +59,32 @@ func ShortcutsAppendToDailynote() { buff.Write(bytes.TrimSpace(data)) buff.WriteString("\n\n") - logging.LogInfof("read dailynote [%s] content [%s]", p, data) + logging.LogInfof("read shorthand [%s] content [%s]", p, data) toRemoves = append(toRemoves, p) } - defer func() { + clearShorthand := func(toRemoves []string) { for _, p := range toRemoves { - if err := os.Remove(p); nil != err { - logging.LogErrorf("remove file [%s] failed: %s", p, err) + if removeErr := os.Remove(p); nil != removeErr { + logging.LogErrorf("remove file [%s] failed: %s", p, removeErr) } } - }() + } content := strings.TrimSpace(buff.String()) if 1 > len(content) { + clearShorthand(toRemoves) return } - logging.LogInfof("append dailynote content [%s]", content) + logging.LogInfof("shorthand content [%s]", content) + + retID, err = CreateWithMarkdown("", boxID, hPath, content, parentID, id, false, "") + if nil != err { + logging.LogErrorf("create doc failed: %s", err) + return + } + + clearShorthand(toRemoves) + return }