@@ -57,6 +57,18 @@ func (s *HTTPStaticServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
57
57
s .m .ServeHTTP (w , r )
58
58
}
59
59
60
+ func (s * HTTPStaticServer ) hIndex (w http.ResponseWriter , r * http.Request ) {
61
+ path := mux .Vars (r )["path" ]
62
+ relPath := filepath .Join (s .Root , path )
63
+
64
+ finfo , err := os .Stat (relPath )
65
+ if err == nil && finfo .IsDir () {
66
+ tmpl .ExecuteTemplate (w , "index" , s )
67
+ } else {
68
+ http .ServeFile (w , r , relPath )
69
+ }
70
+ }
71
+
60
72
func (s * HTTPStaticServer ) hStatus (w http.ResponseWriter , r * http.Request ) {
61
73
data , _ := json .MarshalIndent (s , "" , " " )
62
74
w .Header ().Set ("Content-Type" , "application/json" )
@@ -99,18 +111,6 @@ func (s *HTTPStaticServer) hUpload(w http.ResponseWriter, req *http.Request) {
99
111
w .Write ([]byte ("Upload success" ))
100
112
}
101
113
102
- func (s * HTTPStaticServer ) hIndex (w http.ResponseWriter , r * http.Request ) {
103
- path := mux .Vars (r )["path" ]
104
- relPath := filepath .Join (s .Root , path )
105
- finfo , err := os .Stat (relPath )
106
- if err == nil && finfo .IsDir () {
107
- tmpl .ExecuteTemplate (w , "index" , s )
108
- // tmpl.Execute(w, s)
109
- } else {
110
- http .ServeFile (w , r , relPath )
111
- }
112
- }
113
-
114
114
func (s * HTTPStaticServer ) hZip (w http.ResponseWriter , r * http.Request ) {
115
115
path := mux .Vars (r )["path" ]
116
116
CompressToZip (w , filepath .Join (s .Root , path ))
@@ -192,7 +192,6 @@ func (s *HTTPStaticServer) hIpaLink(w http.ResponseWriter, r *http.Request) {
192
192
193
193
func (s * HTTPStaticServer ) hFileOrDirectory (w http.ResponseWriter , r * http.Request ) {
194
194
path := mux .Vars (r )["path" ]
195
- log .Println ("Path:" , s .Root , path )
196
195
http .ServeFile (w , r , filepath .Join (s .Root , path ))
197
196
}
198
197
@@ -204,34 +203,64 @@ type ListResponse struct {
204
203
}
205
204
206
205
func (s * HTTPStaticServer ) hJSONList (w http.ResponseWriter , r * http.Request ) {
207
- path := mux .Vars (r )["path" ]
208
- lrs := make ([]ListResponse , 0 )
209
- fd , err := os .Open (filepath .Join (s .Root , path ))
210
- if err != nil {
211
- http .Error (w , err .Error (), 500 )
212
- return
213
- }
214
- defer fd .Close ()
206
+ requestPath := mux .Vars (r )["path" ]
207
+ localPath := filepath .Join (s .Root , requestPath )
208
+ search := r .FormValue ("search" )
215
209
216
- files , err := fd .Readdir (- 1 )
217
- if err != nil {
218
- http .Error (w , err .Error (), 500 )
219
- return
210
+ // path string -> info os.FileInfo
211
+ fileInfoMap := make (map [string ]os.FileInfo , 0 )
212
+
213
+ if search != "" {
214
+ err := filepath .Walk (localPath , func (path string , info os.FileInfo , err error ) error {
215
+ if info .IsDir () {
216
+ return nil
217
+ }
218
+ if strings .Contains (strings .ToLower (path ), strings .ToLower (search )) {
219
+ relPath , _ := filepath .Rel (localPath , path )
220
+ fileInfoMap [relPath ] = info
221
+ }
222
+ return nil
223
+ })
224
+ if err != nil {
225
+ http .Error (w , err .Error (), 500 )
226
+ return
227
+ }
228
+ } else {
229
+ fd , err := os .Open (localPath )
230
+ if err != nil {
231
+ http .Error (w , err .Error (), 500 )
232
+ return
233
+ }
234
+ defer fd .Close ()
235
+
236
+ infos , err := fd .Readdir (- 1 )
237
+ if err != nil {
238
+ http .Error (w , err .Error (), 500 )
239
+ return
240
+ }
241
+ for _ , info := range infos {
242
+ fileInfoMap [filepath .Join (requestPath , info .Name ())] = info
243
+ }
220
244
}
221
- for _ , file := range files {
245
+
246
+ lrs := make ([]ListResponse , 0 )
247
+ for path , info := range fileInfoMap {
222
248
lr := ListResponse {
223
- Name : file .Name (),
224
- Path : filepath .Join (path , file .Name ()), // lstrip "/"
249
+ Name : info .Name (),
250
+ Path : path , // filepath.Join(path, file.Name()), // lstrip "/"
251
+ }
252
+ if search != "" {
253
+ lr .Name = path
225
254
}
226
- if file .IsDir () {
227
- fileName := deepPath (filepath . Join ( s . Root , path ), file .Name ())
228
- lr .Name = fileName
229
- lr .Path = filepath .Join (path , fileName )
255
+ if info .IsDir () {
256
+ name := deepPath (localPath , info .Name ())
257
+ lr .Name = name
258
+ lr .Path = filepath .Join (filepath . Dir ( path ), name )
230
259
lr .Type = "dir"
231
260
lr .Size = "-"
232
261
} else {
233
262
lr .Type = "file"
234
- lr .Size = formatSize (file )
263
+ lr .Size = formatSize (info )
235
264
}
236
265
lrs = append (lrs , lr )
237
266
}
0 commit comments