From 2072f031b98bc237ae5c427d2d4abd6844460a5f Mon Sep 17 00:00:00 2001 From: or-else Date: Fri, 14 Apr 2023 19:14:15 -0700 Subject: [PATCH] fix web attachment downloads by adding Content-Disposition header --- server/hdl_files.go | 7 +++++-- server/media/s3/s3.go | 12 +++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/server/hdl_files.go b/server/hdl_files.go index da41ac684..7d5362bd6 100644 --- a/server/hdl_files.go +++ b/server/hdl_files.go @@ -15,6 +15,7 @@ import ( "io" "math/rand" "net/http" + "strconv" "strings" "time" @@ -133,7 +134,9 @@ func largeFileServe(wrt http.ResponseWriter, req *http.Request) { defer rsc.Close() wrt.Header().Set("Content-Type", fd.MimeType) - wrt.Header().Set("Content-Disposition", "attachment") + if isAttachment, _ := strconv.ParseBool(req.URL.Query().Get("asatt")); isAttachment { + wrt.Header().Set("Content-Disposition", "attachment") + } http.ServeContent(wrt, req, "", fd.UpdatedAt, rsc) logs.Info.Println("media serve: OK, uid=", uid) @@ -265,7 +268,7 @@ func largeFileReceive(wrt http.ResponseWriter, req *http.Request) { } mimeType := http.DetectContentType(buff) - // If DetectContentType failed, use user-provided content type. + // If DetectContentType fails, use client-provided content type. if mimeType == "application/octet-stream" { if contentType := header.Header.Get("Content-Type"); contentType != "" { mimeType = contentType diff --git a/server/media/s3/s3.go b/server/media/s3/s3.go index ae61962f0..b99377445 100644 --- a/server/media/s3/s3.go +++ b/server/media/s3/s3.go @@ -7,6 +7,7 @@ import ( "io" "mime" "net/http" + "strconv" "sync/atomic" "time" @@ -172,10 +173,15 @@ func (ah *awshandler) Headers(req *http.Request, serve bool) (http.Header, int, var awsReq *request.Request if req.Method == http.MethodGet { + var contentDisposition *string + if isAttachment, _ := strconv.ParseBool(req.URL.Query().Get("asatt")); isAttachment { + contentDisposition = aws.String("attachment") + } awsReq, _ = ah.svc.GetObjectRequest(&s3.GetObjectInput{ - Bucket: aws.String(ah.conf.BucketName), - Key: aws.String(fid.String32()), - ResponseContentType: aws.String(fd.MimeType), + Bucket: aws.String(ah.conf.BucketName), + Key: aws.String(fid.String32()), + ResponseContentType: aws.String(fd.MimeType), + ResponseContentDisposition: contentDisposition, }) } else if req.Method == http.MethodHead { awsReq, _ = ah.svc.HeadObjectRequest(&s3.HeadObjectInput{