From f0c97314145805f09a049d0098e9f96e81d6c2b3 Mon Sep 17 00:00:00 2001
From: Ruaan <ruaan@uafrica.com>
Date: Mon, 21 Feb 2022 08:00:09 +0200
Subject: [PATCH] Add param to apply content disposition header to s3 downloads

---
 s3/s3.go | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/s3/s3.go b/s3/s3.go
index c086f68..e41a27c 100644
--- a/s3/s3.go
+++ b/s3/s3.go
@@ -28,9 +28,10 @@ type S3UploadResponse struct {
 }
 
 type S3UploadSettings struct {
-	MimeType          MIMEType
-	RetrieveSignedUrl bool
-	ExpiryDuration    *time.Duration
+	MimeType              MIMEType
+	RetrieveSignedUrl     bool
+	ExpiryDuration        *time.Duration
+	AddContentDisposition bool
 }
 
 type MIMEType string
@@ -121,7 +122,7 @@ func (s SessionWithHelpers) UploadWithSettings(data []byte, bucket, fileName str
 	}
 
 	if settings.RetrieveSignedUrl {
-		return s.GetSignedDownloadURL(bucket, fileName, 24*time.Hour)
+		return s.GetSignedDownloadURL(bucket, fileName, 24*time.Hour, settings.AddContentDisposition)
 	}
 
 	return "", nil
@@ -146,7 +147,7 @@ func (s SessionWithHelpers) UploadWith1DayExpiry(data []byte, bucket, fileName s
 }
 
 // GetSignedDownloadURL gets a signed download URL for the duration. If scv is nil, a new session will be created.
-func (s SessionWithHelpers) GetSignedDownloadURL(bucket string, fileName string, duration time.Duration) (string, error) {
+func (s SessionWithHelpers) GetSignedDownloadURL(bucket string, fileName string, duration time.Duration, addContentDisposition bool) (string, error) {
 	getInput := &s3.GetObjectInput{
 		Bucket: aws.String(bucket),
 		Key:    aws.String(fileName),
@@ -162,6 +163,10 @@ func (s SessionWithHelpers) GetSignedDownloadURL(bucket string, fileName string,
 		return "", errors.Error("File does not exist")
 	}
 
+	if addContentDisposition {
+		getRequest.HTTPRequest.Header.Set("content-disposition", "attachment; filename=\""+fileName+"\"")
+	}
+
 	return getRequest.Presign(duration)
 }
 
-- 
GitLab