-
Notifications
You must be signed in to change notification settings - Fork 1
/
option.go
117 lines (103 loc) · 2.57 KB
/
option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package s3
import (
"context"
"encoding/json"
"fmt"
"time"
"reflect"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/fatih/structs"
"github.com/pkg/errors"
"github.com/rai-project/store"
)
type lifetimeKey struct{}
type expirationKey struct{}
type fileSizeLimitKey struct{}
type aclKey struct{}
type contentTypeKey struct{}
type sessionKey struct{}
type prefixKey struct{}
func NewOptions() *store.Options {
return &store.Options{
BaseURL: Config.BaseURL,
Bucket: Config.Bucket,
Context: context.Background(),
}
}
func toStringPtr(m interface{}) *string {
switch m := m.(type) {
case bool, int64, int32, int16, int8, int, float64, float32:
return toStringPtr(fmt.Sprint("%v", m))
case string:
return aws.String(m)
default:
buf, err := json.Marshal(m)
if err != nil {
return toStringPtr(fmt.Sprint("%v", m))
}
return aws.String(string(buf))
}
}
func toMapPtr(m interface{}) (map[string]*string, error) {
switch m := m.(type) {
case map[string]*string:
return m, nil
case map[string]string:
out := map[string]*string{}
for k, v := range m {
out[k] = toStringPtr(v)
}
return out, nil
case map[string]interface{}:
out := map[string]*string{}
for k, v := range m {
out[k] = toStringPtr(v)
}
return out, nil
default:
r := reflect.ValueOf(m)
if r.Kind() == reflect.Ptr {
r = r.Elem()
}
if r.Kind() != reflect.Struct {
return map[string]*string{}, errors.Errorf("input %v must be a map or a struct", m)
}
return toMapPtr(structs.Map(m))
}
}
func Expiration(t time.Time) store.UploadOption {
return func(o *store.UploadOptions) {
o.Context = context.WithValue(o.Context, expirationKey{}, t)
}
}
func Lifetime(t time.Duration) store.UploadOption {
return func(o *store.UploadOptions) {
o.Context = context.WithValue(o.Context, lifetimeKey{}, t)
}
}
func FileSizeLimit(i int64) store.UploadOption {
return func(o *store.UploadOptions) {
o.Context = context.WithValue(o.Context, fileSizeLimitKey{}, i)
}
}
func ContentType(s string) store.UploadOption {
return func(o *store.UploadOptions) {
o.Context = context.WithValue(o.Context, contentTypeKey{}, s)
}
}
func ACL(acl string) store.UploadOption {
return func(o *store.UploadOptions) {
o.Context = context.WithValue(o.Context, aclKey{}, acl)
}
}
func Session(s *session.Session) store.Option {
return func(o *store.Options) {
o.Context = context.WithValue(o.Context, sessionKey{}, s)
}
}
func Prefix(s string) store.ListOption {
return func(o *store.ListOptions) {
o.Context = context.WithValue(o.Context, prefixKey{}, s)
}
}