From 181ff7b0b1b9629cb5546d9c4c1b01775f2e4152 Mon Sep 17 00:00:00 2001 From: Equim Date: Sat, 13 Jan 2018 18:45:55 +0800 Subject: [PATCH] http: download: adopt http.ServeContent in inline mode --- http/download.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/http/download.go b/http/download.go index acf9773c..bfc8b6e4 100644 --- a/http/download.go +++ b/http/download.go @@ -1,7 +1,6 @@ package http import ( - "io" "net/http" "net/url" "os" @@ -86,24 +85,18 @@ func downloadFileHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) return http.StatusInternalServerError, err } - 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)) + if r.URL.Query().Get("inline") == "true" { + w.Header().Set("Content-Disposition", "inline") + } else { + // 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