Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vanessa 2022-11-07 21:38:29 +08:00
commit cd82976170
5 changed files with 147 additions and 7 deletions

View file

@ -61,6 +61,37 @@ func searchHistory(c *gin.Context) {
}
}
func getHistoryItems(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
arg, ok := util.JsonArg(c, ret)
if !ok {
return
}
created := arg["created"].(string)
notebook := ""
if nil != arg["notebook"] {
notebook = arg["notebook"].(string)
}
typ := model.HistoryTypeDoc
if nil != arg["type"] {
typ = int(arg["type"].(float64))
}
query := arg["query"].(string)
op := "all"
if nil != arg["op"] {
op = arg["op"].(string)
}
histories := model.FullTextSearchHistoryItems(created, query, notebook, op, typ)
ret.Data = map[string]interface{}{
"items": histories,
}
}
func reindexHistory(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)

View file

@ -111,6 +111,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/history/clearWorkspaceHistory", model.CheckAuth, model.CheckReadonly, clearWorkspaceHistory)
ginServer.Handle("POST", "/api/history/reindexHistory", model.CheckAuth, model.CheckReadonly, reindexHistory)
ginServer.Handle("POST", "/api/history/searchHistory", model.CheckAuth, model.CheckReadonly, searchHistory)
ginServer.Handle("POST", "/api/history/getHistoryItems", model.CheckAuth, model.CheckReadonly, getHistoryItems)
ginServer.Handle("POST", "/api/outline/getDocOutline", model.CheckAuth, getDocOutline)
ginServer.Handle("POST", "/api/bookmark/getBookmark", model.CheckAuth, getBookmark)

View file

@ -360,6 +360,38 @@ func FullTextSearchHistory(query, box, op string, typ, page int) (ret []string,
return
}
func FullTextSearchHistoryItems(created, query, box, op string, typ int) (ret []*HistoryItem) {
query = gulu.Str.RemoveInvisible(query)
if "" != query {
query = stringQuery(query)
}
table := "histories_fts_case_insensitive"
stmt := "SELECT * FROM " + table + " WHERE "
if "" != query {
stmt += table + " MATCH '{title content}:(" + query + ")'"
} else {
stmt += "1=1"
}
if HistoryTypeDocName == typ {
stmt = strings.ReplaceAll(stmt, "{title content}", "{title}")
}
if HistoryTypeDocName == typ || HistoryTypeDoc == typ {
if "all" != op {
stmt += " AND op = '" + op + "'"
}
stmt += " AND path LIKE '%/" + box + "/%' AND path LIKE '%.sy'"
} else if HistoryTypeAsset == typ {
stmt += " AND path LIKE '%/assets/%'"
}
stmt += " AND created = '" + created + "' ORDER BY created DESC LIMIT " + fmt.Sprintf("%d", Conf.Search.Limit)
sqlHistories := sql.SelectHistoriesRawStmt(stmt)
ret = fromSQLHistories(sqlHistories)
return
}
func GetNotebookHistory() (ret []*History, err error) {
ret = []*History{}
@ -656,3 +688,22 @@ func indexHistoryDir(name string, luteEngine *lute.Lute) {
}
return
}
func fromSQLHistories(sqlHistories []*sql.History) (ret []*HistoryItem) {
if 1 > len(sqlHistories) {
ret = []*HistoryItem{}
return
}
for _, sqlHistory := range sqlHistories {
item := &HistoryItem{
Title: sqlHistory.Title,
Path: filepath.Join(util.HistoryDir, sqlHistory.Path),
}
if HistoryTypeAsset == sqlHistory.Type {
item.Path = filepath.ToSlash(strings.TrimPrefix(item.Path, util.WorkspaceDir))
}
ret = append(ret, item)
}
return
}

View file

@ -78,8 +78,7 @@ func Serve(fastMode bool) {
api.ServeAPI(ginServer)
if !fastMode {
// 杀掉占用 6806 的已有内核进程
killByPort(util.FixedPort)
killRunningKernel()
}
var host string
@ -460,6 +459,39 @@ func corsMiddleware() gin.HandlerFunc {
}
}
func killRunningKernel() {
defer logging.Recover()
if "dev" == util.Mode {
return
}
if util.ContainerStd != util.Container {
return
}
processes, err := goPS.Processes()
if nil != err {
logging.LogErrorf("get processes failed: %s", err)
killByPort(util.FixedPort)
return
}
killed := false
for _, process := range processes {
procName := strings.ToLower(process.Executable())
if strings.Contains(procName, "siyuan-kernel") {
kill(fmt.Sprintf("%d", process.Pid()))
killed = true
}
}
if killed {
portJSON := filepath.Join(util.HomeDir, ".config", "siyuan", "port.json")
os.RemoveAll(portJSON)
}
}
func killByPort(port string) {
if !isPortOpen(port) {
return
@ -497,14 +529,14 @@ func isPortOpen(port string) bool {
}
func kill(pid string) {
var kill *exec.Cmd
var killCmd *exec.Cmd
if gulu.OS.IsWindows() {
kill = exec.Command("cmd", "/c", "TASKKILL /F /PID "+pid)
killCmd = exec.Command("cmd", "/c", "TASKKILL /F /PID "+pid)
} else {
kill = exec.Command("kill", "-9", pid)
killCmd = exec.Command("kill", "-9", pid)
}
gulu.CmdAttr(kill)
kill.CombinedOutput()
gulu.CmdAttr(killCmd)
killCmd.CombinedOutput()
}
func pidByPort(port string) (ret string) {

View file

@ -69,6 +69,31 @@ func QueryHistory(stmt string) (ret []map[string]interface{}, err error) {
return
}
func SelectHistoriesRawStmt(stmt string) (ret []*History) {
rows, err := historyDB.Query(stmt)
if nil != err {
logging.LogWarnf("sql query [%s] failed: %s", stmt, err)
return
}
defer rows.Close()
for rows.Next() {
if history := scanHistoryRows(rows); nil != history {
ret = append(ret, history)
}
}
return
}
func scanHistoryRows(rows *sql.Rows) (ret *History) {
var history History
if err := rows.Scan(&history.Type, &history.Op, &history.Title, &history.Content, &history.Path, &history.Created); nil != err {
logging.LogErrorf("query scan field failed: %s\n%s", err, logging.ShortStack())
return
}
ret = &history
return
}
func queryHistory(query string, args ...interface{}) (*sql.Rows, error) {
query = strings.TrimSpace(query)
if "" == query {