Skip to content
Snippets Groups Projects
Commit 5ed549c8 authored by Daniel Naude's avatar Daniel Naude
Browse files

Update S3 upload functions to use MIME type detection based on file content instead of name

parent ce4f2c70
Branches
Tags v1.232.0
No related merge requests found
......@@ -5,9 +5,9 @@ import (
"context"
"encoding/binary"
"fmt"
"net/http"
"net/url"
"os"
"path"
"strings"
"time"
......@@ -142,7 +142,7 @@ func (s ClientWithHelpers) Upload(data []byte, bucket, fileName string, metadata
return nil, ErrorS3ClientNotEstablished
}
mimeType := getTypeForFilename(fileName)
mimeType := http.DetectContentType(data)
putInput := &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(fileName),
......@@ -168,7 +168,7 @@ func (s ClientWithHelpers) UploadWithSettings(data []byte, bucket, fileName stri
}
if settings.MimeType == "" {
settings.MimeType = getTypeForFilename(fileName)
settings.MimeType = MIMEType(http.DetectContentType(data))
}
putInput := &s3.PutObjectInput{
......@@ -242,7 +242,7 @@ func (s ClientWithHelpers) UploadWithSettingsRevised(data []byte, bucket string,
}
if settings.MimeType == "" {
settings.MimeType = getTypeForFilename(fullFileName)
settings.MimeType = MIMEType(http.DetectContentType(data))
}
putInput := &s3.PutObjectInput{
......@@ -302,7 +302,7 @@ func (s ClientWithHelpers) UploadWith1DayExpiry(data []byte, bucket, fileName st
}
if mimeType == "" {
mimeType = getTypeForFilename(fileName)
mimeType = MIMEType(http.DetectContentType(data))
}
expiry := 24 * time.Hour
......@@ -427,23 +427,6 @@ func (s ClientWithHelpers) UploadWithFileExtension(data []byte, bucket, filePref
return response, nil
}
func getTypeForFilename(f string) MIMEType {
ext := strings.ToLower(path.Ext(f))
switch ext {
case "pdf":
return MIMETypePDF
case "csv":
return MIMETypeCSV
case "zip":
return MIMETypeZIP
case "txt":
return MIMETypeText
}
return MIMETypeDefault
}
func (s ClientWithHelpers) GetObject(bucket string, fileName string, isDebug bool) (*s3.GetObjectOutput, error) {
if s.S3Client == nil {
return nil, ErrorS3ClientNotEstablished
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment