From 6b8365ab6ed5af833a069fbef1c600c12e6994d6 Mon Sep 17 00:00:00 2001 From: Zijing Zhang <50045289+pluveto@users.noreply.github.com> Date: Wed, 10 Jul 2024 11:57:20 +0800 Subject: [PATCH] feat(s3): detect and set proper mime type (#83) Co-authored-by: pluveto --- lib/s3/s3_uploader.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/s3/s3_uploader.go b/lib/s3/s3_uploader.go index 2338a10..1738d8d 100644 --- a/lib/s3/s3_uploader.go +++ b/lib/s3/s3_uploader.go @@ -1,6 +1,7 @@ package s3 import ( + "mime" "os" "path/filepath" "strings" @@ -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 }