core

package
v1.0.0-rc2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ImageTypes = map[string]vips.ImageType{
	"gif":  vips.ImageTypeGIF,
	"jpeg": vips.ImageTypeJPEG,
	"jpg":  vips.ImageTypeJPEG,
	"png":  vips.ImageTypePNG,
	"tiff": vips.ImageTypeTIFF,
	"webp": vips.ImageTypeWEBP,
	"heif": vips.ImageTypeHEIF,
	"svg":  vips.ImageTypeSVG,
	"psd":  vips.ImageTypePSD,
}
View Source
var Version = "v0.0.0"

Functions

func DecryptAES128GCM

func DecryptAES128GCM(key []byte, base64EncryptedText string) (string, error)

DecryptAES128GCM takes a base64-encoded ciphertext and decrypts it using AES-128-GCM. The input must be encoded as: IV (12 bytes) | Ciphertext | Tag (16 bytes).

func DecryptURL

func DecryptURL(url string) (string, error)

func DecryptURLKey added in v0.7.2

func DecryptURLKey(secretKey string, url string) (string, error)

DecryptURLKey decrypts the given eurl string using a derived AES-128-GCM key.

func EncryptAES128GCM

func EncryptAES128GCM(key []byte, plaintext string) (string, error)

EncryptAES128GCM encrypts the given plaintext using AES-128-GCM with the provided key. The result is base64-encoded and includes IV (12 bytes) | ciphertext | tag (16 bytes).

func EncryptURLKey added in v0.7.2

func EncryptURLKey(secretKey string, url string) (string, error)

func ErrorImage added in v0.6.0

func ErrorImage(color string) (*vips.ImageRef, error)

func HostAllowed

func HostAllowed(host string, allowed []string) bool

HostAllowed reports whether host passes the configured allowlist. An empty allowlist permits every host.

func IsPublicAddress

func IsPublicAddress(ip net.IP) bool

IsPublicAddress reports whether an address is routable on the public internet. An IPv4 address mapped into IPv6 is judged as IPv4, so ::ffff:169.254.169.254 is refused along with 169.254.169.254.

func LegacyKey

func LegacyKey(secretKey string) bool

LegacyKey reports whether the signing key uses the mod_dims sha1: prefix. That path derives the AES key from 16 hex characters, so it carries 64 bits of key material spread across 16 bytes.

func NewJpegExportParams

func NewJpegExportParams(options JpegCompression, stripMetadata bool) *vips.JpegExportParams

func NewPngExportParams

func NewPngExportParams(options PngCompression, stripMetadata bool) *vips.PngExportParams

func NewWebpExportParams

func NewWebpExportParams(options WebpCompression, stripMetadata bool) *vips.WebpExportParams

func ReadSource

func ReadSource(body io.Reader, declared int64) ([]byte, error)

ReadSource reads a source image and refuses one above DIMS_MAX_SOURCE_BYTES. The pixel caps do not cover this. They run once the whole body is already in memory, so only a byte limit bounds what a burst of downloads allocates.

declared is the size the origin announced. Pass a negative number when the origin announced none. A declared size above the limit is refused before the read, so an oversized body costs nothing.

func RecordStartupError

func RecordStartupError(err error)

RecordStartupError stores a configuration failure for Validate to report. Only safe to call during package initialisation, which is single threaded.

func RegisterImageBackend added in v0.6.0

func RegisterImageBackend(sourceBackend SourceBackend)

func SignedMessage

func SignedMessage(commands string, imageUrl string, signedQuery string) string

SignedMessage returns the string a signature covers. It holds one field per line: the command path, the image URL, then the canonical query.

A line break cannot appear inside a field. The query is percent-encoded, and a control character in the command path or the image URL is refused before the request is signed or validated. So one field can never stand in for two.

func StartVips

func StartVips(config *Config)

StartVips starts libvips with the configured cache and concurrency limits. Call it after the logger is configured, so the effective values reach the log in the configured format.

func ValidateImageURL

func ValidateImageURL(u *url.URL, config Network) error

ValidateImageURL checks the scheme and host of a URL before any connection is made. The address itself is checked separately, once it resolves.

Types

type Config

type Config struct {
	BindAddress     string `env:"DIMS_BIND_ADDRESS" envDefault:":8080"`
	DevelopmentMode bool   `env:"DIMS_DEVELOPMENT_MODE" envDefault:"false"`
	DebugMode       bool   `env:"DIMS_DEBUG_MODE" envDefault:"false"`
	LogFormat       string `env:"DIMS_LOG_FORMAT" envDefault:"text"`

	Timeout
	Limits
	Vips
	Server
	Network
	EdgeControl
	Signing
	Error
	OriginCacheControl
	OutputFormat
	Options
	ImageOutputOptions
}

func ReadConfig

func ReadConfig() *Config

func (*Config) Validate

func (c *Config) Validate() error

Validate reports settings that would otherwise fail quietly at request time.

type EdgeControl

type EdgeControl struct {
	DownstreamTtl int `env:"DIMS_EDGE_CONTROL_DOWNSTREAM_TTL" envDefault:"0"`
}

type Error

type Error struct {
	Background string `env:"DIMS_ERROR_BACKGROUND" envDefault:"#5ADAFD"`
}

type FileSource added in v0.6.0

type FileSource struct {
	BaseDir string `env:"DIMS_FILE_BASE_DIR" envDefault:"./resources"`
}

type Image

type Image struct {
	Bytes        []byte         // The downloaded image.
	Format       vips.ImageType // The original image format.
	Status       int            // The HTTP status code of the downloaded image.
	CacheControl string         // The cache headers from the downloaded image.
	EdgeControl  string         // The edge control headers from the downloaded image.
	LastModified string         // The last modified header from the downloaded image.
	Etag         string         // The etag header from the downloaded image.
}

func FetchImage

func FetchImage(imageSource string, timeout time.Duration) (*Image, error)

type ImageOutputOptions

type ImageOutputOptions struct {
	Jpeg JpegCompression
	Png  PngCompression
	Webp WebpCompression
}

type JpegCompression

type JpegCompression struct {
	Quality            int  `env:"DIMS_JPEG_QUALITY" envDefault:"80"`
	Interlace          bool `env:"DIMS_JPEG_INTERLACE" envDefault:"false"`
	OptimizeCoding     bool `env:"DIMS_JPEG_OPTIMIZE_CODING" envDefault:"true"`
	SubsampleMode      bool `env:"DIMS_JPEG_SUBSAMPLE_MODE" envDefault:"true"`
	TrellisQuant       bool `env:"DIMS_JPEG_TRELLIS_QUANT" envDefault:"false"`
	OvershootDeringing bool `env:"DIMS_JPEG_OVERSHOOT_DERINGING" envDefault:"false"`
	OptimizeScans      bool `env:"DIMS_JPEG_OPTIMIZE_SCANS" envDefault:"false"`
	QuantTable         int  `env:"DIMS_JPEG_QUANT_TABLE" envDefault:"3"`
}

type Limits

type Limits struct {
	// Cost is linear in output pixels, measured at roughly 30 megapixels per
	// second per thread. These caps bound the work one request can ask for.
	// Zero disables a check.
	MaxSourcePixels int `env:"DIMS_MAX_SOURCE_PIXELS" envDefault:"100000000"`
	MaxOutputPixels int `env:"DIMS_MAX_OUTPUT_PIXELS" envDefault:"50000000"`

	// MaxConcurrent caps how many images are processed at once. Zero derives
	// a value from the CPU count. A negative value removes the limit.
	MaxConcurrent int `env:"DIMS_MAX_CONCURRENT" envDefault:"0"`

	// MaxConcurrentWait is how long a request queues for a slot before it is
	// refused, in milliseconds.
	MaxConcurrentWait int `env:"DIMS_MAX_CONCURRENT_WAIT" envDefault:"5000"`

	// MaxSourceBytes caps the size of one source image, in bytes. The pixel
	// caps do not bound memory, because the whole body is resident before any
	// pixel check runs. Zero disables the check.
	MaxSourceBytes int `env:"DIMS_MAX_SOURCE_BYTES" envDefault:"67108864"`

	// MaxDownloadConcurrent caps how many source downloads run at once. Each
	// download holds a whole source body, so this and MaxSourceBytes together
	// bound the memory a burst can reach. Zero derives a value from the CPU
	// count. A negative value removes the limit.
	MaxDownloadConcurrent int `env:"DIMS_MAX_DOWNLOAD_CONCURRENT" envDefault:"0"`
}

type Network

type Network struct {
	// AllowedHosts limits which hosts an image may be fetched from. An entry
	// starting with a dot matches any subdomain. An empty list allows any host
	// that passes the address check.
	AllowedHosts []string `env:"DIMS_ALLOWED_HOSTS"`

	// AllowPrivateNetworks permits connections to loopback, link local,
	// private, and other non-public addresses. Keep it false unless the origin
	// sits inside the same network.
	AllowPrivateNetworks bool `env:"DIMS_ALLOW_PRIVATE_NETWORKS" envDefault:"false"`

	// MaxRedirects caps how many redirects one fetch will follow.
	MaxRedirects int `env:"DIMS_MAX_REDIRECTS" envDefault:"3"`
}

type Options

type Options struct {
	StripMetadata      bool `env:"DIMS_STRIP_METADATA" envDefault:"true"`
	IncludeDisposition bool `env:"DIMS_INCLUDE_DISPOSITION" envDefault:"false"`
}

type OriginCacheControl

type OriginCacheControl struct {
	UseOrigin bool `env:"DIMS_CACHE_CONTROL_USE_ORIGIN" envDefault:"false"`
	Min       int  `env:"DIMS_CACHE_CONTROL_MIN" envDefault:"0"`
	Max       int  `env:"DIMS_CACHE_CONTROL_MAX" envDefault:"0"`
	Default   int  `env:"DIMS_CACHE_CONTROL_DEFAULT" envDefault:"31536000"`
	Error     int  `env:"DIMS_CACHE_CONTROL_ERROR" envDefault:"60"`
}

type OutputFormat

type OutputFormat struct {
	Default  string   `env:"DIMS_DEFAULT_OUTPUT_FORMAT"`
	Excluded []string `env:"DIMS_EXCLUDED_OUTPUT_FORMATS"`
}

type PngCompression

type PngCompression struct {
	Quality     int  `env:"DIMS_PNG_QUALITY" envDefault:"80"`
	Interlace   bool `env:"DIMS_PNG_INTERLACE" envDefault:"false"`
	Compression int  `env:"DIMS_PNG_COMPRESSION" envDefault:"4"`
}

type S3

type S3 struct {
	Region string `env:"DIMS_S3_REGION" envDefault:""`
	Bucket string `env:"DIMS_S3_BUCKET" envDefault:""`
	Prefix string `env:"DIMS_S3_PREFIX" envDefault:""`
}

type Server

type Server struct {
	// All values are milliseconds. Zero disables the timeout, which is the
	// standard library default and the reason a slow client could previously
	// hold a connection open for as long as it liked.
	ReadHeaderTimeout int `env:"DIMS_READ_HEADER_TIMEOUT" envDefault:"5000"`
	ReadTimeout       int `env:"DIMS_READ_TIMEOUT" envDefault:"15000"`
	WriteTimeout      int `env:"DIMS_WRITE_TIMEOUT" envDefault:"60000"`
	IdleTimeout       int `env:"DIMS_IDLE_TIMEOUT" envDefault:"120000"`
	ShutdownTimeout   int `env:"DIMS_SHUTDOWN_TIMEOUT" envDefault:"30000"`
	MaxHeaderBytes    int `env:"DIMS_MAX_HEADER_BYTES" envDefault:"65536"`
}

type Signer added in v0.6.0

type Signer interface {
	SignedUrl() string
}

type Signing

type Signing struct {
	SigningKey string `env:"DIMS_SIGNING_KEY"`

	// Compat set to "legacy" also accepts signatures that cover only the
	// parameters named by _keys. Use it to migrate existing URLs, then clear
	// it. Empty means strict.
	Compat string `env:"DIMS_SIGNING_COMPAT"`
}

type Source added in v0.6.0

type Source struct {
	Default string   `env:"DIMS_DEFAULT_SOURCE_BACKEND" envDefault:"http"`
	Allowed []string `env:"DIMS_ALLOWED_SOURCE_BACKENDS" envDefault:"http"`
}

type SourceBackend added in v0.6.0

type SourceBackend interface {
	Name() string
	CanHandle(imageSource string) bool
	FetchImage(imageSource string, timeout time.Duration) (*Image, error)
}

type StatusError

type StatusError struct {
	StatusCode int
	Message    string
}

func NewStatusError

func NewStatusError(statusCode int, message string) *StatusError

func (*StatusError) Error

func (e *StatusError) Error() string

type Timeout

type Timeout struct {
	Download int `env:"DIMS_DOWNLOAD_TIMEOUT" envDefault:"3000"`
}

type Vips

type Vips struct {
	// ConcurrencyLevel is how many threads libvips uses inside one operation.
	// Raise it on a host with spare cores. A negative value keeps the libvips
	// default.
	ConcurrencyLevel int `env:"DIMS_VIPS_CONCURRENCY" envDefault:"1"`

	// MaxCacheMem is the largest amount of memory the operation cache holds,
	// in bytes.
	MaxCacheMem int `env:"DIMS_VIPS_MAX_CACHE_MEM" envDefault:"52428800"`

	// MaxCacheSize is how many operations the cache holds.
	MaxCacheSize int `env:"DIMS_VIPS_MAX_CACHE_SIZE" envDefault:"100"`

	// MaxCacheFiles is how many open files the cache holds.
	MaxCacheFiles int `env:"DIMS_VIPS_MAX_CACHE_FILES" envDefault:"0"`
}

type WebpCompression

type WebpCompression struct {
	Quality         int    `env:"DIMS_WEBP_QUALITY" envDefault:"80"`
	Compression     string `env:"DIMS_WEBP_COMPRESSION" envDefault:"lossy"`
	ReductionEffort int    `env:"DIMS_WEBP_REDUCTION_EFFORT" envDefault:"4"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL