Skip to content

Commit d6543e3

Browse files
committed
feat(serverHandler): omit url parameter for default sort
1 parent 500f08d commit d6543e3

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

src/serverHandler/pathContext.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package serverHandler
22

33
type pathContext struct {
4-
sort string
4+
defaultSort string
5+
sort *string
56
}
67

78
func (ctx *pathContext) QueryString() string {
8-
if len(ctx.sort) > 0 {
9-
return "?sort=" + ctx.sort
9+
if ctx.sort != nil && *(ctx.sort) != ctx.defaultSort {
10+
return "?sort=" + *(ctx.sort)
1011
} else {
1112
return ""
1213
}
1314
}
1415

1516
func (ctx *pathContext) QueryStringOfSort(sort string) string {
1617
copiedCtx := *ctx
17-
copiedCtx.sort = sort
18+
copiedCtx.sort = &sort
1819
return copiedCtx.QueryString()
1920
}

src/serverHandler/pathContext_test.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,39 @@ import (
66

77
func TestPathContext(t *testing.T) {
88
var result string
9+
var sort string
910

10-
ctx := &pathContext{}
11-
result = ctx.QueryString()
11+
result = (&pathContext{}).QueryString()
1212
if result != "" {
1313
t.Error(result)
1414
}
1515

16-
ctx.sort = "/n"
17-
result = ctx.QueryString()
18-
if result != "?sort=/n" {
16+
result = (&pathContext{defaultSort: "/n"}).QueryString()
17+
if result != "" {
18+
t.Error(result)
19+
}
20+
21+
sort = ""
22+
result = (&pathContext{defaultSort: "/n", sort: &sort}).QueryString()
23+
if result != "?sort=" {
24+
t.Error(result)
25+
}
26+
27+
sort = "/n"
28+
result = (&pathContext{defaultSort: "/n", sort: &sort}).QueryString()
29+
if result != "" {
30+
t.Error(result)
31+
}
32+
33+
sort = ""
34+
result = (&pathContext{sort: &sort}).QueryString()
35+
if result != "" {
1936
t.Error(result)
2037
}
2138

39+
sort = "/n"
40+
result = (&pathContext{sort: &sort}).QueryString()
41+
if result != "?sort=/n" {
42+
t.Error(result)
43+
}
2244
}

src/serverHandler/responseData.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,10 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
340340

341341
subItemPrefix := getSubItemPrefix(rawReqPath, tailSlash)
342342

343-
context := &pathContext{sort: rawSortBy}
343+
context := &pathContext{
344+
defaultSort: h.defaultSort,
345+
sort: rawSortBy,
346+
}
344347

345348
canUpload := h.getCanUpload(item, rawReqPath, reqFsPath)
346349
canMkdir := h.getCanMkdir(item, rawReqPath, reqFsPath)

src/serverHandler/sort.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,21 +191,23 @@ func sortSubItemOriginal(subInfos []os.FileInfo, compareDir fnCompareDir) {
191191
return i < j
192192
})
193193
}
194-
func sortSubItems(subInfos []os.FileInfo, rawQuery string, defaultSortBy string) (rawSortBy string, sortInfo SortState) {
194+
func sortSubItems(subInfos []os.FileInfo, rawQuery string, defaultSortBy string) (rawSortBy *string, sortInfo SortState) {
195195
const sortPrefix = "sort="
196196
var sortBy string
197197

198198
// extract sort string
199199
iSortBy := strings.Index(rawQuery, sortPrefix)
200200
if iSortBy < 0 {
201201
sortBy = defaultSortBy
202-
} else if len(rawQuery) > iSortBy+len(sortPrefix) {
203-
sortBy = rawQuery[iSortBy+len(sortPrefix):]
204-
iAmp := strings.IndexByte(sortBy, '&')
205-
if iAmp >= 0 {
206-
sortBy = sortBy[:iAmp]
202+
} else {
203+
if len(rawQuery) > iSortBy+len(sortPrefix) {
204+
sortBy = rawQuery[iSortBy+len(sortPrefix):]
205+
iAmp := strings.IndexByte(sortBy, '&')
206+
if iAmp >= 0 {
207+
sortBy = sortBy[:iAmp]
208+
}
207209
}
208-
rawSortBy = sortBy
210+
rawSortBy = &sortBy
209211
}
210212

211213
if len(sortBy) == 0 {

0 commit comments

Comments
 (0)