-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model.go
502 lines (404 loc) · 12 KB
/
Model.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
/*
* Copyright (c) 2020. App Nerds LLC. All rights reserved
*/
package database
import (
"io"
"path/filepath"
"time"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/pkg/errors"
)
/*
Dial establishes a new session to one or more Mongo databases
*/
func Dial(url string) (Session, error) {
s, err := mgo.Dial(url)
result := &MongoSession{
Session: s,
}
return result, err
}
// Session is an interface to access to the Session struct.
type Session interface {
DB(name string) Database
Close()
}
// MongoSession is currently a Mongo session.
type MongoSession struct {
*mgo.Session
}
// DB shadows *mgo.DB to returns a DataLayer interface instead of *mgo.Database.
func (s MongoSession) DB(name string) Database {
return &MongoDatabase{Database: s.Session.DB(name)}
}
// DataLayer is an interface to access to the database struct.
type Database interface {
C(name string) Collection
GridFS(prefix string) GridFS
}
// MongoDatabase wraps a mgo.Database to embed methods in models.
type MongoDatabase struct {
*mgo.Database
}
// C shadows *mgo.DB to returns a DataLayer interface instead of *mgo.Database.
func (d MongoDatabase) C(name string) Collection {
return &MongoCollection{Collection: d.Database.C(name)}
}
func (d MongoDatabase) GridFS(prefix string) GridFS {
return &MongoGridFS{GridFS: d.Database.GridFS(prefix)}
}
// Collection is an interface to access to the collection struct.
type Collection interface {
Count() (int, error)
DropAllIndexes() error
DropCollection() error
DropIndex(key ...string) error
DropIndexName(name string) error
EnsureIndex(index mgo.Index) error
EnsureIndexKey(key ...string) error
Find(query interface{}) Query
FindWithPaging(query interface{}, skip, limit int) (Query, int, error)
FindId(id interface{}) Query
Indexes() ([]mgo.Index, error)
Insert(docs ...interface{}) error
Remove(selector interface{}) error
RemoveAll(selector interface{}) (*mgo.ChangeInfo, error)
RemoveId(id interface{}) error
Update(selector interface{}, update interface{}) error
UpdateAll(selector interface{}, update interface{}) (*mgo.ChangeInfo, error)
UpdateId(id interface{}, update interface{}) error
Upsert(selector interface{}, update interface{}) (*mgo.ChangeInfo, error)
UpsertId(id interface{}, update interface{}) (*mgo.ChangeInfo, error)
}
// MongoCollection wraps a mgo.Collection to embed methods in models.
type MongoCollection struct {
*mgo.Collection
}
func (c *MongoCollection) Count() (int, error) {
return c.Collection.Count()
}
func (c *MongoCollection) DropAllIndexes() error {
return c.Collection.DropAllIndexes()
}
func (c *MongoCollection) DropCollection() error {
return c.Collection.DropCollection()
}
func (c *MongoCollection) DropIndex(key ...string) error {
return c.Collection.DropIndex(key...)
}
func (c *MongoCollection) DropIndexName(name string) error {
return c.DropIndexName(name)
}
func (c *MongoCollection) EnsureIndex(index mgo.Index) error {
return c.Collection.EnsureIndex(index)
}
func (c *MongoCollection) EnsureIndexKey(key ...string) error {
return c.Collection.EnsureIndexKey(key...)
}
// Find shadows *mgo.Collection to returns a Query interface instead of *mgo.Query.
func (c *MongoCollection) Find(query interface{}) Query {
return &MongoQuery{Query: c.Collection.Find(query)}
}
func (c *MongoCollection) FindWithPaging(query interface{}, skip, limit int) (Query, int, error) {
var (
err error
result Query
count int
)
result = &MongoQuery{Query: c.Collection.Find(query).Skip(skip).Limit(limit)}
count, err = c.Collection.Find(query).Count()
return result, count, err
}
func (c *MongoCollection) FindId(id interface{}) Query {
return &MongoQuery{Query: c.Collection.FindId(id)}
}
func (c *MongoCollection) Indexes() ([]mgo.Index, error) {
return c.Collection.Indexes()
}
func (c *MongoCollection) Insert(docs ...interface{}) error {
return c.Collection.Insert(docs...)
}
func (c *MongoCollection) Remove(selector interface{}) error {
return c.Collection.Remove(selector)
}
func (c *MongoCollection) RemoveAll(selector interface{}) (*mgo.ChangeInfo, error) {
return c.Collection.RemoveAll(selector)
}
func (c *MongoCollection) RemoveId(id interface{}) error {
return c.Collection.RemoveId(id)
}
func (c *MongoCollection) Update(selector interface{}, update interface{}) error {
return c.Collection.Update(selector, update)
}
func (c *MongoCollection) UpdateAll(selector interface{}, update interface{}) (*mgo.ChangeInfo, error) {
return c.Collection.UpdateAll(selector, update)
}
func (c *MongoCollection) UpdateId(id interface{}, update interface{}) error {
return c.Collection.UpdateId(id, update)
}
func (c *MongoCollection) Upsert(selector interface{}, update interface{}) (*mgo.ChangeInfo, error) {
return c.Collection.Upsert(selector, update)
}
func (c *MongoCollection) UpsertId(id interface{}, update interface{}) (*mgo.ChangeInfo, error) {
return c.Collection.UpsertId(id, update)
}
// Query is an interface to access to the database struct
type Query interface {
All(result interface{}) error
Count() (int, error)
Distinct(key string, result interface{}) error
Limit(n int) Query
One(result interface{}) (err error)
Select(selector interface{}) Query
Skip(n int) Query
Sort(fields ...string) Query
}
// MongoQuery wraps a mgo.Query to embed methods in models.
type MongoQuery struct {
*mgo.Query
}
func (q *MongoQuery) All(result interface{}) error {
return q.Query.All(result)
}
func (q *MongoQuery) Count() (int, error) {
return q.Query.Count()
}
func (q *MongoQuery) Distinct(key string, result interface{}) error {
return q.Query.Distinct(key, result)
}
func (q *MongoQuery) Limit(n int) Query {
result := q.Query.Limit(n)
return &MongoQuery{Query: result}
}
func (q *MongoQuery) One(result interface{}) error {
return q.Query.One(result)
}
func (q *MongoQuery) Select(selector interface{}) Query {
result := q.Query.Select(selector)
return &MongoQuery{Query: result}
}
func (q *MongoQuery) Skip(n int) Query {
result := q.Query.Skip(n)
return &MongoQuery{Query: result}
}
func (q *MongoQuery) Sort(fields ...string) Query {
result := q.Query.Sort(fields...)
return &MongoQuery{Query: result}
}
// GridFS stores files in a MongoDB database
type GridFS interface {
Create(name string) (GridFile, error)
Find(query interface{}) Query
Open(name string) (GridFile, error)
OpenId(id interface{}) (GridFile, error)
Remove(name string) (err error)
RemoveId(id interface{}) error
}
// MongoGridFS is the wrapper around GridFS
type MongoGridFS struct {
*mgo.GridFS
}
// Create makes a new GridFS storage
func (gfs *MongoGridFS) Create(name string) (GridFile, error) {
result, err := gfs.GridFS.Create(name)
return &MongoGridFile{GridFile: result}, err
}
// Find queries GridFS
func (gfs *MongoGridFS) Find(query interface{}) Query {
return &MongoQuery{Query: gfs.GridFS.Find(query)}
}
// Open opens a grid file
func (gfs *MongoGridFS) Open(name string) (GridFile, error) {
result, err := gfs.GridFS.Open(name)
return &MongoGridFile{GridFile: result}, err
}
func (gfs *MongoGridFS) OpenId(id interface{}) (GridFile, error) {
result, err := gfs.GridFS.OpenId(id)
return &MongoGridFile{GridFile: result}, err
}
func (gfs *MongoGridFS) Remove(name string) error {
return gfs.GridFS.Remove(name)
}
func (gfs *MongoGridFS) RemoveId(id interface{}) error {
return gfs.GridFS.RemoveId(id)
}
// GridFile represents a single file in GridFS
type GridFile interface {
Abort()
Close() error
ContentType() string
GetMeta(result interface{}) error
Id() interface{}
MD5() string
Name() string
Read(b []byte) (int, error)
Seek(offset int64, whence int) (int64, error)
SetChunkSize(bytes int)
SetContentType(ctype string)
SetId(id interface{})
SetMeta(metadata interface{})
SetName(name string)
SetUploadDate(t time.Time)
Size() int64
Write(data []byte) (int, error)
}
type MongoGridFile struct {
*mgo.GridFile
}
func (mgf *MongoGridFile) Abort() {
mgf.GridFile.Abort()
}
func (mgf *MongoGridFile) Close() error {
return mgf.GridFile.Close()
}
func (mgf *MongoGridFile) ContentType() string {
return mgf.GridFile.ContentType()
}
func (mgf *MongoGridFile) GetMeta(result interface{}) error {
return mgf.GridFile.GetMeta(result)
}
func (mgf *MongoGridFile) Id() interface{} {
return mgf.GridFile.Id()
}
func (mgf *MongoGridFile) MD5() string {
return mgf.GridFile.MD5()
}
func (mgf *MongoGridFile) Name() string {
return mgf.GridFile.Name()
}
func (mgf *MongoGridFile) Read(b []byte) (int, error) {
return mgf.GridFile.Read(b)
}
func (mgf *MongoGridFile) Seek(offset int64, whence int) (int64, error) {
return mgf.GridFile.Seek(offset, whence)
}
func (mgf *MongoGridFile) SetChunkSize(bytes int) {
mgf.GridFile.SetChunkSize(bytes)
}
func (mgf *MongoGridFile) SetContentType(ctype string) {
mgf.GridFile.SetContentType(ctype)
}
func (mgf *MongoGridFile) SetId(id interface{}) {
mgf.GridFile.SetId(id)
}
func (mgf *MongoGridFile) SetMeta(metadata interface{}) {
mgf.GridFile.SetMeta(metadata)
}
func (mgf *MongoGridFile) SetName(name string) {
mgf.GridFile.SetName(name)
}
func (mgf *MongoGridFile) SetUploadDate(t time.Time) {
mgf.GridFile.SetUploadDate(t)
}
func (mgf *MongoGridFile) Size() int64 {
return mgf.GridFile.Size()
}
func (mgf *MongoGridFile) Write(data []byte) (int, error) {
return mgf.GridFile.Write(data)
}
type Iter interface {
All(result interface{}) error
Close() error
Done() bool
Err() error
For(result interface{}, f func() error) error
Next(result interface{}) bool
State() (int64, []bson.Raw)
Timeout() bool
}
type MongoIter struct {
*mgo.Iter
}
func (iter *MongoIter) All(result interface{}) error {
return iter.Iter.All(result)
}
func (iter *MongoIter) Close() error {
return iter.Iter.Close()
}
func (iter *MongoIter) Done() bool {
return iter.Iter.Done()
}
func (iter *MongoIter) Err() error {
return iter.Iter.Err()
}
func (iter *MongoIter) For(result interface{}, f func() error) error {
return iter.Iter.For(result, f)
}
func (iter *MongoIter) Next(result interface{}) bool {
return iter.Iter.Next(result)
}
func (iter *MongoIter) State() (int64, []bson.Raw) {
return iter.Iter.State()
}
func (iter *MongoIter) Timeout() bool {
return iter.Iter.Timeout()
}
/*
A DatabaseUploader defines an interface for uploading files to a database
*/
type DatabaseUploader interface {
Upload(reader io.Reader, name, path string) (*DatabaseUploadResponse, error)
}
/*
A DatabaseUploadResponse is used to alert a caller information about their uploaded file.
*/
type DatabaseUploadResponse struct {
BytesWritten int `json:"bytesWritten"`
FileID bson.ObjectId `json:"fileID"`
FileName string `json:"fileName"`
Height int `json:"height"`
Width int `json:"width"`
}
/*
A MongoUploader uploads files to a MongoDB database. This struct
satisfies the DatabaseUploader interface
*/
type MongoUploader struct {
DB Database
}
/*
Upload uploads a file into the MongoDB GridFS system
*/
func (u *MongoUploader) Upload(reader io.Reader, name, path string) (*DatabaseUploadResponse, error) {
var err error
var file GridFile
result := &DatabaseUploadResponse{}
totalBytesWritten := 0
bytesWritten := 0
bytesRead := 0
buffer := make([]byte, 2048)
name = u.sanitizeFileName(name)
/*
* Create the file in GridFS
*/
if file, err = u.DB.GridFS(path).Create(name); err != nil {
return result, errors.Wrapf(err, "Error uploading file '%s' to MongoDB GridFS", name)
}
defer file.Close()
/*
* Read bytes from the file, write bytes to GridFS
*/
for {
bytesRead, err = reader.Read(buffer)
if err == nil {
if bytesRead > 0 {
if bytesWritten, err = file.Write(buffer); err != nil {
result.BytesWritten = bytesWritten
return result, errors.Wrapf(err, "Error writing file bytes to GridFS")
}
totalBytesWritten += bytesWritten
}
}
if err == io.EOF {
result.BytesWritten = totalBytesWritten
result.FileID = (file.Id()).(bson.ObjectId)
result.FileName = name
return result, nil
}
}
}
func (u *MongoUploader) sanitizeFileName(name string) string {
return filepath.Base(filepath.Clean(name))
}