@@ -57,6 +57,18 @@ func (s *HTTPStaticServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
5757 s .m .ServeHTTP (w , r )
5858}
5959
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+
6072func (s * HTTPStaticServer ) hStatus (w http.ResponseWriter , r * http.Request ) {
6173 data , _ := json .MarshalIndent (s , "" , " " )
6274 w .Header ().Set ("Content-Type" , "application/json" )
@@ -99,18 +111,6 @@ func (s *HTTPStaticServer) hUpload(w http.ResponseWriter, req *http.Request) {
99111 w .Write ([]byte ("Upload success" ))
100112}
101113
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-
114114func (s * HTTPStaticServer ) hZip (w http.ResponseWriter , r * http.Request ) {
115115 path := mux .Vars (r )["path" ]
116116 CompressToZip (w , filepath .Join (s .Root , path ))
@@ -192,7 +192,6 @@ func (s *HTTPStaticServer) hIpaLink(w http.ResponseWriter, r *http.Request) {
192192
193193func (s * HTTPStaticServer ) hFileOrDirectory (w http.ResponseWriter , r * http.Request ) {
194194 path := mux .Vars (r )["path" ]
195- log .Println ("Path:" , s .Root , path )
196195 http .ServeFile (w , r , filepath .Join (s .Root , path ))
197196}
198197
@@ -204,34 +203,64 @@ type ListResponse struct {
204203}
205204
206205func (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" )
215209
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+ }
220244 }
221- for _ , file := range files {
245+
246+ lrs := make ([]ListResponse , 0 )
247+ for path , info := range fileInfoMap {
222248 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
225254 }
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 )
230259 lr .Type = "dir"
231260 lr .Size = "-"
232261 } else {
233262 lr .Type = "file"
234- lr .Size = formatSize (file )
263+ lr .Size = formatSize (info )
235264 }
236265 lrs = append (lrs , lr )
237266 }
0 commit comments