2018-09-07 15:37:25 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-10-05 17:17:36 +02:00
|
|
|
"context"
|
2018-09-07 15:37:25 +02:00
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-05-08 13:42:48 +02:00
|
|
|
"net/http"
|
2018-11-02 17:35:21 +02:00
|
|
|
"net/url"
|
2018-10-04 14:27:49 +02:00
|
|
|
"regexp"
|
2018-09-07 15:37:25 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-09-07 19:41:06 +02:00
|
|
|
type urlOptions map[string][]string
|
|
|
|
|
2018-11-02 18:38:41 +02:00
|
|
|
type processingHeaders struct {
|
|
|
|
Accept string
|
|
|
|
Width string
|
|
|
|
ViewportWidth string
|
2018-11-14 15:46:59 +02:00
|
|
|
DPR string
|
2018-11-02 18:38:41 +02:00
|
|
|
}
|
|
|
|
|
2018-09-07 16:45:57 +02:00
|
|
|
type gravityType int
|
|
|
|
|
|
|
|
const (
|
2019-06-07 20:14:41 +02:00
|
|
|
gravityUnknown gravityType = iota
|
|
|
|
gravityCenter
|
2018-09-07 16:45:57 +02:00
|
|
|
gravityNorth
|
|
|
|
gravityEast
|
|
|
|
gravitySouth
|
|
|
|
gravityWest
|
2018-10-19 11:47:11 +02:00
|
|
|
gravityNorthWest
|
|
|
|
gravityNorthEast
|
|
|
|
gravitySouthWest
|
|
|
|
gravitySouthEast
|
2018-09-07 16:45:57 +02:00
|
|
|
gravitySmart
|
2018-09-11 13:23:28 +02:00
|
|
|
gravityFocusPoint
|
2018-09-07 16:45:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var gravityTypes = map[string]gravityType{
|
2018-10-19 11:47:11 +02:00
|
|
|
"ce": gravityCenter,
|
|
|
|
"no": gravityNorth,
|
|
|
|
"ea": gravityEast,
|
|
|
|
"so": gravitySouth,
|
|
|
|
"we": gravityWest,
|
|
|
|
"nowe": gravityNorthWest,
|
|
|
|
"noea": gravityNorthEast,
|
|
|
|
"sowe": gravitySouthWest,
|
|
|
|
"soea": gravitySouthEast,
|
|
|
|
"sm": gravitySmart,
|
|
|
|
"fp": gravityFocusPoint,
|
2018-09-11 13:23:28 +02:00
|
|
|
}
|
|
|
|
|
2018-09-07 16:45:57 +02:00
|
|
|
type resizeType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
resizeFit resizeType = iota
|
|
|
|
resizeFill
|
|
|
|
resizeCrop
|
|
|
|
)
|
|
|
|
|
2018-09-07 15:37:25 +02:00
|
|
|
var resizeTypes = map[string]resizeType{
|
2018-09-07 16:45:57 +02:00
|
|
|
"fit": resizeFit,
|
|
|
|
"fill": resizeFill,
|
|
|
|
"crop": resizeCrop,
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2019-01-11 17:08:34 +02:00
|
|
|
type rgbColor struct{ R, G, B uint8 }
|
2018-10-02 14:20:23 +02:00
|
|
|
|
2018-10-04 14:27:49 +02:00
|
|
|
var hexColorRegex = regexp.MustCompile("^([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$")
|
|
|
|
|
|
|
|
const (
|
|
|
|
hexColorLongFormat = "%02x%02x%02x"
|
|
|
|
hexColorShortFormat = "%1x%1x%1x"
|
|
|
|
)
|
|
|
|
|
2019-06-20 14:49:25 +02:00
|
|
|
type gravityOptions struct {
|
|
|
|
Type gravityType
|
|
|
|
X, Y float64
|
|
|
|
}
|
|
|
|
|
2019-06-07 20:14:41 +02:00
|
|
|
type cropOptions struct {
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
Gravity gravityOptions
|
|
|
|
}
|
|
|
|
|
2018-10-19 11:47:11 +02:00
|
|
|
type watermarkOptions struct {
|
|
|
|
Enabled bool
|
|
|
|
Opacity float64
|
|
|
|
Replicate bool
|
|
|
|
Gravity gravityType
|
|
|
|
OffsetX int
|
|
|
|
OffsetY int
|
2018-10-28 14:05:57 +02:00
|
|
|
Scale float64
|
2018-10-19 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
2018-09-07 15:37:25 +02:00
|
|
|
type processingOptions struct {
|
2018-10-19 11:47:11 +02:00
|
|
|
Resize resizeType
|
|
|
|
Width int
|
|
|
|
Height int
|
2018-11-15 13:19:27 +02:00
|
|
|
Dpr float64
|
2018-10-19 11:47:11 +02:00
|
|
|
Gravity gravityOptions
|
|
|
|
Enlarge bool
|
2019-06-17 12:15:10 +02:00
|
|
|
Extend bool
|
2019-06-07 20:14:41 +02:00
|
|
|
Crop cropOptions
|
2018-10-19 11:47:11 +02:00
|
|
|
Format imageType
|
2018-10-30 14:20:02 +02:00
|
|
|
Quality int
|
2018-10-19 11:47:11 +02:00
|
|
|
Flatten bool
|
2019-01-11 17:08:34 +02:00
|
|
|
Background rgbColor
|
2018-10-19 11:47:11 +02:00
|
|
|
Blur float32
|
|
|
|
Sharpen float32
|
|
|
|
|
2018-10-29 10:54:30 +02:00
|
|
|
CacheBuster string
|
|
|
|
|
2018-10-19 11:47:11 +02:00
|
|
|
Watermark watermarkOptions
|
|
|
|
|
2018-10-04 14:02:24 +02:00
|
|
|
UsedPresets []string
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 17:17:36 +02:00
|
|
|
const (
|
|
|
|
imageURLCtxKey = ctxKey("imageUrl")
|
|
|
|
processingOptionsCtxKey = ctxKey("processingOptions")
|
2018-11-02 17:35:21 +02:00
|
|
|
urlTokenPlain = "plain"
|
2018-11-15 13:19:27 +02:00
|
|
|
maxClientHintDPR = 8
|
2018-11-20 14:53:44 +02:00
|
|
|
|
|
|
|
msgForbidden = "Forbidden"
|
|
|
|
msgInvalidURL = "Invalid URL"
|
2018-10-05 17:17:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-11-02 17:35:21 +02:00
|
|
|
errInvalidImageURL = errors.New("Invalid image url")
|
2018-10-05 17:17:36 +02:00
|
|
|
errInvalidURLEncoding = errors.New("Invalid url encoding")
|
|
|
|
errResultingImageFormatIsNotSupported = errors.New("Resulting image format is not supported")
|
2018-11-20 14:53:44 +02:00
|
|
|
errInvalidPath = newError(404, "Invalid path", msgInvalidURL)
|
2018-10-05 17:17:36 +02:00
|
|
|
)
|
|
|
|
|
2018-09-11 13:48:16 +02:00
|
|
|
func (gt gravityType) String() string {
|
|
|
|
for k, v := range gravityTypes {
|
|
|
|
if v == gt {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt resizeType) String() string {
|
|
|
|
for k, v := range resizeTypes {
|
|
|
|
if v == rt {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:35:21 +02:00
|
|
|
func (po *processingOptions) isPresetUsed(name string) bool {
|
|
|
|
for _, usedName := range po.UsedPresets {
|
|
|
|
if usedName == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (po *processingOptions) presetUsed(name string) {
|
|
|
|
po.UsedPresets = append(po.UsedPresets, name)
|
|
|
|
}
|
|
|
|
|
2019-01-11 17:08:34 +02:00
|
|
|
func colorFromHex(hexcolor string) (rgbColor, error) {
|
|
|
|
c := rgbColor{}
|
2018-10-04 14:27:49 +02:00
|
|
|
|
|
|
|
if !hexColorRegex.MatchString(hexcolor) {
|
|
|
|
return c, fmt.Errorf("Invalid hex color: %s", hexcolor)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hexcolor) == 3 {
|
|
|
|
fmt.Sscanf(hexcolor, hexColorShortFormat, &c.R, &c.G, &c.B)
|
|
|
|
c.R *= 17
|
|
|
|
c.G *= 17
|
|
|
|
c.B *= 17
|
|
|
|
} else {
|
|
|
|
fmt.Sscanf(hexcolor, hexColorLongFormat, &c.R, &c.G, &c.B)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:35:21 +02:00
|
|
|
func decodeBase64URL(parts []string) (string, string, error) {
|
|
|
|
var format string
|
|
|
|
|
|
|
|
urlParts := strings.Split(strings.Join(parts, ""), ".")
|
|
|
|
|
|
|
|
if len(urlParts) > 2 {
|
|
|
|
return "", "", errInvalidURLEncoding
|
2018-10-04 14:02:24 +02:00
|
|
|
}
|
|
|
|
|
2018-11-02 17:35:21 +02:00
|
|
|
if len(urlParts) == 2 && len(urlParts[1]) > 0 {
|
|
|
|
format = urlParts[1]
|
|
|
|
}
|
|
|
|
|
2019-01-14 12:09:14 +02:00
|
|
|
imageURL, err := base64.RawURLEncoding.DecodeString(strings.TrimRight(urlParts[0], "="))
|
2018-11-02 17:35:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", "", errInvalidURLEncoding
|
|
|
|
}
|
|
|
|
|
2019-05-08 13:42:48 +02:00
|
|
|
fullURL := fmt.Sprintf("%s%s", conf.BaseURL, string(imageURL))
|
2018-11-02 17:35:21 +02:00
|
|
|
|
|
|
|
if _, err := url.ParseRequestURI(fullURL); err != nil {
|
|
|
|
return "", "", errInvalidImageURL
|
|
|
|
}
|
|
|
|
|
|
|
|
return fullURL, format, nil
|
2018-10-04 14:02:24 +02:00
|
|
|
}
|
|
|
|
|
2018-11-02 17:35:21 +02:00
|
|
|
func decodePlainURL(parts []string) (string, string, error) {
|
|
|
|
var format string
|
2018-09-07 15:37:25 +02:00
|
|
|
|
2018-11-02 17:35:21 +02:00
|
|
|
urlParts := strings.Split(strings.Join(parts, "/"), "@")
|
2018-09-07 15:37:25 +02:00
|
|
|
|
|
|
|
if len(urlParts) > 2 {
|
2018-10-05 17:17:36 +02:00
|
|
|
return "", "", errInvalidURLEncoding
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2018-11-02 17:35:21 +02:00
|
|
|
if len(urlParts) == 2 && len(urlParts[1]) > 0 {
|
|
|
|
format = urlParts[1]
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2018-11-02 17:35:21 +02:00
|
|
|
if unescaped, err := url.PathUnescape(urlParts[0]); err == nil {
|
|
|
|
fullURL := fmt.Sprintf("%s%s", conf.BaseURL, unescaped)
|
|
|
|
if _, err := url.ParseRequestURI(fullURL); err == nil {
|
2019-05-08 13:42:48 +02:00
|
|
|
return fullURL, format, nil
|
2018-11-02 17:35:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", "", errInvalidImageURL
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeURL(parts []string) (string, string, error) {
|
|
|
|
if len(parts) == 0 {
|
2018-10-05 17:17:36 +02:00
|
|
|
return "", "", errInvalidURLEncoding
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2018-11-02 17:35:21 +02:00
|
|
|
if parts[0] == urlTokenPlain && len(parts) > 1 {
|
|
|
|
return decodePlainURL(parts[1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
return decodeBase64URL(parts)
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 20:14:41 +02:00
|
|
|
func parseDimension(d *int, name, arg string) error {
|
|
|
|
if v, err := strconv.Atoi(arg); err == nil && v >= 0 {
|
|
|
|
*d = v
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid %s: %s", name, arg)
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 20:14:41 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-20 14:49:25 +02:00
|
|
|
func isGravityOffcetValid(gravity gravityType, offset float64) bool {
|
|
|
|
if gravity == gravityCenter {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset >= 0 && (gravity != gravityFocusPoint || offset <= 1)
|
|
|
|
}
|
|
|
|
|
2019-06-07 20:14:41 +02:00
|
|
|
func parseGravity(g *gravityOptions, args []string) error {
|
2019-06-20 14:49:25 +02:00
|
|
|
nArgs := len(args)
|
|
|
|
|
|
|
|
if nArgs > 3 {
|
|
|
|
return fmt.Errorf("Invalid gravity arguments: %v", args)
|
|
|
|
}
|
|
|
|
|
2019-06-07 20:14:41 +02:00
|
|
|
if t, ok := gravityTypes[args[0]]; ok {
|
|
|
|
g.Type = t
|
2018-09-07 15:37:25 +02:00
|
|
|
} else {
|
2019-06-07 20:14:41 +02:00
|
|
|
return fmt.Errorf("Invalid gravity: %s", args[0])
|
|
|
|
}
|
|
|
|
|
2019-06-20 14:49:25 +02:00
|
|
|
if g.Type == gravitySmart && nArgs > 1 {
|
|
|
|
return fmt.Errorf("Invalid gravity arguments: %v", args)
|
|
|
|
} else if g.Type == gravityFocusPoint && nArgs != 3 {
|
|
|
|
return fmt.Errorf("Invalid gravity arguments: %v", args)
|
|
|
|
}
|
2019-06-07 20:14:41 +02:00
|
|
|
|
2019-06-20 14:49:25 +02:00
|
|
|
if nArgs > 1 {
|
|
|
|
if x, err := strconv.ParseFloat(args[1], 64); err == nil && isGravityOffcetValid(g.Type, x) {
|
2019-06-07 20:14:41 +02:00
|
|
|
g.X = x
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid gravity X: %s", args[1])
|
|
|
|
}
|
2019-06-20 14:49:25 +02:00
|
|
|
}
|
2019-06-07 20:14:41 +02:00
|
|
|
|
2019-06-20 14:49:25 +02:00
|
|
|
if nArgs > 2 {
|
|
|
|
if y, err := strconv.ParseFloat(args[2], 64); err == nil && isGravityOffcetValid(g.Type, y) {
|
2019-06-07 20:14:41 +02:00
|
|
|
g.Y = y
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid gravity Y: %s", args[2])
|
|
|
|
}
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-07 20:14:41 +02:00
|
|
|
func applyWidthOption(po *processingOptions, args []string) error {
|
2018-09-07 15:37:25 +02:00
|
|
|
if len(args) > 1 {
|
2019-06-07 20:14:41 +02:00
|
|
|
return fmt.Errorf("Invalid width arguments: %v", args)
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 20:14:41 +02:00
|
|
|
return parseDimension(&po.Width, "width", args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyHeightOption(po *processingOptions, args []string) error {
|
|
|
|
if len(args) > 1 {
|
|
|
|
return fmt.Errorf("Invalid height arguments: %v", args)
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 20:14:41 +02:00
|
|
|
return parseDimension(&po.Height, "height", args[0])
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func applyEnlargeOption(po *processingOptions, args []string) error {
|
|
|
|
if len(args) > 1 {
|
|
|
|
return fmt.Errorf("Invalid enlarge arguments: %v", args)
|
|
|
|
}
|
|
|
|
|
|
|
|
po.Enlarge = args[0] != "0"
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 17:55:20 +02:00
|
|
|
func applyExtendOption(po *processingOptions, args []string) error {
|
|
|
|
if len(args) > 1 {
|
2019-06-17 12:15:10 +02:00
|
|
|
return fmt.Errorf("Invalid extend arguments: %v", args)
|
2019-02-21 17:55:20 +02:00
|
|
|
}
|
|
|
|
|
2019-06-17 12:15:10 +02:00
|
|
|
po.Extend = args[0] != "0"
|
2019-02-21 17:55:20 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:37:25 +02:00
|
|
|
func applySizeOption(po *processingOptions, args []string) (err error) {
|
2019-02-21 17:55:20 +02:00
|
|
|
if len(args) > 4 {
|
2018-09-07 15:37:25 +02:00
|
|
|
return fmt.Errorf("Invalid size arguments: %v", args)
|
|
|
|
}
|
|
|
|
|
2018-10-03 17:33:22 +02:00
|
|
|
if len(args) >= 1 && len(args[0]) > 0 {
|
2018-09-07 15:37:25 +02:00
|
|
|
if err = applyWidthOption(po, args[0:1]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 17:33:22 +02:00
|
|
|
if len(args) >= 2 && len(args[1]) > 0 {
|
2018-09-07 15:37:25 +02:00
|
|
|
if err = applyHeightOption(po, args[1:2]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 17:55:20 +02:00
|
|
|
if len(args) >= 3 && len(args[2]) > 0 {
|
2018-09-07 15:37:25 +02:00
|
|
|
if err = applyEnlargeOption(po, args[2:3]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 17:55:20 +02:00
|
|
|
if len(args) == 4 && len(args[3]) > 0 {
|
|
|
|
if err = applyExtendOption(po, args[3:4]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:37:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-03 17:34:13 +02:00
|
|
|
func applyResizingTypeOption(po *processingOptions, args []string) error {
|
|
|
|
if len(args) > 1 {
|
|
|
|
return fmt.Errorf("Invalid resizing type arguments: %v", args)
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if r, ok := resizeTypes[args[0]]; ok {
|
|
|
|
po.Resize = r
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid resize type: %s", args[0])
|
|
|
|
}
|
|
|
|
|
2018-10-03 17:34:13 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyResizeOption(po *processingOptions, args []string) error {
|
2019-02-21 17:55:20 +02:00
|
|
|
if len(args) > 5 {
|
2018-10-03 17:34:13 +02:00
|
|
|
return fmt.Errorf("Invalid resize arguments: %v", args)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args[0]) > 0 {
|
|
|
|
if err := applyResizingTypeOption(po, args[0:1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:37:25 +02:00
|
|
|
if len(args) > 1 {
|
|
|
|
if err := applySizeOption(po, args[1:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:19:27 +02:00
|
|
|
func applyDprOption(po *processingOptions, args []string) error {
|
|
|
|
if len(args) > 1 {
|
|
|
|
return fmt.Errorf("Invalid dpr arguments: %v", args)
|
|
|
|
}
|
|
|
|
|
2019-06-19 12:14:41 +02:00
|
|
|
if d, err := strconv.ParseFloat(args[0], 64); err == nil && d > 0 {
|
2018-11-15 13:19:27 +02:00
|
|
|
po.Dpr = d
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid dpr: %s", args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:37:25 +02:00
|
|
|
func applyGravityOption(po *processingOptions, args []string) error {
|
2019-06-07 20:14:41 +02:00
|
|
|
return parseGravity(&po.Gravity, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyCropOption(po *processingOptions, args []string) error {
|
|
|
|
if len(args) > 5 {
|
|
|
|
return fmt.Errorf("Invalid crop arguments: %v", args)
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 20:14:41 +02:00
|
|
|
if err := parseDimension(&po.Crop.Width, "crop width", args[0]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-11 13:23:28 +02:00
|
|
|
|
2019-06-07 20:14:41 +02:00
|
|
|
if len(args) > 1 {
|
|
|
|
if err := parseDimension(&po.Crop.Height, "crop height", args[1]); err != nil {
|
|
|
|
return err
|
2018-09-11 13:23:28 +02:00
|
|
|
}
|
2019-06-07 20:14:41 +02:00
|
|
|
}
|
2018-09-11 13:23:28 +02:00
|
|
|
|
2019-06-07 20:14:41 +02:00
|
|
|
if len(args) > 2 {
|
|
|
|
return parseGravity(&po.Crop.Gravity, args[2:])
|
2018-09-11 13:23:28 +02:00
|
|
|
}
|
|
|
|
|
2018-09-07 15:37:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-30 14:20:02 +02:00
|
|
|
func applyQualityOption(po *processingOptions, args []string) error {
|
|
|
|
if len(args) > 1 {
|
|
|
|
return fmt.Errorf("Invalid quality arguments: %v", args)
|
|
|
|
}
|
|
|
|
|
|
|
|
if q, err := strconv.Atoi(args[0]); err == nil && q > 0 && q <= 100 {
|
|
|
|
po.Quality = q
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid quality: %s", args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-02 14:20:23 +02:00
|
|
|
func applyBackgroundOption(po *processingOptions, args []string) error {
|
2018-10-04 14:27:49 +02:00
|
|
|
switch len(args) {
|
|
|
|
case 1:
|
|
|
|
if len(args[0]) == 0 {
|
|
|
|
po.Flatten = false
|
|
|
|
} else if c, err := colorFromHex(args[0]); err == nil {
|
|
|
|
po.Flatten = true
|
|
|
|
po.Background = c
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid background argument: %s", err)
|
|
|
|
}
|
2018-10-02 14:20:23 +02:00
|
|
|
|
2018-10-04 14:27:49 +02:00
|
|
|
case 3:
|
|
|
|
po.Flatten = true
|
2018-10-02 14:20:23 +02:00
|
|
|
|
2019-04-11 16:07:09 +02:00
|
|
|
if r, err := strconv.ParseUint(args[0], 10, 8); err == nil && r <= 255 {
|
2018-10-04 14:27:49 +02:00
|
|
|
po.Background.R = uint8(r)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid background red channel: %s", args[0])
|
|
|
|
}
|
2018-10-02 14:20:23 +02:00
|
|
|
|
2019-04-11 16:07:09 +02:00
|
|
|
if g, err := strconv.ParseUint(args[1], 10, 8); err == nil && g <= 255 {
|
2018-10-04 14:27:49 +02:00
|
|
|
po.Background.G = uint8(g)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid background green channel: %s", args[1])
|
|
|
|
}
|
2018-10-02 14:20:23 +02:00
|
|
|
|
2019-04-11 16:07:09 +02:00
|
|
|
if b, err := strconv.ParseUint(args[2], 10, 8); err == nil && b <= 255 {
|
2018-10-04 14:27:49 +02:00
|
|
|
po.Background.B = uint8(b)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid background blue channel: %s", args[2])
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Invalid background arguments: %v", args)
|
|
|
|
}
|
2018-10-02 14:20:23 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-07 16:46:16 +02:00
|
|
|
func applyBlurOption(po *processingOptions, args []string) error {
|
|
|
|
if len(args) > 1 {
|
|
|
|
return fmt.Errorf("Invalid blur arguments: %v", args)
|
|
|
|
}
|
|
|
|
|
2019-05-28 13:39:20 +02:00
|
|
|
if b, err := strconv.ParseFloat(args[0], 32); err == nil && b >= 0 {
|
2018-09-07 16:46:16 +02:00
|
|
|
po.Blur = float32(b)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid blur: %s", args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applySharpenOption(po *processingOptions, args []string) error {
|
|
|
|
if len(args) > 1 {
|
|
|
|
return fmt.Errorf("Invalid sharpen arguments: %v", args)
|
|
|
|
}
|
|
|
|
|
2019-05-28 13:39:20 +02:00
|
|
|
if s, err := strconv.ParseFloat(args[0], 32); err == nil && s >= 0 {
|
2018-09-07 16:46:16 +02:00
|
|
|
po.Sharpen = float32(s)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid sharpen: %s", args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-07 19:41:06 +02:00
|
|
|
func applyPresetOption(po *processingOptions, args []string) error {
|
|
|
|
for _, preset := range args {
|
|
|
|
if p, ok := conf.Presets[preset]; ok {
|
2018-10-04 14:02:24 +02:00
|
|
|
if po.isPresetUsed(preset) {
|
2019-05-07 11:19:23 +02:00
|
|
|
logWarning("Recursive preset usage is detected: %s", preset)
|
|
|
|
continue
|
2018-10-04 14:02:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
po.presetUsed(preset)
|
|
|
|
|
2018-09-12 15:46:43 +02:00
|
|
|
if err := applyProcessingOptions(po, p); err != nil {
|
|
|
|
return err
|
2018-09-07 19:41:06 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Unknown asset: %s", preset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 11:47:11 +02:00
|
|
|
func applyWatermarkOption(po *processingOptions, args []string) error {
|
2018-10-28 14:05:57 +02:00
|
|
|
if len(args) > 7 {
|
|
|
|
return fmt.Errorf("Invalid watermark arguments: %v", args)
|
|
|
|
}
|
|
|
|
|
2018-10-19 11:47:11 +02:00
|
|
|
if o, err := strconv.ParseFloat(args[0], 64); err == nil && o >= 0 && o <= 1 {
|
|
|
|
po.Watermark.Enabled = o > 0
|
|
|
|
po.Watermark.Opacity = o
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid watermark opacity: %s", args[0])
|
|
|
|
}
|
|
|
|
|
2018-10-28 14:05:57 +02:00
|
|
|
if len(args) > 1 && len(args[1]) > 0 {
|
2018-10-19 11:47:11 +02:00
|
|
|
if args[1] == "re" {
|
|
|
|
po.Watermark.Replicate = true
|
2018-10-19 13:31:09 +02:00
|
|
|
} else if g, ok := gravityTypes[args[1]]; ok && g != gravityFocusPoint && g != gravitySmart {
|
2018-10-19 11:47:11 +02:00
|
|
|
po.Watermark.Gravity = g
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid watermark position: %s", args[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-28 14:05:57 +02:00
|
|
|
if len(args) > 2 && len(args[2]) > 0 {
|
2018-10-19 11:47:11 +02:00
|
|
|
if x, err := strconv.Atoi(args[2]); err == nil {
|
|
|
|
po.Watermark.OffsetX = x
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid watermark X offset: %s", args[2])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-28 14:05:57 +02:00
|
|
|
if len(args) > 3 && len(args[3]) > 0 {
|
2018-10-19 11:47:11 +02:00
|
|
|
if y, err := strconv.Atoi(args[3]); err == nil {
|
|
|
|
po.Watermark.OffsetY = y
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid watermark Y offset: %s", args[3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-28 14:05:57 +02:00
|
|
|
if len(args) > 4 && len(args[4]) > 0 {
|
|
|
|
if s, err := strconv.ParseFloat(args[4], 64); err == nil && s >= 0 {
|
|
|
|
po.Watermark.Scale = s
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid watermark scale: %s", args[4])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 11:47:11 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-11 14:26:27 +02:00
|
|
|
func applyFormatOption(po *processingOptions, args []string) error {
|
|
|
|
if len(args) > 1 {
|
|
|
|
return fmt.Errorf("Invalid format arguments: %v", args)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.EnforceWebp && po.Format == imageTypeWEBP {
|
|
|
|
// Webp is enforced and already set as format
|
|
|
|
return nil
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2018-09-11 14:26:27 +02:00
|
|
|
if f, ok := imageTypes[args[0]]; ok {
|
|
|
|
po.Format = f
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid image format: %s", args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
if !vipsTypeSupportSave[po.Format] {
|
2018-10-05 17:17:36 +02:00
|
|
|
return errResultingImageFormatIsNotSupported
|
2018-09-11 14:26:27 +02:00
|
|
|
}
|
2018-09-07 15:37:25 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-29 10:54:30 +02:00
|
|
|
func applyCacheBusterOption(po *processingOptions, args []string) error {
|
|
|
|
if len(args) > 1 {
|
|
|
|
return fmt.Errorf("Invalid cache buster arguments: %v", args)
|
|
|
|
}
|
|
|
|
|
|
|
|
po.CacheBuster = args[0]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:37:25 +02:00
|
|
|
func applyProcessingOption(po *processingOptions, name string, args []string) error {
|
|
|
|
switch name {
|
2018-10-03 17:36:52 +02:00
|
|
|
case "format", "f", "ext":
|
2018-09-11 14:26:27 +02:00
|
|
|
if err := applyFormatOption(po, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-03 17:36:52 +02:00
|
|
|
case "resize", "rs":
|
2018-09-07 15:37:25 +02:00
|
|
|
if err := applyResizeOption(po, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-03 17:36:52 +02:00
|
|
|
case "resizing_type", "rt":
|
2018-10-03 17:34:13 +02:00
|
|
|
if err := applyResizingTypeOption(po, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-03 17:36:52 +02:00
|
|
|
case "size", "s":
|
2018-09-07 15:37:25 +02:00
|
|
|
if err := applySizeOption(po, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-03 17:36:52 +02:00
|
|
|
case "width", "w":
|
2018-09-07 15:37:25 +02:00
|
|
|
if err := applyWidthOption(po, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-03 17:36:52 +02:00
|
|
|
case "height", "h":
|
2018-09-07 15:37:25 +02:00
|
|
|
if err := applyHeightOption(po, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-03 17:36:52 +02:00
|
|
|
case "enlarge", "el":
|
2018-09-07 15:37:25 +02:00
|
|
|
if err := applyEnlargeOption(po, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-21 17:55:20 +02:00
|
|
|
case "extend", "ex":
|
|
|
|
if err := applyExtendOption(po, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-15 13:19:27 +02:00
|
|
|
case "dpr":
|
|
|
|
if err := applyDprOption(po, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-03 17:36:52 +02:00
|
|
|
case "gravity", "g":
|
2018-09-07 15:37:25 +02:00
|
|
|
if err := applyGravityOption(po, args); err != nil {
|
2019-06-07 20:14:41 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case "crop", "c":
|
|
|
|
if err := applyCropOption(po, args); err != nil {
|
2018-09-07 15:37:25 +02:00
|
|
|
return err
|
|
|
|
}
|
2018-10-30 14:20:02 +02:00
|
|
|
case "quality", "q":
|
|
|
|
if err := applyQualityOption(po, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-03 17:36:52 +02:00
|
|
|
case "background", "bg":
|
2018-10-02 14:20:23 +02:00
|
|
|
if err := applyBackgroundOption(po, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-03 17:36:52 +02:00
|
|
|
case "blur", "bl":
|
2018-09-07 16:46:16 +02:00
|
|
|
if err := applyBlurOption(po, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-03 17:36:52 +02:00
|
|
|
case "sharpen", "sh":
|
2018-09-07 16:46:16 +02:00
|
|
|
if err := applySharpenOption(po, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-19 11:47:11 +02:00
|
|
|
case "watermark", "wm":
|
|
|
|
if err := applyWatermarkOption(po, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-03 17:36:52 +02:00
|
|
|
case "preset", "pr":
|
2018-09-07 19:41:06 +02:00
|
|
|
if err := applyPresetOption(po, args); err != nil {
|
|
|
|
return err
|
2018-10-29 10:54:30 +02:00
|
|
|
}
|
|
|
|
case "cachebuster", "cb":
|
|
|
|
if err := applyCacheBusterOption(po, args); err != nil {
|
|
|
|
return err
|
2018-09-07 19:41:06 +02:00
|
|
|
}
|
2018-09-12 15:46:43 +02:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown processing option: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyProcessingOptions(po *processingOptions, options urlOptions) error {
|
|
|
|
for name, args := range options {
|
|
|
|
if err := applyProcessingOption(po, name, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-07 19:41:06 +02:00
|
|
|
func parseURLOptions(opts []string) (urlOptions, []string) {
|
|
|
|
parsed := make(urlOptions)
|
|
|
|
urlStart := len(opts) + 1
|
2018-09-07 15:37:25 +02:00
|
|
|
|
2018-09-07 19:41:06 +02:00
|
|
|
for i, opt := range opts {
|
|
|
|
args := strings.Split(opt, ":")
|
2018-09-07 15:37:25 +02:00
|
|
|
|
|
|
|
if len(args) == 1 {
|
|
|
|
urlStart = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-09-07 19:41:06 +02:00
|
|
|
parsed[args[0]] = args[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
var rest []string
|
|
|
|
|
|
|
|
if urlStart < len(opts) {
|
|
|
|
rest = opts[urlStart:]
|
|
|
|
} else {
|
|
|
|
rest = []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsed, rest
|
|
|
|
}
|
|
|
|
|
2018-11-02 18:38:41 +02:00
|
|
|
func defaultProcessingOptions(headers *processingHeaders) (*processingOptions, error) {
|
2018-09-11 12:33:36 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
po := processingOptions{
|
2018-10-04 14:02:24 +02:00
|
|
|
Resize: resizeFit,
|
|
|
|
Width: 0,
|
|
|
|
Height: 0,
|
2018-10-19 11:47:11 +02:00
|
|
|
Gravity: gravityOptions{Type: gravityCenter},
|
2018-10-04 14:02:24 +02:00
|
|
|
Enlarge: false,
|
2018-10-30 14:20:02 +02:00
|
|
|
Quality: conf.Quality,
|
2018-11-13 20:50:38 +02:00
|
|
|
Format: imageTypeUnknown,
|
2019-01-11 17:08:34 +02:00
|
|
|
Background: rgbColor{255, 255, 255},
|
2018-10-04 14:02:24 +02:00
|
|
|
Blur: 0,
|
|
|
|
Sharpen: 0,
|
2018-11-14 15:46:59 +02:00
|
|
|
Dpr: 1,
|
2018-10-19 11:47:11 +02:00
|
|
|
Watermark: watermarkOptions{Opacity: 1, Replicate: false, Gravity: gravityCenter},
|
2018-10-05 17:17:36 +02:00
|
|
|
UsedPresets: make([]string, 0, len(conf.Presets)),
|
2018-09-11 12:33:36 +02:00
|
|
|
}
|
|
|
|
|
2018-11-02 18:38:41 +02:00
|
|
|
if (conf.EnableWebpDetection || conf.EnforceWebp) && strings.Contains(headers.Accept, "image/webp") {
|
2018-09-11 14:26:27 +02:00
|
|
|
po.Format = imageTypeWEBP
|
|
|
|
}
|
2018-11-02 18:38:41 +02:00
|
|
|
if conf.EnableClientHints && len(headers.ViewportWidth) > 0 {
|
|
|
|
if vw, err := strconv.Atoi(headers.ViewportWidth); err == nil {
|
|
|
|
po.Width = vw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if conf.EnableClientHints && len(headers.Width) > 0 {
|
|
|
|
if w, err := strconv.Atoi(headers.Width); err == nil {
|
|
|
|
po.Width = w
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 15:46:59 +02:00
|
|
|
if conf.EnableClientHints && len(headers.DPR) > 0 {
|
2019-05-28 13:39:20 +02:00
|
|
|
if dpr, err := strconv.ParseFloat(headers.DPR, 64); err == nil && (dpr > 0 && dpr <= maxClientHintDPR) {
|
2018-11-15 13:19:27 +02:00
|
|
|
po.Dpr = dpr
|
2018-11-14 15:46:59 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-11 12:33:36 +02:00
|
|
|
if _, ok := conf.Presets["default"]; ok {
|
|
|
|
err = applyPresetOption(&po, []string{"default"})
|
|
|
|
}
|
|
|
|
|
2018-10-09 15:03:10 +02:00
|
|
|
return &po, err
|
2018-09-11 12:33:36 +02:00
|
|
|
}
|
|
|
|
|
2018-11-02 18:38:41 +02:00
|
|
|
func parsePathAdvanced(parts []string, headers *processingHeaders) (string, *processingOptions, error) {
|
|
|
|
po, err := defaultProcessingOptions(headers)
|
2018-09-11 12:33:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", po, err
|
|
|
|
}
|
2018-09-07 19:41:06 +02:00
|
|
|
|
|
|
|
options, urlParts := parseURLOptions(parts)
|
|
|
|
|
2018-10-09 15:03:10 +02:00
|
|
|
if err := applyProcessingOptions(po, options); err != nil {
|
2018-09-12 15:46:43 +02:00
|
|
|
return "", po, err
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2018-09-11 14:26:27 +02:00
|
|
|
url, extension, err := decodeURL(urlParts)
|
2018-09-07 15:37:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", po, err
|
|
|
|
}
|
|
|
|
|
2018-09-11 14:26:27 +02:00
|
|
|
if len(extension) > 0 {
|
2018-10-09 15:03:10 +02:00
|
|
|
if err := applyFormatOption(po, []string{extension}); err != nil {
|
2018-10-05 17:17:36 +02:00
|
|
|
return "", po, err
|
2018-09-11 14:26:27 +02:00
|
|
|
}
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2018-11-02 17:35:21 +02:00
|
|
|
return url, po, nil
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 12:24:42 +02:00
|
|
|
func parsePathPresets(parts []string, headers *processingHeaders) (string, *processingOptions, error) {
|
|
|
|
po, err := defaultProcessingOptions(headers)
|
|
|
|
if err != nil {
|
|
|
|
return "", po, err
|
|
|
|
}
|
|
|
|
|
|
|
|
presets := strings.Split(parts[0], ":")
|
|
|
|
urlParts := parts[1:]
|
|
|
|
|
|
|
|
if err := applyPresetOption(po, presets); err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
url, extension, err := decodeURL(urlParts)
|
|
|
|
if err != nil {
|
|
|
|
return "", po, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(extension) > 0 {
|
|
|
|
if err := applyFormatOption(po, []string{extension}); err != nil {
|
|
|
|
return "", po, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return url, po, nil
|
|
|
|
}
|
|
|
|
|
2018-11-06 13:19:34 +02:00
|
|
|
func parsePathBasic(parts []string, headers *processingHeaders) (string, *processingOptions, error) {
|
2018-09-07 15:37:25 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if len(parts) < 6 {
|
2018-10-09 15:03:10 +02:00
|
|
|
return "", nil, errInvalidPath
|
2018-09-11 12:33:36 +02:00
|
|
|
}
|
|
|
|
|
2018-11-02 18:38:41 +02:00
|
|
|
po, err := defaultProcessingOptions(headers)
|
2018-09-11 12:33:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", po, err
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
po.Resize = resizeTypes[parts[0]]
|
|
|
|
|
2018-10-09 15:03:10 +02:00
|
|
|
if err = applyWidthOption(po, parts[1:2]); err != nil {
|
2018-09-07 15:37:25 +02:00
|
|
|
return "", po, err
|
|
|
|
}
|
|
|
|
|
2018-10-09 15:03:10 +02:00
|
|
|
if err = applyHeightOption(po, parts[2:3]); err != nil {
|
2018-09-07 15:37:25 +02:00
|
|
|
return "", po, err
|
|
|
|
}
|
|
|
|
|
2018-10-09 15:03:10 +02:00
|
|
|
if err = applyGravityOption(po, strings.Split(parts[3], ":")); err != nil {
|
2018-09-07 15:37:25 +02:00
|
|
|
return "", po, err
|
|
|
|
}
|
|
|
|
|
2018-10-09 15:03:10 +02:00
|
|
|
if err = applyEnlargeOption(po, parts[4:5]); err != nil {
|
2018-09-07 15:37:25 +02:00
|
|
|
return "", po, err
|
|
|
|
}
|
|
|
|
|
2018-09-11 14:26:27 +02:00
|
|
|
url, extension, err := decodeURL(parts[5:])
|
2018-09-07 15:37:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", po, err
|
|
|
|
}
|
|
|
|
|
2018-09-11 14:26:27 +02:00
|
|
|
if len(extension) > 0 {
|
2018-10-09 15:03:10 +02:00
|
|
|
if err := applyFormatOption(po, []string{extension}); err != nil {
|
2018-10-05 17:17:36 +02:00
|
|
|
return "", po, err
|
2018-09-11 14:26:27 +02:00
|
|
|
}
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2018-11-02 17:35:21 +02:00
|
|
|
return url, po, nil
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2019-05-08 13:42:48 +02:00
|
|
|
func parsePath(ctx context.Context, r *http.Request) (context.Context, error) {
|
|
|
|
path := r.URL.RawPath
|
|
|
|
if len(path) == 0 {
|
|
|
|
path = r.URL.Path
|
|
|
|
}
|
2018-09-07 15:37:25 +02:00
|
|
|
parts := strings.Split(strings.TrimPrefix(path, "/"), "/")
|
|
|
|
|
|
|
|
if len(parts) < 3 {
|
2018-10-05 17:17:36 +02:00
|
|
|
return ctx, errInvalidPath
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:47:46 +02:00
|
|
|
if !conf.AllowInsecure {
|
|
|
|
if err := validatePath(parts[0], strings.TrimPrefix(path, fmt.Sprintf("/%s", parts[0]))); err != nil {
|
2018-11-20 14:53:44 +02:00
|
|
|
return ctx, newError(403, err.Error(), msgForbidden)
|
2018-09-07 19:47:46 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-02 18:39:38 +02:00
|
|
|
|
|
|
|
headers := &processingHeaders{
|
2019-05-08 13:42:48 +02:00
|
|
|
Accept: r.Header.Get("Accept"),
|
|
|
|
Width: r.Header.Get("Width"),
|
|
|
|
ViewportWidth: r.Header.Get("Viewport-Width"),
|
|
|
|
DPR: r.Header.Get("DPR"),
|
2018-11-02 18:39:38 +02:00
|
|
|
}
|
2018-09-07 15:37:25 +02:00
|
|
|
|
2018-10-05 17:17:36 +02:00
|
|
|
var imageURL string
|
2018-10-09 15:03:10 +02:00
|
|
|
var po *processingOptions
|
2018-10-05 17:17:36 +02:00
|
|
|
var err error
|
|
|
|
|
2019-05-16 12:24:42 +02:00
|
|
|
if conf.OnlyPresets {
|
|
|
|
imageURL, po, err = parsePathPresets(parts[1:], headers)
|
|
|
|
} else if _, ok := resizeTypes[parts[1]]; ok {
|
2018-11-06 13:19:34 +02:00
|
|
|
imageURL, po, err = parsePathBasic(parts[1:], headers)
|
2018-10-05 17:17:36 +02:00
|
|
|
} else {
|
2018-11-02 18:38:41 +02:00
|
|
|
imageURL, po, err = parsePathAdvanced(parts[1:], headers)
|
2018-10-05 17:17:36 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 13:01:51 +02:00
|
|
|
if err != nil {
|
2018-11-20 14:53:44 +02:00
|
|
|
return ctx, newError(404, err.Error(), msgInvalidURL)
|
2018-10-18 13:01:51 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 17:17:36 +02:00
|
|
|
ctx = context.WithValue(ctx, imageURLCtxKey, imageURL)
|
2018-10-09 15:03:10 +02:00
|
|
|
ctx = context.WithValue(ctx, processingOptionsCtxKey, po)
|
2018-10-05 17:17:36 +02:00
|
|
|
|
2018-11-20 14:53:44 +02:00
|
|
|
return ctx, nil
|
2018-10-05 17:17:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getImageURL(ctx context.Context) string {
|
|
|
|
return ctx.Value(imageURLCtxKey).(string)
|
|
|
|
}
|
|
|
|
|
2018-10-05 18:20:29 +02:00
|
|
|
func getProcessingOptions(ctx context.Context) *processingOptions {
|
2018-10-05 17:17:36 +02:00
|
|
|
return ctx.Value(processingOptionsCtxKey).(*processingOptions)
|
2018-09-07 15:37:25 +02:00
|
|
|
}
|