-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
423 lines (349 loc) · 11 KB
/
request.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright 2024 Jeremy Collins. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dims
import (
"crypto/md5"
"crypto/sha256"
"errors"
"fmt"
"gopkg.in/gographics/imagick.v3/imagick"
"hash"
"io"
"log/slog"
"net/http"
"net/url"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)
type Kernel interface {
ValidateSignature() bool
FetchImage() error
ProcessImage() (string, []byte, error)
SendHeaders(w http.ResponseWriter)
SendImage(w http.ResponseWriter, status int, imageType string, imageBlob []byte) error
SendError(w http.ResponseWriter, status int, message string)
}
type Request struct {
Id string // The hash of the request -> hash(clientId + commands + imageUrl).
Signature string // The signature of the request.
Config Config // The global configuration.
ClientId string // The client ID of this request.
ImageUrl string // The image URL that is being manipulated.
SendContentDisposition bool // The content disposition of the request.
Commands []Command // The commands (resize, crop, etc).
Error bool // Whether the error image is being served.
SourceImage Image // The source image.
}
// FetchImage downloads the image from the given URL.
func (r *Request) FetchImage() error {
slog.Info("downloadImage", "url", r.ImageUrl)
timeout := time.Duration(r.Config.Timeout.Download) * time.Millisecond
r.SourceImage = _fetchImage(r.ImageUrl, timeout)
if r.SourceImage.Status != 200 {
return fmt.Errorf("failed to download image")
}
return nil
}
func _fetchImage(imageUrl string, timeout time.Duration) Image {
_, err := url.ParseRequestURI(imageUrl)
if err != nil {
return Image{
Status: 400,
}
}
http.DefaultClient.Timeout = timeout
image, err := http.Get(imageUrl)
if err != nil {
return Image{
Status: 500,
}
}
sourceImage := Image{
Status: image.StatusCode,
EdgeControl: image.Header.Get("Edge-Control"),
CacheControl: image.Header.Get("Cache-Control"),
LastModified: image.Header.Get("Last-Modified"),
Etag: image.Header.Get("Etag"),
Format: image.Header.Get("Content-Type"),
}
sourceImage.Size = int(image.ContentLength)
sourceImage.Bytes, _ = io.ReadAll(image.Body)
return sourceImage
}
// Parse through the requested commands and set the optimal image size on the MagicWand.
//
// This is used while reading an image to improve
// performance when generating thumbnails from very
// large images.
//
// An example speed is taking 1817x3000 sized image and
// reducing it to a 78x110 thumbnail:
//
// without MagickSetSize: 396ms
// with MagickSetSize: 105ms
func (r *Request) setOptimalImageSize(mw *imagick.MagickWand) {
for _, command := range r.Commands {
if command.Name == "thumbnail" || command.Name == "resize" {
var rect imagick.RectangleInfo
flags := imagick.ParseAbsoluteGeometry(command.Args, &rect)
if (flags&imagick.WIDTHVALUE != 0) &&
(flags&imagick.HEIGHTVALUE != 0) &&
(flags&imagick.PERCENTVALUE == 0) {
if err := mw.SetSize(rect.Width, rect.Height); err != nil {
slog.Error("setOptimalImageSize failed.", "error", err)
}
return
}
}
}
}
// ProcessImage will execute the commands on the image.
func (r *Request) ProcessImage() (string, []byte, error) {
slog.Debug("executeImagemagick")
mw := imagick.NewMagickWand()
defer mw.Destroy()
// Read the image.
r.setOptimalImageSize(mw)
err := mw.ReadImageBlob(r.SourceImage.Bytes)
if err != nil {
return "", nil, err
}
// Convert image to RGB from CMYK.
if mw.GetImageColorspace() == imagick.COLORSPACE_CMYK {
profiles := mw.GetImageProfiles("icc")
if profiles != nil {
if err := mw.ProfileImage("ICC", CmykIccProfile); err != nil {
return "", nil, err
}
}
if err := mw.ProfileImage("ICC", RgbIccProfile); err != nil {
return "", nil, err
}
err = mw.TransformImageColorspace(imagick.COLORSPACE_RGB)
if err != nil {
return "", nil, err
}
}
// Flip image orientation, if needed.
if err := mw.AutoOrientImage(); err != nil {
return "", nil, err
}
// Execute the commands.
stripMetadata := true
formatProvided := false
for _, command := range r.Commands {
if command.Name == "strip" {
stripMetadata = false
}
if command.Name == "format" {
formatProvided = true
}
if err := command.Operation(mw, command.Args); err != nil {
return "", nil, err
}
mw.MergeImageLayers(imagick.IMAGE_LAYER_TRIM_BOUNDS)
}
// Strip metadata. (if not already stripped)
if stripMetadata && r.Config.StripMetadata {
if err := mw.StripImage(); err != nil {
return "", nil, err
}
}
// Set output format if not provided in the request.
if !formatProvided && r.Config.OutputFormat.OutputFormat != "" {
format := strings.ToLower(mw.GetImageFormat())
if !contains(r.Config.OutputFormat.Exclude, format) {
if err := mw.SetImageFormat(r.Config.OutputFormat.OutputFormat); err != nil {
return "", nil, err
}
}
}
mw.ResetIterator()
return mw.GetImageFormat(), mw.GetImagesBlob(), nil
}
func (r *Request) SendHeaders(w http.ResponseWriter) {
maxAge := r.Config.OriginCacheControl.Default
edgeControlTtl := r.Config.EdgeControl.DownstreamTtl
if r.Config.OriginCacheControl.UseOrigin {
originMaxAge, err := sourceMaxAge(r.SourceImage.CacheControl)
if err == nil {
maxAge = originMaxAge
// If below minimum, set to minimum.
minCacheAge := r.Config.OriginCacheControl.Min
if minCacheAge != 0 && maxAge <= minCacheAge {
maxAge = minCacheAge
}
// If above maximum, set to maximum.
maxCacheAge := r.Config.OriginCacheControl.Max
if maxCacheAge != 0 && maxAge >= maxCacheAge {
maxAge = maxCacheAge
}
}
}
if r.Error {
maxAge = r.Config.OriginCacheControl.Error
}
// Set cache headers.
if maxAge > 0 {
slog.Debug("sendImage", "maxAge", maxAge)
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, public", maxAge))
w.Header().Set("Expires",
fmt.Sprintf("%s", time.Now().
Add(time.Duration(maxAge)*time.Second).
UTC().
Format(http.TimeFormat)))
}
if edgeControlTtl > 0 {
w.Header().Set("Edge-Control", fmt.Sprintf("downstream-ttl=%d", edgeControlTtl))
}
// Set content disposition.
if r.SendContentDisposition {
// Grab filename from imageUrl
u, err := url.Parse(r.ImageUrl)
if err != nil {
return
}
filename := filepath.Base(u.Path)
slog.Debug("sendImage", "sendContentDisposition", r.SendContentDisposition, "filename", filename)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
}
// Set etag header.
if r.SourceImage.Etag != "" || r.SourceImage.LastModified != "" {
var h hash.Hash
if r.Config.Signing.EtagAlgorithm == "md5" {
h = md5.New()
} else {
h = sha256.New()
}
h.Write([]byte(r.Id))
if r.SourceImage.Etag != "" {
h.Write([]byte(r.SourceImage.Etag))
} else if r.SourceImage.LastModified != "" {
h.Write([]byte(r.SourceImage.LastModified))
}
etag := fmt.Sprintf("%x", h.Sum(nil))
w.Header().Set("Etag", etag)
}
}
func (r *Request) SendImage(w http.ResponseWriter, status int, imageFormat string, imageBlob []byte) error {
slog.Info("SendImage", "status", status, "format", imageFormat, "size", len(imageBlob))
if imageBlob == nil {
return fmt.Errorf("image is empty")
}
// Set content type.
w.Header().Set("Content-Type", fmt.Sprintf("image/%s", strings.ToLower(imageFormat)))
// Set content length
w.Header().Set("Content-Length", strconv.Itoa(len(imageBlob)))
// Set status code.
w.WriteHeader(status)
// Write the image.
_, err := w.Write(imageBlob)
if err != nil {
return err
}
return nil
}
func ImageFromMagickWand(mw *imagick.MagickWand) Image {
return Image{
Bytes: mw.GetImagesBlob(),
Format: mw.GetImageFormat(),
EdgeControl: mw.GetImageProperty("Edge-Control"),
CacheControl: mw.GetImageProperty("Cache-Control"),
LastModified: mw.GetImageProperty("Last-Modified"),
Etag: mw.GetImageProperty("Etag"),
Size: len(mw.GetImagesBlob()),
Status: 200,
}
}
func (r *Request) SendError(w http.ResponseWriter, status int, message string) {
slog.Info("sendError", "status", status, "message", message)
mw := imagick.NewMagickWand()
defer mw.Destroy()
pw := imagick.NewPixelWand()
defer pw.Destroy()
pw.SetColor(r.Config.Error.Background)
if err := mw.NewImage(1, 1, pw); err != nil {
slog.Error("sendError failed.", "error", err)
return
}
// Execute the commands on the placeholder image, giving it the same dimensions as the requested image.
for _, command := range r.Commands {
if command.Name == "crop" || command.Name == "thumbnail" {
var rect imagick.RectangleInfo
imagick.ParseAbsoluteGeometry(command.Args, &rect)
if rect.Width > 0 && rect.Height == 0 {
command.Args = fmt.Sprintf("%d", rect.Height)
} else if rect.Height > 0 && rect.Width == 0 {
command.Args = fmt.Sprintf("x%d", rect.Width)
} else if rect.Width > 0 && rect.Height > 0 {
command.Args = fmt.Sprintf("%dx%d", rect.Width, rect.Height)
}
command.Name = "resize"
command.Operation = func(mw *imagick.MagickWand, args string) error {
var rect imagick.RectangleInfo
imagick.SetGeometry(mw.GetImageFromMagickWand(), &rect)
flags := imagick.ParseMetaGeometry(args, &rect.X, &rect.Y, &rect.Width, &rect.Height)
if (flags & imagick.ALLVALUES) != 0 {
return mw.ScaleImage(rect.Width, rect.Height)
}
return nil
}
}
if err := command.Operation(mw, command.Args); err != nil {
return
}
}
// Set the format for the error image.
format := mw.GetImageFormat()
if format == "" && r.Config.OutputFormat.OutputFormat != "" {
format = r.Config.OutputFormat.OutputFormat
} else if format == "" {
format = "png"
}
if err := mw.SetImageFormat(format); err != nil {
slog.Error("sendError failed.", "error", err)
return
}
r.Error = true
err := r.SendImage(w, status, format, mw.GetImageBlob())
if err != nil {
slog.Error("sendError failed.", "error", err)
}
}
func sourceMaxAge(header string) (int, error) {
if header == "" {
return 0, errors.New("empty header")
}
pattern, _ := regexp.Compile(`max-age=(\d+)`)
match := pattern.FindStringSubmatch(header)
if len(match) == 1 {
sourceMaxAge, err := strconv.Atoi(match[0])
if err != nil {
return 0, errors.New("unable to convert to int")
}
return sourceMaxAge, nil
}
return 0, errors.New("max-age not found in header")
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}