From fe6bd4ce08c9f581295bc3a3953e6d0cd83ee134 Mon Sep 17 00:00:00 2001 From: Equim Date: Sat, 13 Jan 2018 18:26:12 +0800 Subject: [PATCH] http: download: adopt http.ServeContent --- http/download.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/http/download.go b/http/download.go index 5ec116a2..32f2860c 100644 --- a/http/download.go +++ b/http/download.go @@ -99,18 +99,12 @@ func downloadHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int } defer file.Close() - w.Header().Set("Content-Disposition", "attachment; filename=\""+name+"\"") + w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+url.QueryEscape(name)) _, err = io.Copy(w, file) return 0, err } func downloadFileHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) { - if r.URL.Query().Get("inline") == "true" { - w.Header().Set("Content-Disposition", "inline") - } else { - w.Header().Set("Content-Disposition", `attachment; filename="`+c.File.Name+`"`) - } - file, err := os.Open(c.File.Path) defer file.Close() @@ -118,10 +112,25 @@ func downloadFileHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) return http.StatusInternalServerError, err } - _, err = io.Copy(w, file) + if r.URL.Query().Get("inline") == "true" { + w.Header().Set("Content-Disposition", "inline") + + _, err = io.Copy(w, file) + if err != nil { + return http.StatusInternalServerError, err + } + + return 0, nil + } + + stat, err := file.Stat() if err != nil { return http.StatusInternalServerError, err } + // As per RFC6266 section 4.3 + w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+url.QueryEscape(c.File.Name)) + http.ServeContent(w, r, stat.Name(), stat.ModTime(), file) + return 0, nil }