Skip to content

Commit a32aa56

Browse files
committed
feat(serverHandler): add download action
1 parent 0e852ca commit a32aa56

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

doc/en-US/api.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ Example:
3636
curl http://localhost/ghfs/?json
3737
```
3838

39+
# Download a file
40+
Notify user agent download a file rather than display its content.
41+
```
42+
GET <path/to/file>?download
43+
```
44+
45+
Example:
46+
```sh
47+
curl http://localhost/ghfs/file?download
48+
```
49+
3950
# Get contents of specified path as archive file
4051
Only work when "archive" is enabled.
4152
```

doc/zh-CN/api.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ GET <path>?json[&sort=key]
3636
curl http://localhost/ghfs/?json
3737
```
3838

39+
# 下载文件
40+
通知用户代理下载文件而不是显示其内容。
41+
```
42+
GET <path/to/file>?download
43+
```
44+
45+
举例:
46+
```sh
47+
curl http://localhost/ghfs/file?download
48+
```
49+
3950
# 以打包文件形式获取指定路径下的内容
4051
仅在“archive”选项启用时有效。
4152
```

src/serverHandler/content.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ func getContentType(item os.FileInfo, file *os.File) (string, error) {
2727
}
2828

2929
func (h *handler) content(w http.ResponseWriter, r *http.Request, data *responseData) {
30+
header := w.Header()
31+
if data.IsDownload {
32+
header.Set("Content-Disposition", "attachment; filename*=UTF-8''"+data.ItemName)
33+
}
34+
3035
item := data.Item
3136
file := data.File
3237

@@ -37,7 +42,6 @@ func (h *handler) content(w http.ResponseWriter, r *http.Request, data *response
3742

3843
ctype, err := getContentType(item, file)
3944
if err == nil {
40-
header := w.Header()
4145
header.Set("Content-Type", ctype)
4246
header.Set("Content-Length", strconv.FormatInt(item.Size(), 10))
4347
header.Set("Date", time.Now().UTC().Format(http.TimeFormat))

src/serverHandler/responseData.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ type responseData struct {
5353
CanCors bool
5454
NeedAuth bool
5555

56-
IsUpload bool
57-
IsMkdir bool
58-
IsDelete bool
59-
IsMutate bool
60-
WantJson bool
56+
IsDownload bool
57+
IsUpload bool
58+
IsMkdir bool
59+
IsDelete bool
60+
IsMutate bool
61+
WantJson bool
6162
}
6263

6364
func isSlash(c rune) bool {
@@ -353,11 +354,14 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
353354
canCors := h.getCanCors(rawReqPath, reqFsPath)
354355
needAuth := h.getNeedAuth(rawReqPath, reqFsPath)
355356

357+
isDownload := false
356358
isUpload := false
357359
isMkdir := false
358360
isDelete := false
359361
isMutate := false
360362
switch {
363+
case strings.HasPrefix(rawQuery, "download"):
364+
isDownload = true
361365
case strings.HasPrefix(rawQuery, "upload") && r.Method == http.MethodPost:
362366
isUpload = true
363367
isMutate = true
@@ -399,10 +403,11 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
399403
CanCors: canCors,
400404
NeedAuth: needAuth,
401405

402-
IsUpload: isUpload,
403-
IsMkdir: isMkdir,
404-
IsDelete: isDelete,
405-
IsMutate: isMutate,
406-
WantJson: wantJson,
406+
IsDownload: isDownload,
407+
IsUpload: isUpload,
408+
IsMkdir: isMkdir,
409+
IsDelete: isDelete,
410+
IsMutate: isMutate,
411+
WantJson: wantJson,
407412
}
408413
}

0 commit comments

Comments
 (0)