-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
167 lines (146 loc) · 3.22 KB
/
api.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import(
"path/filepath"
"fmt"
"os"
"time"
"github.com/gin-gonic/gin"
"strings"
"strconv"
)
var resultCache map[string]*result
func apiInit(){
resultCache = make(map[string]*result)
}
func FailRequest(c *gin.Context, errcode int, errmsg string){
if errcode == 0 {
panic(1)
}
ret := BuildResponse(errcode, errmsg, nil)
c.JSON(200, ret)
}
func BuildResponse(errcode int, errmsg string, result interface{}) map[string]interface{} {
ret := make(map[string]interface{})
ret["errcode"] = errcode
ret["errmsg"] = errmsg
ret["result"] = result
return ret
}
func FinishRequest(c *gin.Context, errmsg string, result interface{}){
ret := BuildResponse(0, errmsg, result)
c.JSON(200, ret)
}
func scan(c *gin.Context) {
p := c.Query("path")
p = filepath.Clean(p)
if len(p) < 1 {
FailRequest(c, -201, "path error")
return
}
_, err := os.Stat(p)
if err != nil {
FailRequest(c, -201, "path not exists")
return
}
res := resultCache[p]
if res != nil {
if !res.finished {
FailRequest(c, -202, p + " in scanning. Try later.")
return
}
res.cleanup()
}
resultCache[p] = new(result)
go walkThrough(p)
FinishRequest(c, "ok", nil)
}
func walkThrough(root string) {
ret := resultCache[root]
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error{
if info == nil || info.Mode()&os.ModeSymlink != 0 {
return nil
}
node := &treeNode{}
node.setInfo(info, path)
ret.insertNode(node)
return nil
})
if err != nil {
fmt.Println(err)
ret.errmsg = err.Error()
}
ret.genDuplicatedKeys()
ret.finished = true
ret.endTime = time.Now()
}
func getStatus(c *gin.Context) {
res := getResFromRequest(c)
FinishRequest(c, "ok", res)
}
func getChildren(c *gin.Context) {
res := getResFromRequest(c)
if res == nil {
FailRequest(c, -201, "no info, scan first")
return
}
nodepath := c.Query("nodepath")
if len(nodepath) < 1 {
nodepath = res.rootPath
}
FinishRequest(c, "ok", res.getChildren(nodepath))
}
func remove(c *gin.Context) {
res := getResFromRequest(c)
if res == nil {
FailRequest(c, -201, "no info, scan first")
return
}
nodepath := c.Query("nodepath")
if len(nodepath) < 1 || res.nodeCache[nodepath] == nil {
FailRequest(c, -201, "path error")
return
}
err := os.RemoveAll(nodepath)
if err != nil {
FailRequest(c, -201, err.Error() )
return
}
node := res.nodeCache[nodepath]
res.removeNode(node)
reloadPath, _ := filepath.Rel(res.rootPath, filepath.Dir(nodepath))
FinishRequest(c, "ok", strings.Split(reloadPath, string(os.PathSeparator)) )
}
func getResFromRequest(c *gin.Context) *result {
p := c.Query("path")
p = filepath.Clean(p)
if len(p) < 1 {
return nil
}
res := resultCache[p]
if res == nil {
return nil
}
return res
}
func getDuplicated(c *gin.Context) {
res := getResFromRequest(c)
if res == nil {
FailRequest(c, -201, "no info, scan first")
return
}
pno := c.DefaultQuery("pageno", "1")
psz := c.DefaultQuery("pagesize", "20")
pnoi, err1 := strconv.Atoi(pno)
pszi, err2 := strconv.Atoi(psz)
if err1 != nil {
pnoi = 1
}
if err2 != nil {
pszi = 20
}
data, finished := res.getDuplicated(pnoi, pszi)
ret := make(map[string]interface{})
ret["data"] = data
ret["finished"] = finished
FinishRequest(c, "ok", ret)
}