From 4e1229e7c9bd5643ed079f3dafa25df4e426da56 Mon Sep 17 00:00:00 2001 From: Toby Allen Date: Sat, 25 Mar 2017 04:54:16 +0000 Subject: [PATCH] browse: sort listing by dir first (#1527) * Default Browse sort by Dir, File * Ignore temp template files * Add folder to test * unneeded chagne --- .gitignore | 4 +++- caddyhttp/browse/browse.go | 9 ++++++++- caddyhttp/browse/browse_test.go | 4 +++- caddyhttp/browse/testdata/photos/test1/test.html | 8 ++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 caddyhttp/browse/testdata/photos/test1/test.html diff --git a/.gitignore b/.gitignore index 4f3845ed4..46e04834c 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,6 @@ Caddyfile og_static/ -.vscode/ \ No newline at end of file +.vscode/ + +caddyhttp/browse/temp* \ No newline at end of file diff --git a/caddyhttp/browse/browse.go b/caddyhttp/browse/browse.go index 9da9b860d..9a7e8e1c6 100644 --- a/caddyhttp/browse/browse.go +++ b/caddyhttp/browse/browse.go @@ -137,7 +137,14 @@ func (l byName) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] // Treat upper and lower case equally func (l byName) Less(i, j int) bool { - return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name) + + // if both are dir or file sort normally + if l.Items[i].IsDir == l.Items[j].IsDir { + return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name) + } else { + // always sort dir ahead of file + return l.Items[i].IsDir + } } // By Size diff --git a/caddyhttp/browse/browse_test.go b/caddyhttp/browse/browse_test.go index 500c6e16c..2294d2cf3 100644 --- a/caddyhttp/browse/browse_test.go +++ b/caddyhttp/browse/browse_test.go @@ -181,6 +181,8 @@ func TestBrowseTemplate(t *testing.T) {

/photos/

+test1
+ test.html
test2.html
@@ -192,7 +194,7 @@ func TestBrowseTemplate(t *testing.T) { ` if respBody != expectedBody { - t.Fatalf("Expected body: %v got: %v", expectedBody, respBody) + t.Fatalf("Expected body: '%v' got: '%v'", expectedBody, respBody) } } diff --git a/caddyhttp/browse/testdata/photos/test1/test.html b/caddyhttp/browse/testdata/photos/test1/test.html new file mode 100644 index 000000000..40535a223 --- /dev/null +++ b/caddyhttp/browse/testdata/photos/test1/test.html @@ -0,0 +1,8 @@ + + + +Test + + + +