/
api.go
300 lines (253 loc) · 7.3 KB
/
api.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
package api
import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"image"
"image/jpeg"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"github.com/127biscuits/apihippo.com/mongo"
"github.com/127biscuits/apihippo.com/settings"
"github.com/gorilla/mux"
"github.com/nfnt/resize"
)
// PaginatedResponse is the struct used for paginated JSON responses
type PaginatedResponse struct {
Meta struct {
HasPrevious bool `json:"hasPrevious"`
HasNext bool `json:"hasNext"`
Pages int `json:"pages"`
} `json:"meta"`
Hippos []*mongo.Hippo `json:"hippos"`
}
// GetHandler is a JSON endpoint that returns ALL the hippos paginated.
// It can be filtered with ?verified=true or ?verified=false
func GetHandler(w http.ResponseWriter, r *http.Request) {
var (
comparator = "$gte"
pageSize = settings.Config.PageSize
query interface{}
votesToVerify = settings.Config.NeededVotesToVerify
)
page, err := strconv.Atoi(r.FormValue("page"))
if err != nil && r.FormValue("page") != "" {
http.Error(w, err.Error(), http.StatusBadRequest)
}
if r.FormValue("verified") != "" {
verified, err := strconv.ParseBool(r.FormValue("verified"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
if verified {
comparator = "$lt"
}
query = bson.M{"votes": bson.M{comparator: votesToVerify}}
}
all := mongo.Collection.Find(query)
sliceAll := all.Limit(pageSize)
if page > 0 {
sliceAll = sliceAll.Skip(pageSize * (page - 1))
}
count, _ := all.Count()
response := &PaginatedResponse{}
response.Meta.Pages = count / pageSize
response.Meta.HasPrevious = page > 0
response.Meta.HasNext = page < response.Meta.Pages
sliceAll.All(&response.Hippos)
// Add URLs
for _, hippo := range response.Hippos {
hippo.Populate()
}
js, _ := json.Marshal(response)
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
// GetHippoHandler is going to find a hippo by Mongo ID and return it in JSON
// format.
// In case that the hippo is not found, we are going to return a 404.
func GetHippoHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
doc := &mongo.Hippo{}
if err := mongo.Collection.FindId(bson.ObjectIdHex(id)).One(doc); err != nil {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(doc.JSON())
}
// VoteHippoHandler is going to increment the number of votes for a cerating
// hippo
func VoteHippoHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
change := bson.M{"$inc": bson.M{"votes": 1}}
err := mongo.Collection.UpdateId(bson.ObjectIdHex(id), change)
switch {
case err == mgo.ErrNotFound:
http.NotFound(w, r)
return
case err != nil:
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
GetHippoHandler(w, r)
}
// PostHandler is able to receive hippo image and store them in our backend.
func PostHandler(w http.ResponseWriter, r *http.Request) {
// TODO: check that the posted file is an image
if err := r.ParseMultipartForm(int64(settings.Config.Server.MaxFileSize)); err != nil {
errMessage := fmt.Sprintf(
"Have you added the Content-Type: multipart/form-data header?"+
"This is the detailed error: %s", err.Error())
http.Error(w, errMessage, http.StatusBadRequest)
return
}
// TODO: support multiple file upload, for now, we return after the first insertion
var key string
for key, _ = range r.MultipartForm.File {
break
}
file, fileHeader, err := r.FormFile(key)
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
defer file.Close()
// TODO: accept PNGs as well (the header is "application/octet-stream".
// We should check file headers and not request headers.
if !strings.HasPrefix(fileHeader.Header.Get("Content-Type"), "image/") {
http.Error(w, "I will just accept an \"image/*\" here!", http.StatusBadRequest)
return
}
checksum := fmt.Sprintf("%x", md5.Sum(fileBytes))
doc, err := mongo.GetHippoByMD5(checksum)
if doc != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(409)
w.Write(doc.JSON())
return
}
if err == mgo.ErrNotFound {
doc, err := mongo.InsertHippo(fileBytes)
if err != nil {
http.Error(w, "Holy s*£%t! I couldn't store your hippo!", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(201)
w.Write(doc.JSON())
return
}
w.WriteHeader(500)
}
// FakeCDNHandler will return the image stream for the hippo.
// TODO: this is just temporal until we have a proper CDN.
func FakeCDNHandler(w http.ResponseWriter, r *http.Request) {
var width, height int
vars := mux.Vars(r)
filename := vars["id"]
err := func() (err error) {
if w := r.FormValue("width"); w != "" {
width, err = strconv.Atoi(w)
if err != nil {
return err
}
}
if h := r.FormValue("height"); h != "" {
height, err = strconv.Atoi(h)
if err != nil {
return err
}
}
return nil
}()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) //"width & height params must be integers!", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "image/jpeg") // TODO: check the type of the image before adding this header
file, err := mongo.GridFS.Open(filename)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
imageBytes := make([]byte, file.Size())
file.Read(imageBytes)
// No resizing needed
if width+height == 0 {
w.Write(imageBytes)
return
}
err = func() error {
originalImage, _, err := image.Decode(bytes.NewReader(imageBytes))
if err != nil {
return err
}
resizedImage := resize.Resize(uint(width), uint(height), originalImage, resize.Lanczos3)
if err = jpeg.Encode(w, resizedImage, nil); err != nil {
return err
}
return nil
}()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// RandomHippoHandler will return a JSON response with a verified hippo
func RandomHippoHandler(w http.ResponseWriter, r *http.Request) {
// TODO: it should be something better than this
w.Header().Set("Access-Control-Allow-Origin", "*")
var (
comparator = "$gte"
votesToVerify = settings.Config.NeededVotesToVerify
)
// Ensure index on Random if we want efficience
err := mongo.Collection.EnsureIndexKey("random")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
rand.Seed(time.Now().UnixNano())
random := rand.Float32()
hippo := &mongo.Hippo{}
if r.FormValue("verified") != "" {
verified, err := strconv.ParseBool(r.FormValue("verified"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
if !verified {
comparator = "$lt"
}
}
// We will need to query both in case that we don't find a result in the
// first interval
for _, r := range []string{"$gte", "$lte"} {
query := bson.M{
"random": bson.M{r: random},
"votes": bson.M{comparator: votesToVerify},
}
qs := mongo.Collection.Find(query)
if n, _ := qs.Count(); n > 0 {
err := qs.One(hippo)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(hippo.JSON())
return
}
}
http.NotFound(w, r)
return
}