Skip to content

Commit

Permalink
feat(s3): detect and set proper mime type (#83)
Browse files Browse the repository at this point in the history
Co-authored-by: pluveto <pluvet@foxmail.com>
  • Loading branch information
pluveto and pluveto authored Jul 10, 2024
1 parent 2d99c81 commit 6b8365a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/s3/s3_uploader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package s3

import (
"mime"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -73,10 +74,18 @@ func (u *S3Uploader) PutFile(localPath, targetPath string) error {
}
defer file.Close()

// Detect the MIME type based on the file extension
ext := filepath.Ext(localPath)
mimeType := mime.TypeByExtension(ext)
if mimeType == "" {
mimeType = "application/octet-stream" // Default to binary/octet-stream if detection fails
}

_, err = u.s3Client.PutObject(&s3.PutObjectInput{
Bucket: aws.String(u.Config.BucketName),
Key: aws.String(targetPath),
Body: file,
Bucket: aws.String(u.Config.BucketName),
Key: aws.String(targetPath),
Body: file,
ContentType: aws.String(mimeType),
})
return err
}
Expand Down

0 comments on commit 6b8365a

Please sign in to comment.