Skip to content

Commit 69abdf3

Browse files
skaukmxpv
andauthored
Add MIME type when uploading to S3 (#778)
* Add MIME type when uploading to S3 Important when hosting files in a public bucket * Update dependencies * Run go mod tidy * Add error handling --------- Co-authored-by: Maksym Pavlenko <[email protected]>
1 parent 059514a commit 69abdf3

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/aws/aws-sdk-go v1.44.144
88
github.com/dgraph-io/badger v1.6.2
99
github.com/eduncan911/podcast v1.4.2
10+
github.com/gabriel-vasile/mimetype v1.4.10
1011
github.com/gilliek/go-opml v1.0.0
1112
github.com/golang/mock v1.6.0
1213
github.com/hashicorp/go-multierror v1.1.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ github.com/eduncan911/podcast v1.4.2/go.mod h1:mSxiK1z5KeNO0YFaQ3ElJlUZbbDV9dA7R
3636
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
3737
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
3838
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
39+
github.com/gabriel-vasile/mimetype v1.4.10 h1:zyueNbySn/z8mJZHLt6IPw0KoZsiQNszIpU+bX4+ZK0=
40+
github.com/gabriel-vasile/mimetype v1.4.10/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
3941
github.com/gilliek/go-opml v1.0.0 h1:X8xVjtySRXU/x6KvaiXkn7OV3a4DHqxY8Rpv6U/JvCY=
4042
github.com/gilliek/go-opml v1.0.0/go.mod h1:fOxmtlzyBvUjU6bjpdjyxCGlWz+pgtAHrHf/xRZl3lk=
4143
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=

pkg/fs/s3.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fs
22

33
import (
4+
"bytes"
45
"context"
56
"io"
67
"net/http"
@@ -13,6 +14,7 @@ import (
1314
"github.com/aws/aws-sdk-go/service/s3"
1415
"github.com/aws/aws-sdk-go/service/s3/s3iface"
1516
"github.com/aws/aws-sdk-go/service/s3/s3manager"
17+
"github.com/gabriel-vasile/mimetype"
1618
"github.com/pkg/errors"
1719
log "github.com/sirupsen/logrus"
1820
)
@@ -79,12 +81,23 @@ func (s *S3) Create(ctx context.Context, name string, reader io.Reader) (int64,
7981
key := s.buildKey(name)
8082
logger := log.WithField("key", key)
8183

84+
// Detect MIME type from the first 512 bytes and then replay them with the rest of the stream.
85+
buf := make([]byte, 512)
86+
n, err := io.ReadFull(reader, buf)
87+
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
88+
return 0, errors.Wrap(err, "failed to read file header for MIME detection")
89+
}
90+
head := buf[:n]
91+
m := mimetype.Detect(head)
92+
body := io.MultiReader(bytes.NewReader(head), reader)
93+
8294
logger.Infof("uploading file to %s", s.bucket)
83-
r := &readerWithN{Reader: reader}
84-
_, err := s.uploader.UploadWithContext(ctx, &s3manager.UploadInput{
85-
Bucket: &s.bucket,
86-
Key: &key,
87-
Body: r,
95+
r := &readerWithN{Reader: body}
96+
_, err = s.uploader.UploadWithContext(ctx, &s3manager.UploadInput{
97+
Body: r,
98+
Bucket: &s.bucket,
99+
ContentType: aws.String(m.String()),
100+
Key: &key,
88101
})
89102
if err != nil {
90103
return 0, errors.Wrap(err, "failed to upload file")

0 commit comments

Comments
 (0)