-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmongodb.go
116 lines (107 loc) · 2.65 KB
/
mongodb.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
/*
* Copyright (c) 2012 Jason McVetta. This is Free Software, released under the
* terms of the WTFPL v2. It comes without any warranty, express or implied.
* See http://sam.zoy.org/wtfpl/COPYING for more details.
*
*
* Derived from:
*
* jQuery File Upload Plugin GAE Go Example 2.0
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2011, Sebastian Tschan
* https://blueimp.net
*
* Original software by Tschan licensed under the MIT license:
* http://www.opensource.org/licenses/MIT
*/
package jfu
import (
"io"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
type mongoStore struct {
col *mgo.Collection // Collection where file info will be stored
gfs *mgo.GridFS // GridFS where file blob will be stored
}
// Exists checks whether a blob identified by key exists in the data store
func (s mongoStore) Exists(key string) (result bool, err error) {
// blobKey := appengine.BlobKey(key)
// bi, err := blobstore.Stat(appengine.NewContext(r), blobKey)
q := s.gfs.Find(bson.M{"_id": key})
cnt, err := q.Count()
if err != nil {
return
}
if cnt > 0 {
result = true
} else {
result = false
}
return
}
// ContentType returns the content type for the saved file indicated by key,
// or returns a FileNotFoundError if no such file exists.
func (s mongoStore) ContentType(key string) (ct string, err error) {
var file mgo.GridFile
q := s.gfs.Find(bson.M{"_id": key})
err = q.One(&file)
switch {
case err == mgo.ErrNotFound:
return ct, FileNotFoundError
case err != nil:
return
}
return file.ContentType(), nil
}
func (s mongoStore) Create(fi *FileInfo, r io.Reader) error {
file, err := s.gfs.Create(fi.Name)
if err != nil {
return err
}
defer file.Close()
file.SetContentType(fi.Type)
_, err = io.Copy(file, r)
if err != nil {
return err
}
fi.Key = file.Id().(bson.ObjectId).Hex()
fi.Size = file.Size()
return nil
}
func (s mongoStore) Get(key string) (fi FileInfo, r io.Reader, err error) {
// blobKey := appengine.BlobKey(key)
// bi, err := blobstore.Stat(appengine.NewContext(r), blobKey)
// blobstore.Send(w, appengine.BlobKey(key))
id := bson.ObjectIdHex(key)
file, err := s.gfs.OpenId(id)
if err != nil {
fi = FileInfo{
Error: err.Error(),
}
} else {
fi = FileInfo{
Key: key,
// Url
// ThumbnailUrl
Name: file.Name(),
Type: file.ContentType(),
Size: file.Size(),
// Error
// DeleteUrl
// DeleteType
}
}
r = file
return
}
func (s mongoStore) Delete(key string) error {
id := bson.ObjectIdHex(key)
return s.gfs.RemoveId(id)
}
// NewMongoStore creates a new DataStore backed by MongoDB
func NewMongoStore(gfs *mgo.GridFS) DataStore {
ms := mongoStore{gfs: gfs}
return ms
}