/
localPath.go
63 lines (53 loc) · 1.16 KB
/
localPath.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package proxy
import (
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/files"
"runtime"
"strings"
)
type LocalPathAction actions.Action
// 本地路径查询
func (this *LocalPathAction) Run(params struct {
Prefix string
}) {
prefix := params.Prefix
this.Data["paths"] = []string{}
if len(prefix) == 0 {
this.Success()
}
dirPrefix := prefix
sub := ""
index := 0
if runtime.GOOS == "windows" {
prefix = strings.Replace(prefix, "/", "\\", -1)
index = strings.LastIndex(prefix, "\\")
} else {
prefix = strings.Replace(prefix, "\\", "/", -1)
index = strings.LastIndex(prefix, "/")
}
if index > -1 {
dirPrefix = prefix[:index+1]
sub = prefix[index+1:]
}
this.Data["dir"] = dirPrefix
this.Data["sub"] = sub
// 查找路径
dir := files.NewFile(dirPrefix)
if !dir.Exists() {
this.Success()
}
paths := []string{}
for _, f := range dir.List() {
absPath, _ := f.AbsPath()
if len(sub) == 0 {
paths = append(paths, absPath)
} else if strings.Index(strings.ToLower(f.Name()), strings.ToLower(sub)) > -1 {
paths = append(paths, absPath)
}
if len(paths) >= 10 {
break
}
}
this.Data["paths"] = paths
this.Success()
}