Skip to content

Commit

Permalink
add file search support
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Jul 31, 2016
1 parent d7e808c commit 2f1b558
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .fsw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
95 changes: 62 additions & 33 deletions httpstaticserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
}

Expand All @@ -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
// 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
}
} 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
}
}
for _, file := range files {

lrs := make([]ListResponse, 0)
for path, info := range fileInfoMap {
lr := ListResponse{
Name: file.Name(),
Path: filepath.Join(path, file.Name()), // lstrip "/"
Name: info.Name(),
Path: path, // filepath.Join(path, file.Name()), // lstrip "/"
}
if search != "" {
lr.Name = path
}
if file.IsDir() {
fileName := deepPath(filepath.Join(s.Root, path), file.Name())
lr.Name = fileName
lr.Path = filepath.Join(path, fileName)
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)
}
Expand Down
2 changes: 1 addition & 1 deletion res/index.tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</ul>
<form class="navbar-form navbar-right">
<div class="input-group">
<input type="text" name="search" class="form-control" placeholder="Not finished.">
<input type="text" name="search" class="form-control" placeholder="{{search}}" autofocus required>
<span class="input-group-btn">
<button class="btn btn-default" type="button">
<span class="glyphicon glyphicon-search"></span>
Expand Down
21 changes: 14 additions & 7 deletions res/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ function pathJoin(parts, sep) {
return parts.join(separator).replace(replace, separator);
}

function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = decodeURI(window.location.search).substr(1).match(reg);
if (r != null) return r[2];
return null;
}

var vm = new Vue({
el: "#app",
data: {
Expand All @@ -20,6 +27,7 @@ var vm = new Vue({
showHidden: false,
previewFile: null,
version: "loading",
search: getQueryString("search") || "Search text",
files: [{
name: "loading ...",
path: "",
Expand Down Expand Up @@ -178,11 +186,13 @@ window.onpopstate = function(event) {

function loadDirectory(reqPath) {
window.history.pushState({}, "", reqPath);
loadFileList()
loadFileList(reqPath)
}

function loadFileList() {
$.getJSON("/-/json" + location.pathname, function(res) {
function loadFileList(pathname) {
var pathname = pathname || location.pathname;
// console.log("load filelist:", pathname)
$.getJSON("/-/json" + pathname, function(res) {
// console.log(res)
res.sort(function(a, b) {
var obj2n = function(v) {
Expand All @@ -193,13 +203,10 @@ function loadFileList() {
vm.files = res;
})
vm.updateBreadcrumb();
// if (Dropzone.options.myDropzone) {
// Dropzone.options.myDropzone.url = location.pathname;
// }
}

// For page first loading
loadFileList()
loadFileList(location.pathname + location.search)

// update version
$.getJSON("/-/sysinfo", function(res) {
Expand Down

0 comments on commit 2f1b558

Please sign in to comment.