-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_handler.go
73 lines (62 loc) · 1.74 KB
/
image_handler.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
package silverdile
import (
"fmt"
"net/http"
"strings"
"cloud.google.com/go/storage"
"github.com/morikuni/failure"
"github.com/sinmetalcraft/goma"
)
func ImageHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
l := strings.Split(r.URL.Path, "/")
if len(l) < 4 {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("invalid argument"))
if err != nil {
fmt.Printf("failed write to response. err%+v", err)
}
}
o, err := BuildImageOption(strings.Join(l[1:], "/"))
if failure.Is(err, InvalidArgument) {
fmt.Printf("404: %+v\n", err)
w.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
fmt.Printf("failed %+v\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
gcs, err := storage.NewClient(ctx)
if err != nil {
fmt.Printf("failed storage.NewClient %+v\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
s := goma.NewStorageService(ctx, gcs)
img, gt, err := s.Read(ctx, o.Bucket, o.Object)
if err != nil {
fmt.Printf("failed goma.Read bucket=%v,object=%v err=%+v\n", o.Bucket, o.Object, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
const min = 0
const max = 2560
if o.Size < min || max <= o.Size {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte(fmt.Sprintf("Size ranges from %d to %d", min, max)))
if err != nil {
fmt.Printf("failed write to response. err=%+v\n", err)
}
return
}
if o.Size > 0 {
img = goma.ResizeToFitLongSide(img, o.Size)
}
w.Header().Set("cache-control", "public, max-age=3600")
w.Header().Set("content-type", gt.ContentType)
w.WriteHeader(http.StatusOK)
if err := goma.Write(w, img, gt.FormatType); err != nil {
fmt.Printf("failed goma.Write to response. err=%+v\n", err)
}
}