Don't try to index an empty slice

This commit is contained in:
Your Name 2018-09-02 19:17:34 +00:00
parent 73ab6461ba
commit a9b5198123
1 changed files with 18 additions and 13 deletions

View File

@ -57,37 +57,42 @@ func dirList(w http.ResponseWriter, r *http.Request, f http.File) {
http.Error(w, "Error reading directory", http.StatusInternalServerError)
return
}
key := ""
// Check for sort parameters
key, ok := r.URL.Query()["key"]
if !ok || (key[0] != "filename" && key[0] != "date" && key[0] != "size") {
key[0] = "filename"
k, ok := r.URL.Query()["key"]
if !ok || (k[0] != "filename" && k[0] != "date" && k[0] != "size") {
key = "filename"
} else {
key = k[0]
}
order, ok := r.URL.Query()["key"]
if !ok || (order[0] != "asc" && order[0] != "desc") {
order[0] = "asc"
order := ""
o, ok := r.URL.Query()["key"]
if !ok || (order != "asc" && order != "desc") {
order = "asc"
} else {
order = o[0]
}
switch {
case (key[0] == "filename" && order[0] == "asc"):
case (key == "filename" && order == "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"):
case (key == "filename" && order == "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"):
case (key == "size" && order == "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"):
case (key == "size" && order == "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"):
case (key == "date" && order == "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"):
case (key == "date" && order == "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>"