Skip to content

Commit

Permalink
Merge pull request #9 from fumeapp/fileheader-fixes
Browse files Browse the repository at this point in the history
💥 breaking changes to Upload for better file header support
  • Loading branch information
acidjazz authored Aug 14, 2024
2 parents 7a4f21d + a376c73 commit f3c8a18
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PACKAGE=tonic

ray:
@go get github.com/octoper/go-ray


unray:
@go mod tidy -e github.com/octoper/go-ray
19 changes: 11 additions & 8 deletions aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func UploadURL(url string) (string, error) {
return "", err
}

return Upload(bodyBytes)
return Upload(bodyBytes, "")
}

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

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

extension, contentType, err := getExtension(bodyBytes)
if contentType == "" {
contentType = http.DetectContentType(bodyBytes)
}

extension, err := getExtension(contentType)
if err != nil {
return "", err
}
Expand All @@ -120,10 +124,9 @@ func Upload(bodyBytes []byte) (string, error) {
}

// Figure out file extension and content type
func getExtension(bytes []byte) (string, string, error) {
func getExtension(contentType string) (string, error) {

var extension string
contentType := http.DetectContentType(bytes)

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

return extension, contentType, nil
return extension, nil
}

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

0 comments on commit f3c8a18

Please sign in to comment.