This repository has been archived by the owner on Jul 4, 2021. It is now read-only.
generated from traefik/plugindemo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
image_optimizer.go
172 lines (142 loc) · 3.45 KB
/
image_optimizer.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
// Package imageopti Image optimizer plugin
package imageopti
import (
"bytes"
"context"
"errors"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/agravelot/imageopti/cache"
"github.com/agravelot/imageopti/config"
"github.com/agravelot/imageopti/processor"
)
// Config the plugin configuration.
type Config struct {
config.Config
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{
config.Config{
Processor: "",
Cache: "",
Imaginary: config.ImaginaryProcessorConfig{URL: ""},
Redis: config.RedisCacheConfig{URL: ""},
File: config.FileCacheConfig{Path: ""},
},
}
}
// ImageOptimizer middleware plugin struct.
type ImageOptimizer struct {
next http.Handler
name string
p processor.Processor
c cache.Cache
}
// New created a new ImageOptimizer plugin.
func New(ctx context.Context, next http.Handler, conf *Config, name string) (http.Handler, error) {
log.Println("Loading image optimization plugin...")
if conf.Processor == "" {
return nil, fmt.Errorf("processor must be defined")
}
c, err := cache.New(conf.Config)
if err != nil {
panic(err)
}
p, err := processor.New(conf.Config)
if err != nil {
panic(err)
}
return &ImageOptimizer{
p: p,
c: c,
next: next,
name: name,
}, nil
}
const (
contentLength = "Content-Length"
contentType = "Content-Type"
cacheStatus = "Cache-Status"
cacheHitStatus = "hit"
cacheMissStatus = "miss"
cacheExpiry = 100 * time.Second
targetFormat = "image/webp"
)
func (a *ImageOptimizer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// TODO Check if cacheable
key, err := cache.Tokenize(req)
if err != nil {
panic(err)
}
// Return cached result here.
if v, err := a.c.Get(key); err == nil {
rw.Header().Set(contentLength, fmt.Sprint(len(v)))
rw.Header().Set(contentType, targetFormat)
rw.Header().Set(cacheStatus, cacheHitStatus)
_, err = rw.Write(v)
if err != nil {
panic(err)
}
return
}
wrappedWriter := &responseWriter{
ResponseWriter: rw,
bypassHeader: true,
wroteHeader: false,
buffer: bytes.Buffer{},
}
a.next.ServeHTTP(wrappedWriter, req)
wrappedWriter.bypassHeader = false
bodyBytes := wrappedWriter.buffer.Bytes()
// If not image response, forward original and leave it here.
if !isImageResponse(rw) {
_, err = rw.Write(bodyBytes)
if err != nil {
panic(err)
}
return
}
width, err := imageWidthRequest(req)
if err != nil {
panic(err)
}
optimized, ct, err := a.p.Optimize(bodyBytes, rw.Header().Get(contentType), targetFormat, 75, width)
if err != nil {
panic(err)
}
rw.Header().Set(contentLength, fmt.Sprint(len(optimized)))
rw.Header().Set(contentType, ct)
rw.Header().Set(cacheStatus, cacheMissStatus)
_, err = rw.Write(optimized)
if err != nil {
panic(err)
}
err = a.c.Set(key, optimized, cacheExpiry)
if err != nil {
panic(err)
}
}
func imageWidthRequest(req *http.Request) (int, error) {
w := req.URL.Query().Get("w")
// if no query param
if len(w) == 0 {
return 0, nil
}
v, err := strconv.Atoi(w)
if err != nil {
return 0, fmt.Errorf("unable to convert w query param to int: %w", err)
}
if v < 0 {
return 0, errors.New("width cannot be negative value")
}
return v, nil
}
// isImageResponse Determine with Content-Type header if the response is an image.
func isImageResponse(rw http.ResponseWriter) bool {
return strings.HasPrefix(rw.Header().Get(contentType), "image/")
}