Skip to content

Commit

Permalink
Merge branch 'master' into otgorm-more-driver
Browse files Browse the repository at this point in the history
  • Loading branch information
Reasno authored Jun 4, 2021
2 parents 9da5675 + 0a8a387 commit 39277f8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
69 changes: 41 additions & 28 deletions ots3/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,29 @@ import (

// Manager manages S3 uploads.
type Manager struct {
bucket string
sess *session.Session
tracer opentracing.Tracer
doer contract.HttpDoer
pathPrefix string
keyer contract.Keyer
locationFunc func(location string) (url string)
bucket string
sess *session.Session
tracer opentracing.Tracer
doer contract.HttpDoer
pathPrefix string
keyer contract.Keyer
locationFunc func(location string) (url string)
autoExtension bool
}

// Config contains a various of configurations for Manager. It is mean to be modified by Option.
type Config struct {
accessKey string
accessSecret string
region string
bucket string
sess *session.Session
tracer opentracing.Tracer
doer contract.HttpDoer
keyer contract.Keyer
pathPrefix string
locationFunc func(location string) (url string)
accessKey string
accessSecret string
region string
bucket string
sess *session.Session
tracer opentracing.Tracer
doer contract.HttpDoer
keyer contract.Keyer
pathPrefix string
locationFunc func(location string) (url string)
autoExtension bool
}

// Option is the type of functional options to alter Config.
Expand Down Expand Up @@ -86,6 +88,13 @@ func WithLocationFunc(f func(location string) (url string)) Option {
}
}

// WithAutoExtension is an option that auto splice extension, default is true.
func WithAutoExtension(auto bool) Option {
return func(c *Config) {
c.autoExtension = auto
}
}

// NewManager creates a new S3 manager
func NewManager(accessKey, accessSecret, endpoint, region, bucket string, opts ...Option) *Manager {
c := &Config{
Expand All @@ -94,6 +103,7 @@ func NewManager(accessKey, accessSecret, endpoint, region, bucket string, opts .
locationFunc: func(location string) (url string) {
return location
},
autoExtension: true,
}
for _, f := range opts {
f(c)
Expand All @@ -109,13 +119,14 @@ func NewManager(accessKey, accessSecret, endpoint, region, bucket string, opts .
sess := session.Must(session.NewSession(s3Config))
c.keyer.Key("/")
m := &Manager{
bucket: bucket,
sess: sess,
tracer: c.tracer,
doer: c.doer,
pathPrefix: c.pathPrefix,
keyer: c.keyer,
locationFunc: c.locationFunc,
bucket: bucket,
sess: sess,
tracer: c.tracer,
doer: c.doer,
pathPrefix: c.pathPrefix,
keyer: c.keyer,
locationFunc: c.locationFunc,
autoExtension: c.autoExtension,
}

// add opentracing capabilities if opt in
Expand All @@ -133,10 +144,12 @@ func (m *Manager) Upload(ctx context.Context, name string, reader io.Reader) (ne
uploader := s3manager.NewUploader(m.sess)
var extension = ""
var buf = bytes.NewBuffer(nil)
var tee = io.TeeReader(reader, buf)
mi, err := mimetype.DetectReader(tee)
if err == nil {
extension = mi.Extension()
if m.autoExtension {
var tee = io.TeeReader(reader, buf)
mi, err := mimetype.DetectReader(tee)
if err == nil {
extension = mi.Extension()
}
}

k := key.KeepOdd(m.keyer).Key("/", name+extension)
Expand Down
1 change: 1 addition & 0 deletions ots3/uploader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestNewManager(t *testing.T) {
return ""
}),
WithKeyer(key.New()),
WithAutoExtension(true),
))
}

Expand Down

0 comments on commit 39277f8

Please sign in to comment.