-
Notifications
You must be signed in to change notification settings - Fork 0
/
panda.go
114 lines (92 loc) · 2.26 KB
/
panda.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
package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"strconv"
"github.com/gorilla/mux"
"gopkg.in/h2non/bimg.v1"
)
func respondWithError(err error, w http.ResponseWriter, status int) {
w.Header().Set("Content-Type", "plain/text")
w.WriteHeader(status)
log.Printf("%v\n", err)
w.Write([]byte(err.Error()))
}
func pandaHandler(w http.ResponseWriter, r *http.Request) {
pandas, err := listOfPandas()
if err != nil {
respondWithError(err, w, http.StatusInternalServerError)
return
}
vars := mux.Vars(r)
width, err := strconv.Atoi(vars["width"])
if err != nil {
respondWithError(err, w, http.StatusBadRequest)
return
}
if width < 0 {
width = 0
}
height, err := strconv.Atoi(vars["height"])
if err != nil {
respondWithError(err, w, http.StatusBadRequest)
return
}
if height < 0 {
height = 0
}
dprFactor := 1.0
if dprHeader := r.Header.Get("DPR"); len(dprHeader) > 0 {
if dprFactor, err = strconv.ParseFloat(dprHeader, 64); err != nil {
dprFactor = 1.0
}
}
width = int(float64(width) * dprFactor)
height = int(float64(height) * dprFactor)
key := fmt.Sprintf("cache/%v/%v.jpg", width, height)
var buf []byte
if checkCache(width, height) {
buf, err = downloadPanda(key)
if err != nil {
respondWithError(err, w, http.StatusBadRequest)
return
}
w.Header().Set("X-Cache-Info", "HIT")
} else {
pandaNum := rand.Int() % len(pandas)
pandaFile, err := downloadPanda(pandas[pandaNum])
if err != nil {
respondWithError(err, w, http.StatusInternalServerError)
return
}
img := bimg.NewImage(pandaFile)
img.SmartCrop(width, height)
buf = img.Image()
w.Header().Set("X-Cache-Info", "MISS")
go func() {
err := uploadPanda(key, buf)
if err != nil {
log.Printf("Error uploading cached image: %v", err)
}
}()
}
writeResponse(buf, w)
}
func writeResponse(buf []byte, w http.ResponseWriter) (err error) {
w.Header().Set("Content-Type", "image/jpeg")
w.Header().Set("Cache-Control", "public, max-age=15552000")
w.Header().Set("Vary", "DPR, Accept")
w.WriteHeader(http.StatusOK)
_, err = w.Write(buf)
return
}
func checkCache(width, height int) bool {
key := fmt.Sprintf("cache/%v/%v.jpg", width, height)
info, err := getPandaInfo(key)
if err != nil || info == nil {
return false
}
return true
}