feat(web): add file sorting to files browser

This commit is contained in:
2026-08-31 05:57:35 +02:00
parent 995441e917
commit ea917b4148
6 changed files with 419 additions and 170 deletions
+45 -1
View File
@@ -211,6 +211,16 @@ func (s *Server) handleFilesGet(w http.ResponseWriter, r *http.Request, username
return
}
// Determine sort order (name asc/desc) from query param, default to asc
sortBy := r.URL.Query().Get("sort_by")
sortDir := r.URL.Query().Get("sort_dir")
if sortBy == "" {
sortBy = "name"
}
if sortDir == "" {
sortDir = "asc"
}
// os.ReadDir already returns entries sorted by filename, so filtering
// into two passes keeps each group (directories, then files)
// alphabetically sorted while grouping directories first.
@@ -234,6 +244,40 @@ func (s *Server) handleFilesGet(w http.ResponseWriter, r *http.Request, username
files = append(files, entry)
}
}
// Sort entries based on sortBy and sortDir
less := func(i, j templates.FileEntry) bool {
switch sortBy {
case "name":
if sortDir == "desc" {
return i.Name > j.Name
}
return i.Name < j.Name
default:
if sortDir == "desc" {
return i.ModTime > j.ModTime
}
return i.ModTime < j.ModTime
}
}
// Sort dirs
for i := 0; i < len(dirs)-1; i++ {
for j := i + 1; j < len(dirs); j++ {
if less(dirs[j], dirs[i]) {
dirs[i], dirs[j] = dirs[j], dirs[i]
}
}
}
// Sort files
for i := 0; i < len(files)-1; i++ {
for j := i + 1; j < len(files); j++ {
if less(files[j], files[i]) {
files[i], files[j] = files[j], files[i]
}
}
}
entries := append(dirs, files...)
var breadcrumbs []templates.Breadcrumb
@@ -248,7 +292,7 @@ func (s *Server) handleFilesGet(w http.ResponseWriter, r *http.Request, username
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_ = templates.FilesPage(username, breadcrumbs, entries, relPath).Render(context.Background(), w)
_ = templates.FilesPage(username, breadcrumbs, entries, relPath, sortBy, sortDir).Render(context.Background(), w)
}
func (s *Server) handleFilesUpload(w http.ResponseWriter, r *http.Request, root, fullPath string) {