Skip to content

Commit 0247d38

Browse files
committed
feat(serverHandler): separate simple and download param
BREAKING CHANGE Before: `GET /dir?download` shows simple page. `GET /dir?downloadfile` shows simple page, and add download param to files. `GET /file?download` download a file instead of displaying its content. `GET /file?downloadfile` download a file instead of displaying its content. After: `GET /dir?simple` shows simple page. `GET /dir?download` add download param to files. `GET /dir?simpledownload` shows simple page, and add download param to files. `GET /file?download` download a file instead of displaying its content.
1 parent a1d2849 commit 0247d38

File tree

7 files changed

+75
-50
lines changed

7 files changed

+75
-50
lines changed

doc/en-US/api.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,31 @@ Example:
3737
curl -H 'Accept: application/json' 'http://localhost/ghfs/'
3838
```
3939

40-
# Render page for downloading
40+
# Render page in simple mode
4141
```
42-
GET <path>?download[&sort=key]
43-
GET <path>?downloadfile[&sort=key]
42+
GET <path>?simple[&sort=key]
4443
```
45-
Similar to regular page rendering, but hide path list,
46-
sortable list header,
47-
and parent directory link.
44+
Similar to regular page rendering, but
45+
hide path list, sortable list header, and parent directory link.
4846
It's convenient for tools like "wget" to download files recursively.
4947

5048
Example:
5149
```shell
5250
wget --recursive -nc -nH -np 'http://localhost/dir/?download'
5351
```
5452

55-
Option `downloadfile` makes file links downloadable instead of displaying content.
53+
# Add download param to file
54+
```
55+
GET <path>?download[&sort=key]
56+
```
57+
Add download parameter to file links in the page, which
58+
make them downloadable instead of displaying content.
5659

5760
# Download a file
5861
Notify user agent download a file rather than displaying its content,
5962
by outputting `Content-Disposition` header.
6063
```
6164
GET <path/to/file>?download
62-
GET <path/to/file>?downloadfile
6365
```
6466

6567
Example:

doc/zh-CN/api.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ Accept: application/json
3737
curl -H 'Accept: application/json' 'http://localhost/ghfs/'
3838
```
3939

40-
# 显示用于下载的页面
40+
# 显示精简页面
4141
```
42-
GET <path>?download[&sort=key]
43-
GET <path>?downloadfile[&sort=key]
42+
GET <path>?simple[&sort=key]
4443
```
4544
类似于常规显示的页面,但隐藏路径列表、可排序的表头和上级目录链接。
4645
这为“wget”之类的工具递归下载提供了方便。
@@ -50,13 +49,16 @@ GET <path>?downloadfile[&sort=key]
5049
wget --recursive -nc -nH -np 'http://localhost/dir/?download'
5150
```
5251

53-
选项`downloadfile`使文件链接可被下载,而不是显示其内容。
52+
# 为文件添加下载参数
53+
```
54+
GET <path>?download[&sort=key]
55+
```
56+
为页面中的文件链接添加下载参数,使其可被下载,而不是显示其内容。
5457

5558
# 下载文件
5659
通过输出`Content-Disposition`头,通知用户代理下载文件而不是显示其内容。
5760
```
5861
GET <path/to/file>?download
59-
GET <path/to/file>?downloadfile
6062
```
6163

6264
举例:

src/serverHandler/page.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func updateSubItemsHtml(data *responseData) {
2020
data.SubItemsHtml = make([]itemHtml, length)
2121

2222
dirSuffix := "/" + data.Context.QueryString()
23-
fileSuffix := data.Context.FileQueryString()
23+
fileSuffix := data.Context.SubFileQueryString()
2424

2525
for i, info := range data.SubItems {
2626
name := info.Name()

src/serverHandler/pathContext.go

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

33
type pathContext struct {
4-
download bool
5-
downloadfile bool
6-
sort *string // keep different for param is not specified or is empty
7-
defaultSort string
4+
simple bool
5+
download bool
6+
sort *string // keep different for param is not specified or is empty
7+
defaultSort string
88
}
99

1010
func (ctx pathContext) QueryString() string {
11-
// ?downloadfile&sort=x/&
12-
buffer := make([]byte, 1, 22)
11+
// ?simpledownload&sort=x/&
12+
buffer := make([]byte, 1, 24)
1313
buffer[0] = '?' // 1 byte
1414

1515
switch {
16-
case ctx.downloadfile:
17-
buffer = append(buffer, []byte("downloadfile&")...) // 13 bytes
16+
case ctx.simple && ctx.download:
17+
buffer = append(buffer, []byte("simpledownload&")...) // 15 bytes
18+
case ctx.simple:
19+
buffer = append(buffer, []byte("simple&")...) // 7 bytes
1820
case ctx.download:
1921
buffer = append(buffer, []byte("download&")...) // 9 bytes
2022
}
@@ -35,9 +37,9 @@ func (ctx pathContext) QueryStringOfSort(sort string) string {
3537
return copiedCtx.QueryString()
3638
}
3739

38-
func (ctx pathContext) FileQueryString() string {
39-
if ctx.downloadfile {
40-
return "?downloadfile"
40+
func (ctx pathContext) SubFileQueryString() string {
41+
if ctx.download {
42+
return "?download"
4143
}
4244

4345
return ""

src/serverHandler/pathContext_test.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
func TestPathContext(t *testing.T) {
88
var result string
99
var sort string
10-
var download bool
1110

1211
result = (&pathContext{}).QueryString()
1312
if result != "" {
@@ -38,29 +37,47 @@ func TestPathContext(t *testing.T) {
3837
t.Error(result)
3938
}
4039

41-
download = false
42-
result = (&pathContext{download: download}).QueryString()
40+
result = (&pathContext{simple: false}).QueryString()
4341
if result != "" {
4442
t.Error(result)
4543
}
4644

47-
download = false
45+
result = (&pathContext{simple: true}).QueryString()
46+
if result != "?simple" {
47+
t.Error(result)
48+
}
49+
50+
result = (&pathContext{download: true}).QueryString()
51+
if result != "?download" {
52+
t.Error(result)
53+
}
54+
55+
result = (&pathContext{simple: true, download: true}).QueryString()
56+
if result != "?simpledownload" {
57+
t.Error(result)
58+
}
59+
4860
sort = "/n"
49-
result = (&pathContext{download: download, sort: &sort}).QueryString()
61+
result = (&pathContext{simple: false, sort: &sort}).QueryString()
5062
if result != "?sort=/n" {
5163
t.Error(result)
5264
}
5365

54-
download = true
55-
result = (&pathContext{download: download}).QueryString()
56-
if result != "?download" {
66+
sort = "/n"
67+
result = (&pathContext{simple: true, sort: &sort}).QueryString()
68+
if result != "?simple&sort=/n" {
5769
t.Error(result)
5870
}
5971

60-
download = true
6172
sort = "/n"
62-
result = (&pathContext{download: download, sort: &sort}).QueryString()
73+
result = (&pathContext{download: true, sort: &sort}).QueryString()
6374
if result != "?download&sort=/n" {
6475
t.Error(result)
6576
}
77+
78+
sort = "/n"
79+
result = (&pathContext{simple: true, download: true, sort: &sort}).QueryString()
80+
if result != "?simpledownload&sort=/n" {
81+
t.Error(result)
82+
}
6683
}

src/serverHandler/sessionData.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ type sessionContext struct {
8686
type responseData struct {
8787
AuthUserName string
8888

89-
IsDownload bool
90-
IsDownloadFile bool
89+
IsSimple bool
90+
IsDownload bool
9191

9292
CanIndex bool
9393
CanUpload bool
@@ -368,16 +368,18 @@ func (h *aliasHandler) getSessionData(r *http.Request) (session *sessionContext,
368368

369369
headers := h.getHeaders(vhostReqPath, fsPath, authSuccess)
370370

371+
isSimple := false
371372
isDownload := false
372-
isDownloadFile := false
373373
isUpload := false
374374
isMkdir := false
375375
isDelete := false
376376
isMutate := false
377377
switch {
378-
case strings.HasPrefix(rawQuery, "downloadfile"):
378+
case strings.HasPrefix(rawQuery, "simpledownload"):
379+
isSimple = true
379380
isDownload = true
380-
isDownloadFile = true
381+
case strings.HasPrefix(rawQuery, "simple"):
382+
isSimple = true
381383
case strings.HasPrefix(rawQuery, "download"):
382384
isDownload = true
383385
case strings.HasPrefix(rawQuery, "upload") && r.Method == http.MethodPost:
@@ -537,8 +539,8 @@ func (h *aliasHandler) getSessionData(r *http.Request) (session *sessionContext,
537539
data = &responseData{
538540
AuthUserName: authUserName,
539541

540-
IsDownload: isDownload,
541-
IsDownloadFile: isDownloadFile,
542+
IsSimple: isSimple,
543+
IsDownload: isDownload,
542544

543545
CanIndex: canIndex,
544546
CanUpload: canUpload,
@@ -564,10 +566,10 @@ func (h *aliasHandler) getSessionData(r *http.Request) (session *sessionContext,
564566
SubItemPrefix: subItemPrefix,
565567
SortState: sortState,
566568
Context: pathContext{
567-
download: isDownload,
568-
downloadfile: isDownloadFile,
569-
sort: rawSortBy,
570-
defaultSort: h.defaultSort,
569+
simple: isSimple,
570+
download: isDownload,
571+
sort: rawSortBy,
572+
defaultSort: h.defaultSort,
571573
},
572574
}
573575
return

src/tpl/defaultTheme/frontend/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
</head>
1313
<body class="{{if .IsRoot}}root-dir{{else}}sub-dir{{end}}">
1414
{{$contextQueryString := .Context.QueryString}}
15-
{{$isDownload := .IsDownload}}
15+
{{$isSimple := .IsSimple}}
1616
{{$SubItemPrefix := .SubItemPrefix}}
17-
{{if not $isDownload}}
17+
{{if not $isSimple}}
1818
<ol class="path-list" translate="no">
1919
{{range .Paths}}
2020
<li><a href="{{.Path}}{{$contextQueryString}}">{{fmtFilename .Name}}</a></li>
@@ -98,7 +98,7 @@
9898
</div>
9999
{{end}}
100100
<ul class="item-list{{if .HasDeletable}} has-deletable{{end}}">
101-
{{if not $isDownload}}
101+
{{if not $isSimple}}
102102
<li class="header">{{$dirSort := .SortState.DirSort}}{{$sortKey := .SortState.Key}}
103103
<span class="detail">
104104
<a class="field dir" href="{{.SubItemPrefix}}{{.Context.QueryStringOfSort .SortState.NextDirSort}}">{{.Trans.ListDirLabel}}{{if eq $dirSort -1}}&uarr;{{else if eq $dirSort 1}}&darr;{{end}}</a>
@@ -123,7 +123,7 @@
123123
<span class="field size">{{.DisplaySize}}</span>
124124
<span class="field time">{{.DisplayTime}}</span>
125125
</a>
126-
{{if and (not $isDownload) .DeleteUrl}}<form class="delete" method="post" action="{{$SubItemPrefix}}?delete" onsubmit="return confirmDelete(this)"><input type="hidden" name="name" value="{{.DeleteUrl}}"/><input type="hidden" name="contextquerystring" value="{{$contextQueryString}}"/><button type="submit">x</button></form>{{end}}
126+
{{if and (not $isSimple) .DeleteUrl}}<form class="delete" method="post" action="{{$SubItemPrefix}}?delete" onsubmit="return confirmDelete(this)"><input type="hidden" name="name" value="{{.DeleteUrl}}"/><input type="hidden" name="contextquerystring" value="{{$contextQueryString}}"/><button type="submit">x</button></form>{{end}}
127127
</li>
128128
{{end}}
129129
</ul>

0 commit comments

Comments
 (0)