From 2f1b55892a4f857e532071ad59234e607799729c Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Sun, 31 Jul 2016 11:47:02 +0800 Subject: [PATCH] add file search support --- .fsw.yml | 2 +- README.md | 2 +- httpstaticserver.go | 99 +++++++++++++++++++++++++++++---------------- res/index.tmpl.html | 2 +- res/js/index.js | 21 ++++++---- 5 files changed, 81 insertions(+), 45 deletions(-) diff --git a/.fsw.yml b/.fsw.yml index 9db2a22..a60a618 100644 --- a/.fsw.yml +++ b/.fsw.yml @@ -6,7 +6,7 @@ triggers: - '**/*.tmpl.html' env: DEBUG: "1" - cmd: (killall gohttpserver; true) && go build && ./gohttpserver --upload& + cmd: go build && ./gohttpserver --upload --root testdata shell: true delay: 100ms signal: KILL diff --git a/README.md b/README.md index accbb37..dcf252c 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Upload size now limited to 1G 1. [ ] Offline download 1. [ ] Code file preview 1. [ ] Edit file support -1. [ ] Global file search +1. [x] Global file search 1. [x] Hidden work `download` and `qrcode` in small screen 1. [x] Theme select support 1. [x] OK to working behide Nginx diff --git a/httpstaticserver.go b/httpstaticserver.go index 54df916..fa5b445 100644 --- a/httpstaticserver.go +++ b/httpstaticserver.go @@ -57,6 +57,18 @@ func (s *HTTPStaticServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.m.ServeHTTP(w, r) } +func (s *HTTPStaticServer) hIndex(w http.ResponseWriter, r *http.Request) { + path := mux.Vars(r)["path"] + relPath := filepath.Join(s.Root, path) + + finfo, err := os.Stat(relPath) + if err == nil && finfo.IsDir() { + tmpl.ExecuteTemplate(w, "index", s) + } else { + http.ServeFile(w, r, relPath) + } +} + func (s *HTTPStaticServer) hStatus(w http.ResponseWriter, r *http.Request) { data, _ := json.MarshalIndent(s, "", " ") w.Header().Set("Content-Type", "application/json") @@ -99,18 +111,6 @@ func (s *HTTPStaticServer) hUpload(w http.ResponseWriter, req *http.Request) { w.Write([]byte("Upload success")) } -func (s *HTTPStaticServer) hIndex(w http.ResponseWriter, r *http.Request) { - path := mux.Vars(r)["path"] - relPath := filepath.Join(s.Root, path) - finfo, err := os.Stat(relPath) - if err == nil && finfo.IsDir() { - tmpl.ExecuteTemplate(w, "index", s) - // tmpl.Execute(w, s) - } else { - http.ServeFile(w, r, relPath) - } -} - func (s *HTTPStaticServer) hZip(w http.ResponseWriter, r *http.Request) { path := mux.Vars(r)["path"] CompressToZip(w, filepath.Join(s.Root, path)) @@ -192,7 +192,6 @@ func (s *HTTPStaticServer) hIpaLink(w http.ResponseWriter, r *http.Request) { func (s *HTTPStaticServer) hFileOrDirectory(w http.ResponseWriter, r *http.Request) { path := mux.Vars(r)["path"] - log.Println("Path:", s.Root, path) http.ServeFile(w, r, filepath.Join(s.Root, path)) } @@ -204,34 +203,64 @@ type ListResponse struct { } func (s *HTTPStaticServer) hJSONList(w http.ResponseWriter, r *http.Request) { - path := mux.Vars(r)["path"] - lrs := make([]ListResponse, 0) - fd, err := os.Open(filepath.Join(s.Root, path)) - if err != nil { - http.Error(w, err.Error(), 500) - return - } - defer fd.Close() + requestPath := mux.Vars(r)["path"] + localPath := filepath.Join(s.Root, requestPath) + search := r.FormValue("search") - files, err := fd.Readdir(-1) - if err != nil { - http.Error(w, err.Error(), 500) - return - } - for _, file := range files { - lr := ListResponse{ - Name: file.Name(), - Path: filepath.Join(path, file.Name()), // lstrip "/" + // path string -> info os.FileInfo + fileInfoMap := make(map[string]os.FileInfo, 0) + + if search != "" { + err := filepath.Walk(localPath, func(path string, info os.FileInfo, err error) error { + if info.IsDir() { + return nil + } + if strings.Contains(strings.ToLower(path), strings.ToLower(search)) { + relPath, _ := filepath.Rel(localPath, path) + fileInfoMap[relPath] = info + } + return nil + }) + if err != nil { + http.Error(w, err.Error(), 500) + return } - if file.IsDir() { - fileName := deepPath(filepath.Join(s.Root, path), file.Name()) - lr.Name = fileName - lr.Path = filepath.Join(path, fileName) + } else { + fd, err := os.Open(localPath) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + defer fd.Close() + + infos, err := fd.Readdir(-1) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + for _, info := range infos { + fileInfoMap[filepath.Join(requestPath, info.Name())] = info + } + } + + lrs := make([]ListResponse, 0) + for path, info := range fileInfoMap { + lr := ListResponse{ + Name: info.Name(), + Path: path, // filepath.Join(path, file.Name()), // lstrip "/" + } + if search != "" { + lr.Name = path + } + if info.IsDir() { + name := deepPath(localPath, info.Name()) + lr.Name = name + lr.Path = filepath.Join(filepath.Dir(path), name) lr.Type = "dir" lr.Size = "-" } else { lr.Type = "file" - lr.Size = formatSize(file) + lr.Size = formatSize(info) } lrs = append(lrs, lr) } diff --git a/res/index.tmpl.html b/res/index.tmpl.html index 88af5a3..97cf60f 100644 --- a/res/index.tmpl.html +++ b/res/index.tmpl.html @@ -33,7 +33,7 @@