package model import ( . "github.com/mickael-kerjean/filestash/server/common" "os" "path/filepath" "strings" "time" ) type PathQuandidate struct { Path string Score int } func scoreBoostForPath(p string) int { b := strings.ToLower(filepath.Base(p)) // some path are garbage we don't want to explore unless there's nothing else to do if b == "node_modules" { return -100 } else if strings.HasPrefix(b, ".") { return -10 } // not all path are equally interesting, we bump the score of what we thing is interesting score := 0 if strings.Contains(b, "document") { score += 3 } else if strings.Contains(b, "project") { score += 3 } else if strings.Contains(b, "home") { score += 3 } else if strings.Contains(b, "note") { score += 3 } return score } func scoreBoostForFilesInDirectory(f []os.FileInfo) int { s := 0 for i:=0; i 4 { return 4 } } return s } func scoreBoostOnDepth(p string) int { return - strings.Count(p, "/") } func SearchStateLess(app *App, path string, keyword string) []File { files := make([]File, 0) toVisit := []PathQuandidate{PathQuandidate{path, 0}} MAX_SEARCH_TIME := 300 * time.Millisecond for start := time.Now() ; time.Since(start) < MAX_SEARCH_TIME; { if len(toVisit) == 0 { return files } currentPath := toVisit[0] if len(toVisit) == 0 { toVisit = make([]PathQuandidate, 0) } else { toVisit = toVisit[1:] } // Ls on the directory f, err := app.Backend.Ls(currentPath.Path) if err != nil { continue } score1 := scoreBoostForFilesInDirectory(f) for i:=0; i toVisit[k].Score { break } t[k] = toVisit[k] } t[k] = PathQuandidate{fullpath, score} for k=k+1; k