forked from josjevv/moire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
129 lines (106 loc) · 2.58 KB
/
common.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
package handlers
import (
log "github.com/Sirupsen/logrus"
"strings"
"time"
"github.com/bulletind/moire/config"
"github.com/bulletind/moire/db"
"gopkg.in/mgo.v2/bson"
)
func getConn() *db.MConn {
return db.GetConn(
config.Settings.Moire.DbUrl,
config.Settings.Moire.DbName)
}
func ConcatenateErrors(errs *[]error) string {
var errString string
for i := 0; i < len(*errs); i++ {
errString += (*errs)[i].Error()
if (len(*errs) - i) > 1 {
errString += ","
}
}
return errString
}
func updateAsset(conn *db.MConn, _id string, doc db.M) {
err := conn.Update(db.ASSET, db.M{"_id": bson.ObjectIdHex(_id)}, doc)
if err != nil {
panic(err)
}
}
func pollUntilReady(conn *db.MConn, _id string) {
timeout := time.After(time.Duration(config.Settings.Moire.ImageTimeout) * time.Second)
tick := time.Tick(1000 * time.Millisecond)
// Keep trying until we're timed out or got a result or got an error
for {
select {
// Got a timeout! fail with a timeout error
case <-timeout:
log.Debugln("Polling timed out")
return
// Got a tick, we should check on doSomething()
case <-tick:
asset := getAsset(conn, _id)
ok := asset.Status == db.READY
if ok {
return
}
}
}
}
func getAsset(conn *db.MConn, _id string) *db.Asset {
var asset db.Asset
err := conn.GetOne(db.ASSET, db.M{"_id": bson.ObjectIdHex(_id)}, &asset)
if err != nil {
panic(err)
}
return &asset
}
func assetReady(conn *db.MConn, path, bucket string, doc db.M) *db.Asset {
var asset db.Asset
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
conn.GetOne(db.ASSET, db.M{
"path": path,
"bucket": bucket,
}, &asset)
if asset.Status != db.PENDING {
log.WithField("asset", asset).Debugln("asset has an invalid state")
return nil
}
err := conn.FindAndUpdate(db.ASSET, db.M{
"path": path,
"bucket": bucket,
"status": db.PENDING,
}, doc, &asset)
if err != nil {
panic(err)
}
return &asset
}
func createAsset(conn *db.MConn, args *assetArgs) *db.Asset {
assetId := bson.NewObjectId()
if args.Collection == "" {
args.Collection = DefaultCollection
}
asset := db.Asset{
Id: assetId,
CreatedOn: db.EpochNow(),
Name: args.Name,
Bucket: config.Settings.S3.Bucket,
FileType: args.fileType,
MimeType: args.MimeType,
Status: db.PENDING,
Collection: args.Collection,
Path: getUploadURL(assetId.Hex(), args.Collection, args.Name),
}
conn.Insert(db.ASSET, &asset)
return &asset
}
func isMimeAllowed(mimeType string) bool {
if mimeType != ImageFile && mimeType != VideoFile && mimeType != AudioFile {
return false
}
return true
}