-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmps3.go
310 lines (266 loc) · 7.73 KB
/
mps3.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package mps3
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log"
"mime"
"mime/multipart"
"net/http"
"net/url"
"path/filepath"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/google/uuid"
"github.com/h2non/filetype"
)
type Logger interface {
Printf(format string, args ...any)
}
type Config struct {
// S3Config specifies credentials and endpoint configuration. If not specified the middleware
// will load the default configuration with a background context.
//
// To provide a custom endpoint (required when not using AWS S3 API) you can do something like this
// (more info at https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/endpoints/):
//
// resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
// if service == s3.ServiceID {
// return aws.Endpoint{
// URL: "http://localhost:9000",
// SigningRegion: "eu-central-1",
// HostnameImmutable: true,
// }, nil
// }
// // returning EndpointNotFoundError will allow the service to fallback to it's default resolution
// return aws.Endpoint{}, &aws.EndpointNotFoundError{}
// })
//
// s3cfg, err := config.LoadDefaultConfig(context.Background(), config.WithEndpointResolverWithOptions(resolver))
S3Config *aws.Config
// Bucket name of the bucket to use to store uploaded files
Bucket string
// BucketACL if CreateBucket is true the bucket will be created with this ACL (default: "private")
BucketACL string
// CreateBucket if true it will try to create a bucket with the specified Bucket name.
// Error of type BucketAlreadyOwnedByYou will be silently ignored (default: true)
CreateBucket bool
// FileACL defines ACL string to use for uploaded files (default: "private")
FileACL string
// PartSize defines the size of the chunk that is uploaded to S3, by default is 5 MB,
// which is the minimum part size. If a value smaller than the minimum is set, it
// will be silently adjusted to the minimum.
PartSize int64
// PrefixFunc defines a function that gets executed to define the S3 key prefix
// for each uploaded file. By default it's a function that returns the current date
// in the format `/YYYY/MM/DD/`
PrefixFunc func(*http.Request) string
// Logger is used to log errors during request processing (default: log.Default())
Logger Logger
}
type Wrapper struct {
uploader *manager.Uploader
logger Logger
bucket string
fileACL string
prefixFunc func(*http.Request) string
}
type file struct {
name string
ftype string
key string
size int64
}
func New(cfg Config) (*Wrapper, error) {
if cfg.S3Config == nil {
s3cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to create S3 configuration: %w", err)
}
cfg.S3Config = &s3cfg
}
cli := s3.NewFromConfig(*cfg.S3Config)
if cfg.Bucket == "" {
return nil, fmt.Errorf("bucket name is required")
}
if cfg.CreateBucket {
if cfg.BucketACL == "" {
cfg.BucketACL = "private"
}
if err := createBucket(cli, cfg.Bucket, cfg.BucketACL); err != nil {
return nil, err
}
}
if cfg.PartSize < manager.MinUploadPartSize {
cfg.PartSize = manager.MinUploadPartSize
}
w := Wrapper{
uploader: manager.NewUploader(cli, func(u *manager.Uploader) {
u.PartSize = cfg.PartSize
}),
logger: cfg.Logger,
bucket: cfg.Bucket,
fileACL: cfg.FileACL,
prefixFunc: cfg.PrefixFunc,
}
if w.logger == nil {
w.logger = log.Default()
}
if w.fileACL == "" {
w.fileACL = "private"
}
if w.prefixFunc == nil {
w.prefixFunc = func(*http.Request) string {
return time.Now().UTC().Format("/2006/01/02/")
}
}
return &w, nil
}
func (wr Wrapper) Wrap(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if !strings.HasPrefix(req.Header.Get("Content-Type"), "multipart/form-data") {
next.ServeHTTP(w, req)
return
}
mr, err := req.MultipartReader()
if err != nil {
wr.logAndErr(w, fmt.Errorf("failed create multipart reader: %w", err))
return
}
f := make(url.Values)
for {
part, err := mr.NextPart()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
wr.logAndErr(w, fmt.Errorf("failed to read request part: %w", err))
return
}
if err := wr.readPart(req, part, f); err != nil {
wr.logAndErr(w, err)
return
}
}
if req.Form == nil {
req.Form = make(url.Values)
}
if req.PostForm == nil {
req.PostForm = make(url.Values)
}
for k, v := range f {
req.PostForm[k] = append(req.PostForm[k], v...)
req.Form[k] = append(req.Form[k], v...)
}
next.ServeHTTP(w, req)
})
}
func (wr Wrapper) readPart(req *http.Request, part *multipart.Part, frm url.Values) error {
defer func() {
if err := part.Close(); err != nil {
wr.logger.Printf("failed to close part: %v", err)
}
}()
name := part.FormName()
// read file
if part.FileName() != "" {
f, err := wr.readFile(req, part)
if err != nil {
return err
}
// if couldn't find type based on file header, try based on extension
if f.ftype == "application/octet-stream" {
if t := mime.TypeByExtension(filepath.Ext(f.name)); t != "" {
f.ftype = t
}
}
frm[name] = append(frm[name], f.key)
frm[name+"_name"] = append(frm[name+"_name"], f.name)
frm[name+"_type"] = append(frm[name+"_type"], f.ftype)
frm[name+"_size"] = append(frm[name+"_size"], fmt.Sprintf("%d", f.size))
return nil
}
// read string
val, err := wr.readString(part)
if err != nil {
return err
}
frm[name] = append(frm[name], val)
return nil
}
func (wr Wrapper) readFile(req *http.Request, part *multipart.Part) (file, error) {
f := file{
name: filepath.Clean(part.FileName()),
key: wr.prefixFunc(req) + uuid.NewString(),
}
counter := &bytesCounter{r: part}
_, err := wr.uploader.Upload(req.Context(), &s3.PutObjectInput{
ACL: types.ObjectCannedACL(wr.fileACL),
Key: aws.String(f.key),
Body: counter,
Bucket: aws.String(wr.bucket),
})
if err != nil {
return file{}, fmt.Errorf("failed to upload file to S3: %w", err)
}
f.size = counter.count
f.ftype = counter.fileType
return f, nil
}
func (Wrapper) readString(p *multipart.Part) (string, error) {
buf := bytes.Buffer{}
if _, err := buf.ReadFrom(p); err != nil {
return "", fmt.Errorf("failed to read string part: %w", err)
}
return buf.String(), nil
}
func createBucket(cli *s3.Client, name, acl string) error {
_, err := cli.CreateBucket(context.Background(), &s3.CreateBucketInput{
Bucket: aws.String(name),
ACL: types.BucketCannedACL(acl),
})
if err != nil {
var aerr *types.BucketAlreadyOwnedByYou
if errors.As(err, &aerr) {
return nil
}
return fmt.Errorf("failed to create bucket %q: %w", name, err)
}
return nil
}
func (wr Wrapper) logAndErr(w http.ResponseWriter, err error) {
wr.logger.Printf("failed to read request part: %v", err)
http.Error(w, http.StatusText(500), 500)
}
type bytesCounter struct {
r io.Reader
count int64
typeBuf []byte
fileType string
}
func (bc *bytesCounter) Read(b []byte) (int, error) {
n, err := bc.r.Read(b)
bc.count += int64(n)
// accumulate a few bytes (at most 261 according to https://github.com/h2non/filetype)
// so we can try to detect the content type via the file header
if bc.fileType == "" {
bc.typeBuf = append(bc.typeBuf, b...)
if errors.Is(err, io.EOF) || len(bc.typeBuf) >= 261 {
t, err := filetype.Match(bc.typeBuf)
if err != nil || t.MIME.Value == "" {
bc.fileType = "application/octet-stream"
} else {
bc.fileType = t.MIME.Value
}
bc.typeBuf = nil
}
}
return n, err
}