-
Notifications
You must be signed in to change notification settings - Fork 7
/
gridfs_bucket.go
538 lines (448 loc) · 13.8 KB
/
gridfs_bucket.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package pail
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/gridfs"
"go.mongodb.org/mongo-driver/mongo/options"
)
// GridFSOptions support the use and creation of GridFS backed buckets.
type GridFSOptions struct {
Name string
Prefix string
Database string
MongoDBURI string
DryRun bool
DeleteOnSync bool
DeleteOnPush bool
DeleteOnPull bool
Verbose bool
}
func (o *GridFSOptions) validate() error {
if (o.DeleteOnPush != o.DeleteOnPull) && o.DeleteOnSync {
return errors.New("ambiguous delete on sync options set")
}
return nil
}
type gridfsBucket struct {
opts GridFSOptions
client *mongo.Client
}
func (b *gridfsBucket) normalizeKey(key string) string { return b.Join(b.opts.Prefix, key) }
func (b *gridfsBucket) denormalizeKey(key string) string {
return consistentTrimPrefix(key, b.opts.Prefix)
}
// NewGridFSBucketWithClient returns a new bucket backed by GridFS with the
// existing Mongo client and given options.
func NewGridFSBucketWithClient(ctx context.Context, client *mongo.Client, opts GridFSOptions) (Bucket, error) {
if client == nil {
return nil, errors.New("must provide a Mongo client")
}
if err := opts.validate(); err != nil {
return nil, err
}
return &gridfsBucket{opts: opts, client: client}, nil
}
// NewGridFSBucket returns a bucket backed by GridFS with the given options.
func NewGridFSBucket(ctx context.Context, opts GridFSOptions) (Bucket, error) {
if err := opts.validate(); err != nil {
return nil, err
}
client, err := mongo.Connect(ctx, options.Client().ApplyURI(opts.MongoDBURI))
if err != nil {
return nil, errors.Wrap(err, "constructing client")
}
return &gridfsBucket{opts: opts, client: client}, nil
}
func (b *gridfsBucket) Check(ctx context.Context) error {
return errors.Wrap(b.client.Ping(ctx, nil), "pinging DB")
}
func (b *gridfsBucket) Exists(ctx context.Context, key string) (bool, error) {
grid, err := b.bucket(ctx)
if err != nil {
return false, errors.Wrap(err, "resolving bucket")
}
if err = grid.GetFilesCollection().FindOne(ctx, bson.M{"filename": b.normalizeKey(key)}).Err(); err == mongo.ErrNoDocuments {
return false, nil
} else if err != nil {
return false, errors.Wrap(err, "finding file")
}
return true, nil
}
func (b *gridfsBucket) Join(elems ...string) string { return consistentJoin(elems) }
// bucket returns a new GridFS bucket configured with the context timeout, if
// it exists. This function is called by each operation that needs to read or
// write from the bucket to avoid read and write deadline conflicts—GridFS only
// supports a single read and a single write deadline for a bucket instance.
// This function sets the read and write deadlines to allow it to respect the
// context timeouts passed in by the caller.
func (b *gridfsBucket) bucket(ctx context.Context) (*gridfs.Bucket, error) {
if err := ctx.Err(); err != nil {
return nil, errors.Wrap(err, "fetching bucket with canceled context")
}
gfs, err := gridfs.NewBucket(b.client.Database(b.opts.Database), options.GridFSBucket().SetName(b.opts.Name))
if err != nil {
return nil, errors.WithStack(err)
}
// The streaming GridFS functions do not accept a context so we need to
// set the deadline from the passed-in context here, if it exists, in
// order to respect context timeouts passed in by the caller.
dl, ok := ctx.Deadline()
if ok {
_ = gfs.SetReadDeadline(dl)
_ = gfs.SetWriteDeadline(dl)
}
return gfs, nil
}
func (b *gridfsBucket) Writer(ctx context.Context, name string) (io.WriteCloser, error) {
grip.DebugWhen(b.opts.Verbose, message.Fields{
"type": "gridfs",
"dry_run": b.opts.DryRun,
"operation": "writer",
"bucket": b.opts.Name,
"bucket_prefix": b.opts.Prefix,
"key": name,
})
grid, err := b.bucket(ctx)
if err != nil {
return nil, errors.Wrap(err, "resolving bucket")
}
if b.opts.DryRun {
return &mockWriteCloser{}, nil
}
writer, err := grid.OpenUploadStream(b.normalizeKey(name))
if err != nil {
return nil, errors.Wrap(err, "opening stream")
}
return writer, nil
}
func (b *gridfsBucket) Reader(ctx context.Context, name string) (io.ReadCloser, error) {
grip.DebugWhen(b.opts.Verbose, message.Fields{
"type": "gridfs",
"operation": "reader",
"bucket": b.opts.Name,
"bucket_prefix": b.opts.Prefix,
"key": name,
})
grid, err := b.bucket(ctx)
if err != nil {
return nil, errors.Wrap(err, "resolving bucket")
}
reader, err := grid.OpenDownloadStreamByName(b.normalizeKey(name))
if err != nil {
if err == gridfs.ErrFileNotFound {
err = MakeKeyNotFoundError(err)
}
return nil, errors.Wrap(err, "opening stream")
}
return reader, nil
}
func (b *gridfsBucket) Put(ctx context.Context, name string, input io.Reader) error {
grip.DebugWhen(b.opts.Verbose, message.Fields{
"type": "gridfs",
"dry_run": b.opts.DryRun,
"operation": "put",
"bucket": b.opts.Name,
"bucket_prefix": b.opts.Prefix,
"key": name,
})
grid, err := b.bucket(ctx)
if err != nil {
return errors.Wrap(err, "resolving bucket")
}
if b.opts.DryRun {
return nil
}
if _, err = grid.UploadFromStream(b.normalizeKey(name), input); err != nil {
return errors.Wrap(err, "uploading file")
}
return nil
}
func (b *gridfsBucket) Get(ctx context.Context, name string) (io.ReadCloser, error) {
grip.DebugWhen(b.opts.Verbose, message.Fields{
"type": "gridfs",
"operation": "get",
"bucket": b.opts.Name,
"bucket_prefix": b.opts.Prefix,
"key": name,
})
return b.Reader(ctx, name)
}
func (b *gridfsBucket) Upload(ctx context.Context, name, path string) error {
grip.DebugWhen(b.opts.Verbose, message.Fields{
"type": "gridfs",
"dry_run": b.opts.DryRun,
"operation": "upload",
"bucket": b.opts.Name,
"bucket_prefix": b.opts.Prefix,
"key": name,
"path": path,
})
f, err := os.Open(path)
if err != nil {
return errors.Wrapf(err, "opening file '%s'", name)
}
defer f.Close()
return errors.WithStack(b.Put(ctx, name, f))
}
func (b *gridfsBucket) Download(ctx context.Context, name, path string) error {
grip.DebugWhen(b.opts.Verbose, message.Fields{
"type": "gridfs",
"operation": "download",
"bucket": b.opts.Name,
"bucket_prefix": b.opts.Prefix,
"key": name,
"path": path,
})
reader, err := b.Reader(ctx, name)
if err != nil {
return errors.WithStack(err)
}
if err = os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return errors.Wrapf(err, "creating enclosing directory for file '%s'", path)
}
f, err := os.Create(path)
if err != nil {
return errors.Wrapf(err, "creating file '%s'", path)
}
defer f.Close()
_, err = io.Copy(f, reader)
return errors.Wrap(err, "copying data to file")
}
func (b *gridfsBucket) Push(ctx context.Context, opts SyncOptions) error {
grip.DebugWhen(b.opts.Verbose, message.Fields{
"type": "gridfs",
"dry_run": b.opts.DryRun,
"operation": "push",
"bucket": b.opts.Name,
"bucket_prefix": b.opts.Prefix,
"remote": opts.Remote,
"local": opts.Local,
"exclude": opts.Exclude,
})
var re *regexp.Regexp
var err error
if opts.Exclude != "" {
re, err = regexp.Compile(opts.Exclude)
if err != nil {
return errors.Wrap(err, "compiling exclude regex")
}
}
localPaths, err := walkLocalTree(ctx, opts.Local)
if err != nil {
return errors.Wrap(err, "finding local paths")
}
for _, path := range localPaths {
if re != nil && re.MatchString(path) {
continue
}
target := b.Join(opts.Remote, path)
if err = b.Upload(ctx, target, filepath.Join(opts.Local, path)); err != nil {
return errors.Wrapf(err, "uploading file '%s' to '%s'", path, target)
}
}
if (b.opts.DeleteOnPush || b.opts.DeleteOnSync) && !b.opts.DryRun {
return errors.Wrap(deleteOnPush(ctx, localPaths, opts.Remote, b), "deleting on sync after push")
}
return nil
}
func (b *gridfsBucket) Pull(ctx context.Context, opts SyncOptions) error {
grip.DebugWhen(b.opts.Verbose, message.Fields{
"type": "gridfs",
"operation": "pull",
"bucket": b.opts.Name,
"bucket_prefix": b.opts.Prefix,
"remote": opts.Remote,
"local": opts.Local,
"exclude": opts.Exclude,
})
var re *regexp.Regexp
var err error
if opts.Exclude != "" {
re, err = regexp.Compile(opts.Exclude)
if err != nil {
return errors.Wrap(err, "compiling exclude regex")
}
}
iter, err := b.List(ctx, opts.Remote)
if err != nil {
return errors.WithStack(err)
}
keys := []string{}
for iter.Next(ctx) {
item := iter.Item()
if re != nil && re.MatchString(item.Name()) {
continue
}
localName, err := filepath.Rel(opts.Remote, item.Name())
if err != nil {
return errors.Wrap(err, "getting relative filepath")
}
keys = append(keys, localName)
if err = b.Download(ctx, item.Name(), filepath.Join(opts.Local, localName)); err != nil {
return errors.WithStack(err)
}
}
if err = iter.Err(); err != nil {
return errors.WithStack(err)
}
if (b.opts.DeleteOnPull || b.opts.DeleteOnSync) && !b.opts.DryRun {
return errors.Wrap(deleteOnPull(ctx, keys, opts.Local), "deleting on sync after pull")
}
return nil
}
func (b *gridfsBucket) Copy(ctx context.Context, opts CopyOptions) error {
grip.DebugWhen(b.opts.Verbose, message.Fields{
"type": "gridfs",
"operation": "copy",
"bucket": b.opts.Name,
"bucket_prefix": b.opts.Prefix,
"source_key": opts.SourceKey,
"dest_key": opts.DestinationKey,
})
from, err := b.Reader(ctx, opts.SourceKey)
if err != nil {
return errors.Wrap(err, "getting reader for source")
}
to, err := opts.DestinationBucket.Writer(ctx, opts.DestinationKey)
if err != nil {
return errors.Wrap(err, "getting writer for destination")
}
if _, err = io.Copy(to, from); err != nil {
return errors.Wrap(err, "copying data")
}
return errors.WithStack(to.Close())
}
func (b *gridfsBucket) Remove(ctx context.Context, key string) error {
grip.DebugWhen(b.opts.Verbose, message.Fields{
"type": "gridfs",
"dry_run": b.opts.DryRun,
"operation": "remove",
"bucket": b.opts.Name,
"bucket_prefix": b.opts.Prefix,
"key": key,
})
return b.RemoveMany(ctx, key)
}
func (b *gridfsBucket) RemoveMany(ctx context.Context, keys ...string) error {
grip.DebugWhen(b.opts.Verbose, message.Fields{
"type": "gridfs",
"dry_run": b.opts.DryRun,
"operation": "remove many",
"bucket": b.opts.Name,
"bucket_prefix": b.opts.Prefix,
"keys": keys,
})
grid, err := b.bucket(ctx)
if err != nil {
return errors.Wrap(err, "resolving bucket")
}
normalizedKeys := make([]string, len(keys))
for i, key := range keys {
normalizedKeys[i] = b.normalizeKey(key)
}
cur, err := grid.FindContext(ctx, bson.M{"filename": bson.M{"$in": normalizedKeys}})
if err != nil {
return errors.Wrap(err, "finding file(s)")
}
catcher := grip.NewBasicCatcher()
document := struct {
ID interface{} `bson:"_id"`
}{}
for cur.Next(ctx) {
if err = cur.Decode(&document); err != nil {
return errors.Wrap(err, "decoding GridFS metadata")
}
if b.opts.DryRun {
continue
}
if err = grid.DeleteContext(ctx, document.ID); err != nil {
catcher.Wrap(err, "deleting GridFS file")
break
}
}
catcher.Wrap(cur.Err(), "iterating GridFS metadata")
catcher.Wrap(cur.Close(ctx), "closing cursor")
return catcher.Resolve()
}
func (b *gridfsBucket) RemovePrefix(ctx context.Context, prefix string) error {
grip.DebugWhen(b.opts.Verbose, message.Fields{
"type": "gridfs",
"dry_run": b.opts.DryRun,
"operation": "remove prefix",
"bucket": b.opts.Name,
"bucket_prefix": b.opts.Prefix,
"prefix": prefix,
})
return removePrefix(ctx, prefix, b)
}
func (b *gridfsBucket) RemoveMatching(ctx context.Context, expr string) error {
grip.DebugWhen(b.opts.Verbose, message.Fields{
"type": "gridfs",
"dry_run": b.opts.DryRun,
"operation": "remove matching",
"bucket": b.opts.Name,
"bucket_prefix": b.opts.Prefix,
"expression": expr,
})
return removeMatching(ctx, expr, b)
}
func (b *gridfsBucket) List(ctx context.Context, prefix string) (BucketIterator, error) {
grip.DebugWhen(b.opts.Verbose, message.Fields{
"type": "gridfs",
"operation": "list",
"bucket": b.opts.Name,
"bucket_prefix": b.opts.Prefix,
"prefix": prefix,
})
grid, err := b.bucket(ctx)
if err != nil {
return nil, errors.Wrap(err, "resolving bucket")
}
filter := bson.M{}
if prefix != "" {
filter = bson.M{"filename": primitive.Regex{Pattern: fmt.Sprintf("^%s.*", b.normalizeKey(prefix))}}
}
cursor, err := grid.FindContext(ctx, filter, options.GridFSFind().SetSort(bson.M{"filename": 1}))
if err != nil {
return nil, errors.Wrap(err, "finding file")
}
return &gridfsIterator{bucket: b, iter: cursor}, nil
}
type gridfsIterator struct {
err error
bucket *gridfsBucket
iter *mongo.Cursor
item *bucketItemImpl
}
func (iter *gridfsIterator) Err() error { return iter.err }
func (iter *gridfsIterator) Item() BucketItem { return iter.item }
func (iter *gridfsIterator) Next(ctx context.Context) bool {
if !iter.iter.Next(ctx) {
iter.err = iter.iter.Err()
return false
}
document := struct {
ID interface{} `bson:"_id"`
Filename string `bson:"filename"`
}{}
if err := iter.iter.Decode(&document); err != nil {
iter.err = err
return false
}
iter.item = &bucketItemImpl{
bucket: iter.bucket.opts.Name,
key: iter.bucket.denormalizeKey(document.Filename),
b: iter.bucket,
}
return true
}