added ?download file serve query param support to force file download

This commit is contained in:
Gani Georgiev
2023-07-20 15:04:26 +03:00
parent 7e0a4e61b4
commit 50d7df45eb
3 changed files with 42 additions and 3 deletions
+10 -1
View File
@@ -321,7 +321,14 @@ var manualExtensionContentTypes = map[string]string{
".css": "text/css", // (see https://github.com/gabriel-vasile/mimetype/pull/113)
}
// forceAttachmentParam is the name of the request query parameter to
// force "Content-Disposition: attachment" header.
const forceAttachmentParam = "download"
// Serve serves the file at fileKey location to an HTTP response.
//
// If the `download` query parameter is used the file will be always served for
// download no matter of its type (aka. with "Content-Disposition: attachment").
func (s *System) Serve(res http.ResponseWriter, req *http.Request, fileKey string, name string) error {
br, readErr := s.bucket.NewReader(s.ctx, fileKey, nil)
if readErr != nil {
@@ -329,9 +336,11 @@ func (s *System) Serve(res http.ResponseWriter, req *http.Request, fileKey strin
}
defer br.Close()
forceAttachment := req.URL.Query().Has(forceAttachmentParam)
disposition := "attachment"
realContentType := br.ContentType()
if list.ExistInSlice(realContentType, inlineServeContentTypes) {
if !forceAttachment && list.ExistInSlice(realContentType, inlineServeContentTypes) {
disposition = "inline"
}