1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-12-23 22:11:10 +02:00
Files
imgproxy/options/parser/parse.go

341 lines
7.9 KiB
Go
Raw Permalink Normal View History

2025-09-25 19:48:53 +03:00
package optionsparser
import (
2025-09-25 17:02:57 +03:00
"encoding/base64"
2025-09-15 02:15:44 +03:00
"fmt"
"log/slog"
2025-09-25 18:52:10 +03:00
"maps"
"slices"
"strconv"
2025-09-25 17:02:57 +03:00
"strings"
2025-09-23 20:16:36 +03:00
2025-09-25 19:48:53 +03:00
"github.com/imgproxy/imgproxy/v3/options"
2025-09-23 20:16:36 +03:00
"github.com/imgproxy/imgproxy/v3/options/keys"
2025-09-25 19:48:53 +03:00
"github.com/imgproxy/imgproxy/v3/processing"
2025-09-25 17:02:57 +03:00
"github.com/imgproxy/imgproxy/v3/vips/color"
)
// ensureMaxArgs checks if the number of arguments is as expected
func (p *Parser) ensureMaxArgs(name string, args []string, max int) error {
if len(args) > max {
return newInvalidArgsError(name, args)
}
return nil
}
// parseBool parses a boolean option value and warns if the value is invalid
func (p *Parser) parseBool(o *options.Options, key string, args ...string) error {
if err := p.ensureMaxArgs(key, args, 1); err != nil {
return err
}
b, err := strconv.ParseBool(args[0])
if err != nil {
2025-09-23 20:16:36 +03:00
slog.Warn(fmt.Sprintf("%s `%s` is not a valid boolean value. Treated as false", key, args[0]))
}
2025-09-23 20:16:36 +03:00
o.Set(key, b)
return nil
}
2025-09-23 20:16:36 +03:00
// parseFloat parses a float64 option value
func (p *Parser) parseFloat(o *options.Options, key string, args ...string) error {
if err := p.ensureMaxArgs(key, args, 1); err != nil {
return err
}
f, err := strconv.ParseFloat(args[0], 64)
if err != nil {
2025-09-23 20:16:36 +03:00
return newInvalidArgsError(key, args)
}
2025-09-23 20:16:36 +03:00
o.Set(key, f)
return nil
}
2025-09-23 20:16:36 +03:00
// parsePositiveFloat parses a positive float64 option value
func (p *Parser) parsePositiveFloat(o *options.Options, key string, args ...string) error {
if err := p.ensureMaxArgs(key, args, 1); err != nil {
return err
}
f, err := strconv.ParseFloat(args[0], 64)
if err != nil || f < 0 {
2025-09-25 17:02:57 +03:00
return newInvalidArgumentError(key, args[0], "positive number or 0")
}
2025-09-23 20:16:36 +03:00
o.Set(key, f)
return nil
}
2025-09-23 20:16:36 +03:00
// parsePositiveNonZeroFloat parses a positive non-zero float64 option value
func (p *Parser) parsePositiveNonZeroFloat(o *options.Options, key string, args ...string) error {
if err := p.ensureMaxArgs(key, args, 1); err != nil {
return err
}
f, err := strconv.ParseFloat(args[0], 64)
if err != nil || f <= 0 {
2025-09-25 17:02:57 +03:00
return newInvalidArgumentError(key, args[0], "positive number")
}
2025-09-23 20:16:36 +03:00
o.Set(key, f)
return nil
}
// parseInt parses a positive integer option value
func (p *Parser) parseInt(o *options.Options, key string, args ...string) error {
if err := p.ensureMaxArgs(key, args, 1); err != nil {
return err
}
i, err := strconv.Atoi(args[0])
if err != nil {
2025-09-25 17:02:57 +03:00
return newInvalidArgumentError(key, args[0], "integer number")
}
2025-09-23 20:16:36 +03:00
o.Set(key, i)
return nil
}
// parsePositiveNonZeroInt parses a positive non-zero integer option value
func (p *Parser) parsePositiveNonZeroInt(o *options.Options, key string, args ...string) error {
if err := p.ensureMaxArgs(key, args, 1); err != nil {
return err
}
i, err := strconv.Atoi(args[0])
if err != nil || i <= 0 {
2025-09-25 17:02:57 +03:00
return newInvalidArgumentError(key, args[0], "positive number")
}
2025-09-23 20:16:36 +03:00
o.Set(key, i)
return nil
}
// parsePositiveInt parses a positive integer option value
func (p *Parser) parsePositiveInt(o *options.Options, key string, args ...string) error {
if err := p.ensureMaxArgs(key, args, 1); err != nil {
return err
}
i, err := strconv.Atoi(args[0])
if err != nil || i < 0 {
2025-09-25 17:02:57 +03:00
return newInvalidArgumentError(key, args[0], "positive number or 0")
}
2025-09-23 20:16:36 +03:00
o.Set(key, i)
return nil
}
// parseQualityInt parses a quality integer option value (1-100)
func (p *Parser) parseQualityInt(o *options.Options, key string, args ...string) error {
if err := p.ensureMaxArgs(key, args, 1); err != nil {
return err
}
i, err := strconv.Atoi(args[0])
if err != nil || i < 1 || i > 100 {
2025-09-25 17:02:57 +03:00
return newInvalidArgumentError(key, args[0], "number in range 1-100")
2025-09-23 20:16:36 +03:00
}
o.Set(key, i)
return nil
}
2025-09-25 17:02:57 +03:00
// parseOpacityFloat parses an opacity float option value (0-1)
func (p *Parser) parseOpacityFloat(o *options.Options, key string, args ...string) error {
if err := p.ensureMaxArgs(key, args, 1); err != nil {
2025-09-25 17:02:57 +03:00
return err
}
f, err := strconv.ParseFloat(args[0], 64)
if err != nil || f < 0 || f > 1 {
return newInvalidArgumentError(key, args[0], "number in range 0-1")
}
o.Set(key, f)
return nil
}
2025-09-23 20:16:36 +03:00
// parseResolution parses a resolution option value in megapixels and stores it as pixels
func (p *Parser) parseResolution(o *options.Options, key string, args ...string) error {
if err := p.ensureMaxArgs(key, args, 1); err != nil {
2025-09-23 20:16:36 +03:00
return err
}
f, err := strconv.ParseFloat(args[0], 64)
if err != nil || f < 0 {
2025-09-25 17:02:57 +03:00
return newInvalidArgumentError(key, args[0], "positive number or 0")
}
2025-09-23 20:16:36 +03:00
// Resolution is defined as megapixels but stored as pixels
o.Set(key, int(f*1000000))
return nil
}
2025-09-25 17:02:57 +03:00
// parseBase64String parses a base64-encoded string option value
func (p *Parser) parseBase64String(o *options.Options, key string, args ...string) error {
if err := p.ensureMaxArgs(key, args, 1); err != nil {
2025-09-25 17:02:57 +03:00
return err
}
b, err := base64.RawURLEncoding.DecodeString(strings.TrimRight(args[0], "="))
if err != nil {
return newInvalidArgumentError(key, args[0], "URL-safe base64-encoded string")
}
o.Set(key, string(b))
return nil
}
2025-09-25 18:52:10 +03:00
// parseHexRGBColor parses a hex-encoded RGB color option value
func (p *Parser) parseHexRGBColor(o *options.Options, key string, args ...string) error {
if err := p.ensureMaxArgs(key, args, 1); err != nil {
2025-09-25 17:02:57 +03:00
return err
}
c, err := color.RGBFromHex(args[0])
if err != nil {
return newInvalidArgumentError(key, args[0], "hex-encoded color")
}
o.Set(key, c)
return nil
}
2025-09-25 18:52:10 +03:00
// parseFromMap parses an option value from a map of allowed values
func parseFromMap[T comparable](
p *Parser,
o *options.Options,
key string,
m map[string]T,
args ...string,
) error {
if err := p.ensureMaxArgs(key, args, 1); err != nil {
2025-09-25 18:52:10 +03:00
return err
}
v, ok := m[args[0]]
if !ok {
return newInvalidArgumentError(key, args[0], slices.Collect(maps.Keys(m))...)
}
o.Set(key, v)
return nil
}
func (p *Parser) parseGravityType(
2025-09-25 19:48:53 +03:00
o *options.Options,
2025-09-25 17:02:57 +03:00
key string,
2025-09-25 19:48:53 +03:00
allowedTypes []processing.GravityType,
2025-09-25 17:02:57 +03:00
args ...string,
2025-09-25 19:48:53 +03:00
) (processing.GravityType, error) {
if err := p.ensureMaxArgs(key, args, 1); err != nil {
2025-09-25 19:48:53 +03:00
return processing.GravityUnknown, err
2025-09-25 17:02:57 +03:00
}
2025-09-25 19:48:53 +03:00
gType, ok := processing.GravityTypes[args[0]]
2025-09-25 17:02:57 +03:00
if !ok || !slices.Contains(allowedTypes, gType) {
types := make([]string, len(allowedTypes))
for i, at := range allowedTypes {
types[i] = at.String()
}
2025-09-25 19:48:53 +03:00
return processing.GravityUnknown, newInvalidArgumentError(key, args[0], types...)
2025-09-25 17:02:57 +03:00
}
o.Set(key, gType)
return gType, nil
}
func (p *Parser) isGravityOffsetValid(gravity processing.GravityType, offset float64) bool {
2025-09-25 19:48:53 +03:00
return gravity != processing.GravityFocusPoint || (offset >= 0 && offset <= 1)
}
func (p *Parser) parseGravity(
2025-09-25 19:48:53 +03:00
o *options.Options,
2025-09-23 20:16:36 +03:00
key string,
2025-09-25 19:48:53 +03:00
allowedTypes []processing.GravityType,
2025-09-25 17:02:57 +03:00
args ...string,
2025-09-23 20:16:36 +03:00
) error {
nArgs := len(args)
2025-09-23 20:16:36 +03:00
keyType := key + keys.SuffixType
keyXOffset := key + keys.SuffixXOffset
keyYOffset := key + keys.SuffixYOffset
gType, err := p.parseGravityType(o, keyType, allowedTypes, args[0])
2025-09-25 17:02:57 +03:00
if err != nil {
return err
}
2025-09-23 20:16:36 +03:00
switch gType {
2025-09-25 19:48:53 +03:00
case processing.GravitySmart:
if nArgs > 1 {
2025-09-23 20:16:36 +03:00
return newInvalidArgsError(key, args)
}
2025-09-23 20:16:36 +03:00
o.Delete(keyXOffset)
o.Delete(keyYOffset)
2025-09-25 19:48:53 +03:00
case processing.GravityFocusPoint:
if nArgs != 3 {
2025-09-23 20:16:36 +03:00
return newInvalidArgsError(key, args)
}
fallthrough
default:
if nArgs > 3 {
2025-09-23 20:16:36 +03:00
return newInvalidArgsError(key, args)
}
if nArgs > 1 {
if x, err := strconv.ParseFloat(args[1], 64); err == nil && p.isGravityOffsetValid(gType, x) {
2025-09-23 20:16:36 +03:00
o.Set(keyXOffset, x)
} else {
2025-09-25 17:02:57 +03:00
return newInvalidArgumentError(keyXOffset, args[1])
}
}
if nArgs > 2 {
if y, err := strconv.ParseFloat(args[2], 64); err == nil && p.isGravityOffsetValid(gType, y) {
2025-09-23 20:16:36 +03:00
o.Set(keyYOffset, y)
} else {
2025-09-25 17:02:57 +03:00
return newInvalidArgumentError(keyYOffset, args[2])
}
}
}
return nil
}
func (p *Parser) parseExtend(o *options.Options, key string, args []string) error {
if err := p.ensureMaxArgs(key, args, 4); err != nil {
return err
}
if err := p.parseBool(o, key+keys.SuffixEnabled, args[0]); err != nil {
return err
}
if len(args) > 1 {
return p.parseGravity(o, key+keys.SuffixGravity, processing.ExtendGravityTypes, args[1:]...)
}
return nil
}