-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageController.go
39 lines (33 loc) · 1.15 KB
/
imageController.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
package insapp
import (
"encoding/json"
"net/http"
"gopkg.in/mgo.v2/bson"
"github.com/gorilla/mux"
)
// UploadNewImageController will upload a new image in the cdn
func UploadNewImageController(w http.ResponseWriter, r *http.Request) {
fileName, err := UploadImage(r)
ResponseHandler(&w, fileName, err)
}
// UploadImageController will upload a new image in the cdn
func UploadImageController(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
fileName, err := UploadImageWithName(r, vars["name"])
ResponseHandler(&w, fileName, err)
}
// ResponseHandler will response to the client
func ResponseHandler(w *http.ResponseWriter, fileName string, err error) {
if err != nil || fileName == "" {
(*w).WriteHeader(http.StatusNotAcceptable)
_ = json.NewEncoder(*w).Encode(bson.M{"error": "Failed to upload image"})
} else {
width, height := GetImageDimension(fileName)
if width == 0 || height == 0 {
_ = json.NewEncoder(*w).Encode(bson.M{"error": "Bad image format"})
return
}
colors := GetImageColors(fileName)
_ = json.NewEncoder(*w).Encode(bson.M{"file": fileName, "size": bson.M{"width": width, "height": height}, "colors": colors})
}
}