Skip to content

Commit f3c8a18

Browse files
authored
Merge pull request #9 from fumeapp/fileheader-fixes
💥 breaking changes to Upload for better file header support
2 parents 7a4f21d + a376c73 commit f3c8a18

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
PACKAGE=tonic
2+
3+
ray:
4+
@go get github.com/octoper/go-ray
5+
6+
7+
unray:
8+
@go mod tidy -e github.com/octoper/go-ray

aws/aws.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func UploadURL(url string) (string, error) {
7777
return "", err
7878
}
7979

80-
return Upload(bodyBytes)
80+
return Upload(bodyBytes, "")
8181
}
8282

8383
// UploadFile - same functionality as UploadURL but take in a multipart.FileHeader
@@ -90,16 +90,20 @@ func UploadFile(fileHeader *multipart.FileHeader) (string, error) {
9090
if err != nil {
9191
return "", err
9292
}
93-
return Upload(bodyBytes)
93+
return Upload(bodyBytes, fileHeader.Header.Get("Content-Type"))
9494
}
9595

9696
// Upload
9797
// Uploads a file to S3 naming it after a hash of the file contents.
9898
// Accepts a public URL
9999
// returns the URL of the uploaded file and an error if there was one.
100-
func Upload(bodyBytes []byte) (string, error) {
100+
func Upload(bodyBytes []byte, contentType string) (string, error) {
101101

102-
extension, contentType, err := getExtension(bodyBytes)
102+
if contentType == "" {
103+
contentType = http.DetectContentType(bodyBytes)
104+
}
105+
106+
extension, err := getExtension(contentType)
103107
if err != nil {
104108
return "", err
105109
}
@@ -120,10 +124,9 @@ func Upload(bodyBytes []byte) (string, error) {
120124
}
121125

122126
// Figure out file extension and content type
123-
func getExtension(bytes []byte) (string, string, error) {
127+
func getExtension(contentType string) (string, error) {
124128

125129
var extension string
126-
contentType := http.DetectContentType(bytes)
127130

128131
switch contentType {
129132
case "image/jpg":
@@ -149,10 +152,10 @@ func getExtension(bytes []byte) (string, string, error) {
149152
case "application/csv":
150153
extension = "csv"
151154
default:
152-
return "", "", errors.New("unable to detect Content Type: " + contentType)
155+
return "", errors.New("unable to detect Content Type: " + contentType)
153156
}
154157

155-
return extension, contentType, nil
158+
return extension, nil
156159
}
157160

158161
func VerifyEmail(to string) (*ses.VerifyEmailIdentityOutput, error) {

0 commit comments

Comments
 (0)