From a369ddfc8deb0b5d6a75650c7ccbbd7d770521a9 Mon Sep 17 00:00:00 2001 From: Doug Keller Date: Thu, 5 Dec 2024 09:43:43 +0100 Subject: [PATCH] sorted directories and files for static file serving --- middleware/static.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/middleware/static.go b/middleware/static.go index 1016f1b09..08c69391b 100644 --- a/middleware/static.go +++ b/middleware/static.go @@ -6,6 +6,7 @@ package middleware import ( "errors" "fmt" + "sort" "html/template" "net/http" "net/url" @@ -50,6 +51,12 @@ type StaticConfig struct { Filesystem http.FileSystem `yaml:"-"` } +// Used for sorting directories in listDir +type byName []os.FileInfo +func (s byName) Len() int { return len(s) } +func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() } +func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] } + const html = ` @@ -251,6 +258,9 @@ func listDir(t *template.Template, name string, dir http.File, res *echo.Respons return } + // Sort directories first + sort.Sort(byName(files)) + // Create directory index res.Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8) data := struct {