Initial add of sorting

This commit is contained in:
Your Name 2018-09-02 19:10:41 +00:00
parent 7064535a00
commit 73ab6461ba
1 changed files with 40 additions and 2 deletions

View File

@ -50,16 +50,54 @@ func mapDirOpenError(originalErr error, name string) error {
} }
func dirList(w http.ResponseWriter, r *http.Request, f http.File) { func dirList(w http.ResponseWriter, r *http.Request, f http.File) {
tableheader := ""
dirs, err := f.Readdir(-1) dirs, err := f.Readdir(-1)
if err != nil { if err != nil {
logf(r, "http: error reading directory: %v", err) logf(r, "http: error reading directory: %v", err)
http.Error(w, "Error reading directory", http.StatusInternalServerError) http.Error(w, "Error reading directory", http.StatusInternalServerError)
return return
} }
// Check for sort parameters
key, ok := r.URL.Query()["key"]
if !ok || (key[0] != "filename" && key[0] != "date" && key[0] != "size") {
key[0] = "filename"
}
order, ok := r.URL.Query()["key"]
if !ok || (order[0] != "asc" && order[0] != "desc") {
order[0] = "asc"
}
switch {
case (key[0] == "filename" && order[0] == "asc"):
sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() })
tableheader = "<th><a href='?key=filename&order=desc'>Filename</a></th><th><a href='?key=size'>Size</a></th><th><a href='?key=date'>Date</a></th>"
case (key[0] == "filename" && order[0] == "desc"):
sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() > dirs[j].Name() })
tableheader = "<th><a href='?key=filename&order=asc'>Filename</a></th><th><a href='?key=size'>Size</a></th><th><a href='?key=date'>Date</a></th>"
case (key[0] == "size" && order[0] == "asc"):
sort.Slice(dirs, func(i, j int) bool { return dirs[i].Size() < dirs[j].Size() })
tableheader = "<th><a href='?key=filename'>Filename</a></th><th><a href='?key=size&order=desc'>Size</a></th><th><a href='?key=date'>Date</a></th>"
case (key[0] == "size" && order[0] == "desc"):
sort.Slice(dirs, func(i, j int) bool { return dirs[i].Size() > dirs[j].Size() })
tableheader = "<th><a href='?key=filename'>Filename</a></th><th><a href='?key=size&order=asc'>Size</a></th><th><a href='?key=date'>Date</a></th>"
case (key[0] == "date" && order[0] == "asc"):
sort.Slice(dirs, func(i, j int) bool { return dirs[i].ModTime().UnixNano() < dirs[j].ModTime().UnixNano() })
tableheader = "<th><a href='?key=filename'>Filename</a></th><th><a href='?key=size'>Size</a></th><th><a href='?key=date&order=desc'>Date</a></th>"
case (key[0] == "date" && order[0] == "desc"):
sort.Slice(dirs, func(i, j int) bool { return dirs[i].ModTime().UnixNano() > dirs[j].ModTime().UnixNano() })
tableheader = "<th><a href='?key=filename'>Filename</a></th><th><a href='?key=size'>Size</a></th><th><a href='?key=date&order=asc'>Date</a></th>"
}
sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() }) sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() })
w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<table border='0'>\n<th>Filename</th><th>Size</th><th>Date</th></tr>")
fmt.Fprintf(w, "<table border='0'>%s\n", tableheader)
for _, d := range dirs { for _, d := range dirs {
name := d.Name() name := d.Name()
if d.IsDir() { if d.IsDir() {
@ -69,7 +107,7 @@ func dirList(w http.ResponseWriter, r *http.Request, f http.File) {
// part of the URL path, and not indicate the start of a query // part of the URL path, and not indicate the start of a query
// string or fragment. // string or fragment.
url := url.URL{Path: name} url := url.URL{Path: name}
fmt.Fprintf(w, "<tr><td><a href=\"%s\">%s</a></td><td>%d</td><td>%s</td></tr>", url.String(), htmlReplacer.Replace(name), d.Size(), d.ModTime().String()) fmt.Fprintf(w, "<tr><td><a href=\"%s\">%s</a></td><td>%d</td><td>%s</td></tr>", url.String(), htmlReplacer.Replace(name), d.Size(), d.ModTime().Format("2006-01-02 15:04:05"))
// fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name)) // fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name))
} }