Skip to content

Commit 86a9e63

Browse files
committed
feat(serverHandler): add downloadfile param
1 parent ee53041 commit 86a9e63

File tree

5 files changed

+46
-26
lines changed

5 files changed

+46
-26
lines changed

doc/en-US/api.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ curl 'http://localhost/ghfs/?json'
3939
# Render page for downloading
4040
```
4141
GET <path>?download[&sort=key]
42+
GET <path>?downloadfile[&sort=key]
4243
```
4344
Similar to regular page rendering, but hide path list,
4445
sortable list header,
@@ -50,10 +51,14 @@ Example:
5051
wget --recursive -nc -nH -np 'http://localhost/dir/?download'
5152
```
5253

54+
Option `downloadfile` makes file links downloadable instead of displaying content.
55+
5356
# Download a file
54-
Notify user agent download a file rather than display its content.
57+
Notify user agent download a file rather than displaying its content,
58+
by outputting `Content-Disposition` header.
5559
```
5660
GET <path/to/file>?download
61+
GET <path/to/file>?downloadfile
5762
```
5863

5964
Example:

doc/zh-CN/api.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ curl 'http://localhost/ghfs/?json'
3939
# 显示用于下载的页面
4040
```
4141
GET <path>?download[&sort=key]
42+
GET <path>?downloadfile[&sort=key]
4243
```
4344
类似于常规显示的页面,但隐藏路径列表、可排序的表头和上级目录链接。
4445
这为“wget”之类的工具递归下载提供了方便。
@@ -48,10 +49,13 @@ GET <path>?download[&sort=key]
4849
wget --recursive -nc -nH -np 'http://localhost/dir/?download'
4950
```
5051

52+
选项`downloadfile`使文件链接可被下载,而不是显示其内容。
53+
5154
# 下载文件
52-
通知用户代理下载文件而不是显示其内容。
55+
通过输出`Content-Disposition`头,通知用户代理下载文件而不是显示其内容。
5356
```
5457
GET <path/to/file>?download
58+
GET <path/to/file>?downloadfile
5559
```
5660

5761
举例:

src/serverHandler/pathContext.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package serverHandler
22

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

910
func (ctx pathContext) QueryString() string {
10-
// ?download&sort=x/
11-
buffer := make([]byte, 1, 18)
11+
// ?downloadfile&sort=x/&
12+
buffer := make([]byte, 1, 22)
1213
buffer[0] = '?'
1314

14-
if ctx.download {
15+
switch {
16+
case ctx.downloadfile:
17+
buffer = append(buffer, []byte("downloadfile&")...) // 13 bytes
18+
case ctx.download:
1519
buffer = append(buffer, []byte("download&")...) // 9 bytes
1620
}
1721

@@ -26,8 +30,8 @@ func (ctx pathContext) QueryString() string {
2630
}
2731

2832
func (ctx pathContext) FileQueryString() string {
29-
if ctx.download {
30-
return "?download"
33+
if ctx.downloadfile {
34+
return "?downloadfile"
3135
}
3236

3337
return ""

src/serverHandler/responseData.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ type responseData struct {
3838

3939
Headers [][2]string
4040

41-
IsDownload bool
42-
IsUpload bool
43-
IsMkdir bool
44-
IsDelete bool
45-
IsMutate bool
46-
WantJson bool
41+
IsDownload bool
42+
IsDownloadFile bool
43+
IsUpload bool
44+
IsMkdir bool
45+
IsDelete bool
46+
IsMutate bool
47+
WantJson bool
4748

4849
CanUpload bool
4950
CanMkdir bool
@@ -322,11 +323,15 @@ func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsP
322323

323324
rawQuery := r.URL.RawQuery
324325
isDownload := false
326+
isDownloadFile := false
325327
isUpload := false
326328
isMkdir := false
327329
isDelete := false
328330
isMutate := false
329331
switch {
332+
case strings.HasPrefix(rawQuery, "downloadfile"):
333+
isDownload = true
334+
isDownloadFile = true
330335
case strings.HasPrefix(rawQuery, "download"):
331336
isDownload = true
332337
case strings.HasPrefix(rawQuery, "upload") && r.Method == http.MethodPost:
@@ -420,9 +425,10 @@ func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsP
420425
canCors := h.getCanCors(rawReqPath, reqFsPath)
421426

422427
context := pathContext{
423-
download: isDownload,
424-
sort: rawSortBy,
425-
defaultSort: h.defaultSort,
428+
download: isDownload,
429+
downloadfile: isDownloadFile,
430+
sort: rawSortBy,
431+
defaultSort: h.defaultSort,
426432
}
427433

428434
return &responseData{
@@ -439,12 +445,13 @@ func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsP
439445

440446
Headers: headers,
441447

442-
IsDownload: isDownload,
443-
IsUpload: isUpload,
444-
IsMkdir: isMkdir,
445-
IsDelete: isDelete,
446-
IsMutate: isMutate,
447-
WantJson: wantJson,
448+
IsDownload: isDownload,
449+
IsDownloadFile: isDownloadFile,
450+
IsUpload: isUpload,
451+
IsMkdir: isMkdir,
452+
IsDelete: isDelete,
453+
IsMutate: isMutate,
454+
WantJson: wantJson,
448455

449456
CanUpload: canUpload,
450457
CanMkdir: canMkdir,

src/tpl/frontend/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
{{end}}
9797
{{end}}
9898
<ul class="item-list{{if .HasDeletable}} has-deletable{{end}}">
99-
{{if not .IsDownload}}
99+
{{if not $isDownload}}
100100
<li class="header">{{$dirSort := .SortState.DirSort}}{{$sortKey := .SortState.Key}}
101101
<span class="detail">
102102
<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>

0 commit comments

Comments
 (0)