-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandle.go
186 lines (149 loc) · 4.27 KB
/
handle.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
package main
import (
"bytes"
"context"
"errors"
"net/http"
"net/url"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/smithy-go"
"github.com/go-resty/resty/v2"
"github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog/log"
)
func NewHandler() Handle {
h := Handle{
cache: NewCache(),
client: resty.New(),
cachedCounter: prometheus.NewCounter(
prometheus.CounterOpts{
Name: "chii_img_cached_request_count",
Help: "Count of cached image request",
},
),
requestCounter: prometheus.NewCounter(
prometheus.CounterOpts{
Name: "chii_img_all_request_count",
Help: "Count of all image request",
},
),
cachedRequestHist: prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "chii_img_cached_request_duration_seconds",
Buckets: []float64{.005, .01, .025, .05, .1, .2, .3, .4, .5, 0.75, 1, 2},
}),
uncachedRequestHist: prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "chii_img_uncached_request_duration_seconds",
Buckets: []float64{.005, .01, .025, .05, .1, .2, .3, .4, .5, 0.75, 1, 2, 3, 4, 5, 7.5, 10},
}),
}
func() {
_, err := h.cache.s3.HeadBucket(context.Background(), &s3.HeadBucketInput{Bucket: &s3bucket})
if err == nil {
return
}
var e smithy.APIError
if !errors.As(err, &e) {
panic(err)
}
if e.ErrorCode() != "NoSuchBucket" && e.ErrorCode() != "NotFound" && e.ErrorCode() != "NoSuchBucketException" {
panic(err)
}
_, err = h.cache.s3.CreateBucket(context.Background(), &s3.CreateBucketInput{Bucket: &s3bucket})
if err != nil {
panic(err)
}
}()
return h
}
type Handle struct {
cache *Cache
client *resty.Client
cachedCounter prometheus.Counter
requestCounter prometheus.Counter
cachedRequestHist prometheus.Histogram
uncachedRequestHist prometheus.Histogram
}
func (h Handle) fetchRawImage(ctx context.Context, p string, hd bool) ([]byte, string, error) {
s3Path := strings.TrimPrefix(p, "pic/")
if hd {
s3Path = "hd/" + s3Path
}
// 生产环境走的是内网,不能用 https
sourceURL := "http://lain.bgm.tv/" + p
if hd {
sourceURL += "?hd=1"
}
img, err := h.client.R().SetContext(ctx).Get(sourceURL)
if err != nil {
return nil, "", err
}
if img.StatusCode() == 404 {
return nil, "", echo.NewHTTPError(http.StatusNotFound, "image not found")
}
if img.StatusCode() >= 300 {
return nil, "", echo.NewHTTPError(http.StatusInternalServerError, img.String())
}
return img.Body(), img.Header().Get(echo.HeaderContentType), nil
}
func (h Handle) processImage(c echo.Context, upstream *url.URL, p string, size Size, hd bool) (Image, error) {
cachedPath := localCacheFilePath(p, size, hd)
ctx := c.Request().Context()
return h.withS3Cached(c, cachedPath, func() (Image, error) {
img, ct, err := h.fetchRawImage(ctx, p, hd)
if err != nil {
return Image{}, err
}
action := "smartcrop"
if size.Height == 0 || size.Width == 0 {
action = "resize"
}
qs := url.Values{
"height": {strconv.FormatUint(size.Height, 10)},
"width": {strconv.FormatUint(size.Width, 10)},
"field": {"file"},
}
switch path.Ext(path.Base(p)) {
case ".jpg":
qs.Set("type", "jpeg")
case ".webp":
qs.Set("type", "webp")
}
upstreamUrl := upstream.String() + "/" + action + "?" + qs.Encode()
resp, err := h.client.R().SetContext(ctx).
SetMultipartField("file", filepath.Base(p), ct, bytes.NewBuffer(img)).
Post(upstreamUrl)
if err != nil {
return Image{}, err
}
content := resp.Body()
contentType := resp.Header().Get(echo.HeaderContentType)
if resp.StatusCode() > 300 {
return Image{}, echo.NewHTTPError(http.StatusInternalServerError, "failed to process image: "+resp.String())
}
return Image{body: content, contentType: contentType}, nil
})
}
func (h Handle) withS3Cached(c echo.Context, filepath string, getter func() (Image, error)) (Image, error) {
ctx := c.Request().Context()
item, cached, err := h.cache.Get(ctx, filepath)
if err != nil {
return Image{}, err
}
c.Set("cached", cached)
if cached {
return item, nil
}
image, err := getter()
if err != nil {
return Image{}, err
}
if err := h.cache.Set(ctx, filepath, image); err != nil {
log.Err(err).Msg("failed to set cache")
}
return image, nil
}