Skip to content

Commit

Permalink
fix web attachment downloads by adding Content-Disposition header
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Apr 15, 2023
1 parent dda74cd commit 2072f03
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
7 changes: 5 additions & 2 deletions server/hdl_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"io"
"math/rand"
"net/http"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions server/media/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"mime"
"net/http"
"strconv"
"sync/atomic"
"time"

Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 2072f03

Please sign in to comment.