This commit is contained in:
Liang Ding 2014-11-13 17:00:29 +08:00
parent c40c055465
commit 7e93fcf20d
2 changed files with 94 additions and 2 deletions

View File

@ -285,6 +285,18 @@ func RenameFile(w http.ResponseWriter, r *http.Request) {
} }
} }
// Use to find results sorting.
type foundPath struct {
Path string `json:"path"`
score int
}
type foundPaths []*foundPath
func (f foundPaths) Len() int { return len(f) }
func (f foundPaths) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f foundPaths) Less(i, j int) bool { return f[i].score > f[j].score }
// Find handles request of find files under the specified directory with the specified filename pattern. // Find handles request of find files under the specified directory with the specified filename pattern.
func Find(w http.ResponseWriter, r *http.Request) { func Find(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true} data := map[string]interface{}{"succ": true}
@ -298,10 +310,31 @@ func Find(w http.ResponseWriter, r *http.Request) {
return return
} }
dir := args["dir"].(string) path := args["path"].(string) // path of selected file in file tree
name := args["name"].(string) name := args["name"].(string)
founds := find(dir, name, []*string{}) session, _ := session.HTTPSession.Get(r, "wide-session")
username := session.Values["username"].(string)
userWorkspace := conf.Wide.GetUserWorkspace(username)
workspaces := filepath.SplitList(userWorkspace)
if "" != path && !isDir(path) {
path = filepath.Dir(path)
}
founds := foundPaths{}
for _, workspace := range workspaces {
rs := find(workspace, name, []*string{})
for _, r := range rs {
substr := util.Str.LCS(path, *r)
founds = append(founds, &foundPath{Path: *r, score: len(substr)})
}
}
sort.Sort(founds)
data["founds"] = founds data["founds"] = founds
} }
@ -661,6 +694,18 @@ func isImg(extension string) bool {
} }
} }
// isDir determines whether the specified path is a directory.
func isDir(path string) bool {
fio, err := os.Lstat(path)
if nil != err {
glog.Warningf("Determines whether [%s] is a directory failed: [%v]", path, err)
return false
}
return fio.IsDir()
}
// GetUsre gets the user the specified path belongs to. Returns nil if not found. // GetUsre gets the user the specified path belongs to. Returns nil if not found.
func GetUsre(path string) *conf.User { func GetUsre(path string) *conf.User {
for _, user := range conf.Wide.Users { for _, user := range conf.Wide.Users {

47
util/string.go Executable file
View File

@ -0,0 +1,47 @@
package util
type str struct{}
// String utilities.
var Str = str{}
// Contains determines whether the str is in the strs.
func (*str) Contains(str string, strs []string) bool {
for _, v := range strs {
if v == str {
return true
}
}
return false
}
// LCS gets the longest common substring of s1 and s2.
//
// Refers to http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Longest_common_substring.
func (*str) LCS(s1 string, s2 string) string {
var m = make([][]int, 1+len(s1))
for i := 0; i < len(m); i++ {
m[i] = make([]int, 1+len(s2))
}
longest := 0
x_longest := 0
for x := 1; x < 1+len(s1); x++ {
for y := 1; y < 1+len(s2); y++ {
if s1[x-1] == s2[y-1] {
m[x][y] = m[x-1][y-1] + 1
if m[x][y] > longest {
longest = m[x][y]
x_longest = x
}
} else {
m[x][y] = 0
}
}
}
return s1[x_longest-longest : x_longest]
}