Don't try to index an empty slice
This commit is contained in:
parent
73ab6461ba
commit
a9b5198123
|
@ -57,37 +57,42 @@ func dirList(w http.ResponseWriter, r *http.Request, f http.File) {
|
||||||
http.Error(w, "Error reading directory", http.StatusInternalServerError)
|
http.Error(w, "Error reading directory", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
key := ""
|
||||||
// Check for sort parameters
|
// Check for sort parameters
|
||||||
key, ok := r.URL.Query()["key"]
|
k, ok := r.URL.Query()["key"]
|
||||||
if !ok || (key[0] != "filename" && key[0] != "date" && key[0] != "size") {
|
if !ok || (k[0] != "filename" && k[0] != "date" && k[0] != "size") {
|
||||||
key[0] = "filename"
|
key = "filename"
|
||||||
|
} else {
|
||||||
|
key = k[0]
|
||||||
}
|
}
|
||||||
|
order := ""
|
||||||
order, ok := r.URL.Query()["key"]
|
o, ok := r.URL.Query()["key"]
|
||||||
if !ok || (order[0] != "asc" && order[0] != "desc") {
|
if !ok || (order != "asc" && order != "desc") {
|
||||||
order[0] = "asc"
|
order = "asc"
|
||||||
|
} else {
|
||||||
|
order = o[0]
|
||||||
}
|
}
|
||||||
switch {
|
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() })
|
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>"
|
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() })
|
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>"
|
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() })
|
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>"
|
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() })
|
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>"
|
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() })
|
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>"
|
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() })
|
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>"
|
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>"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue