forked from mtusi2026/mtusi2026lab5
289 lines
8.5 KiB
Go
289 lines
8.5 KiB
Go
// Package pipeline turns one validated original image into the required
|
|
// variants. It is deterministic and side-effect free so a broker redelivery can
|
|
// simply overwrite the same keys with the same bytes.
|
|
package pipeline
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"image/jpeg"
|
|
_ "image/png"
|
|
"os"
|
|
"time"
|
|
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/config"
|
|
"github.com/gen2brain/webp"
|
|
"golang.org/x/image/draw"
|
|
"golang.org/x/sync/semaphore"
|
|
)
|
|
|
|
// Re-encoding drops EXIF/XMP/ICC payloads from variants. The original is
|
|
// copied byte-for-byte and keeps its metadata and checksum.
|
|
|
|
type Processor struct {
|
|
maxPixels int64
|
|
budgetBytes int64
|
|
decodeBudget *semaphore.Weighted
|
|
}
|
|
|
|
// Encoded is one variant ready to be uploaded.
|
|
type Encoded struct {
|
|
Name string
|
|
ContentType string
|
|
Bytes []byte
|
|
Width int
|
|
Height int
|
|
SHA256 string
|
|
}
|
|
|
|
func New(maxPixels, decodeBudgetBytes int64) *Processor {
|
|
return &Processor{
|
|
maxPixels: maxPixels,
|
|
budgetBytes: decodeBudgetBytes,
|
|
decodeBudget: semaphore.NewWeighted(decodeBudgetBytes),
|
|
}
|
|
}
|
|
|
|
// Failure classifies pipeline errors for the retry policy. A corrupt image is
|
|
// permanent (retrying cannot help); I/O, lease expiry and dependency errors are
|
|
// transient.
|
|
type Failure struct {
|
|
Permanent bool
|
|
Code string
|
|
Err error
|
|
}
|
|
|
|
func (f *Failure) Error() string {
|
|
if f.Err == nil {
|
|
return f.Code
|
|
}
|
|
return f.Code + ": " + f.Err.Error()
|
|
}
|
|
|
|
func (f *Failure) Unwrap() error { return f.Err }
|
|
|
|
func permanent(code string, err error) *Failure {
|
|
return &Failure{Permanent: true, Code: code, Err: err}
|
|
}
|
|
|
|
func transient(code string, err error) *Failure {
|
|
return &Failure{Permanent: false, Code: code, Err: err}
|
|
}
|
|
|
|
// Stats separates decode, pixel scaling and the two encoders. A single
|
|
// processing_ms number cannot answer whether a large image costs more because
|
|
// of resampling or because of JPEG/WebP compression.
|
|
type Stats struct {
|
|
Decode time.Duration
|
|
Scale time.Duration
|
|
JPEGEncode time.Duration
|
|
WebPEncode time.Duration
|
|
}
|
|
|
|
func (s Stats) Total() time.Duration {
|
|
return s.Decode + s.Scale + s.JPEGEncode + s.WebPEncode
|
|
}
|
|
|
|
// Process decodes the file once and returns all variants in memory. Worker
|
|
// concurrency and IMAGE_DECODE_MEMORY_MB bound the number of simultaneous
|
|
// decoded images, which is the memory-heavy part of the pipeline.
|
|
func (p *Processor) Process(ctx context.Context, file *os.File) ([]Encoded, Stats, error) {
|
|
var stats Stats
|
|
started := time.Now()
|
|
img, err := p.decode(ctx, file)
|
|
stats.Decode = time.Since(started)
|
|
if err != nil {
|
|
return nil, stats, err
|
|
}
|
|
|
|
started = time.Now()
|
|
thumbnailPixels := fitOnWhite(img, config.ThumbnailMax, config.ThumbnailMax)
|
|
stats.Scale += time.Since(started)
|
|
thumbnail, encode, err := renderJPEGTimed("thumbnail", thumbnailPixels)
|
|
stats.JPEGEncode += encode
|
|
if err != nil {
|
|
return nil, stats, permanent("processing_failed", err)
|
|
}
|
|
|
|
started = time.Now()
|
|
cropPixels := centerSquare(img, config.CropSize)
|
|
stats.Scale += time.Since(started)
|
|
crop, encode, err := renderJPEGTimed("crop", cropPixels)
|
|
stats.JPEGEncode += encode
|
|
if err != nil {
|
|
return nil, stats, permanent("processing_failed", err)
|
|
}
|
|
|
|
started = time.Now()
|
|
optimizedPixels := fitOnWhite(img, config.OptimizedMax, config.OptimizedMax)
|
|
stats.Scale += time.Since(started)
|
|
optimized, encode, err := renderWebPTimed(optimizedPixels)
|
|
stats.WebPEncode += encode
|
|
if err != nil {
|
|
return nil, stats, permanent("processing_failed", err)
|
|
}
|
|
|
|
return []Encoded{thumbnail, crop, optimized}, stats, nil
|
|
}
|
|
|
|
func renderJPEGTimed(name string, img image.Image) (Encoded, time.Duration, error) {
|
|
started := time.Now()
|
|
encoded, err := renderJPEG(name, img)
|
|
return encoded, time.Since(started), err
|
|
}
|
|
|
|
func renderWebPTimed(img image.Image) (Encoded, time.Duration, error) {
|
|
started := time.Now()
|
|
encoded, err := renderWebP(img)
|
|
return encoded, time.Since(started), err
|
|
}
|
|
|
|
func (p *Processor) decode(ctx context.Context, file *os.File) (image.Image, error) {
|
|
if _, err := file.Seek(0, 0); err != nil {
|
|
return nil, transient("storage_read_failed", err)
|
|
}
|
|
cfg, format, err := image.DecodeConfig(file)
|
|
if err != nil {
|
|
return nil, permanent("invalid_image", err)
|
|
}
|
|
if format != "png" && format != "jpeg" {
|
|
return nil, permanent("invalid_image", fmt.Errorf("unsupported format %q", format))
|
|
}
|
|
if cfg.Width <= 0 || cfg.Height <= 0 {
|
|
return nil, permanent("invalid_image", errors.New("empty image bounds"))
|
|
}
|
|
pixels := int64(cfg.Width) * int64(cfg.Height)
|
|
if pixels > p.maxPixels {
|
|
return nil, permanent("invalid_image", errors.New("image exceeds MAX_IMAGE_PIXELS"))
|
|
}
|
|
|
|
// The semaphore is acquired before Decode, not after. A decoder
|
|
// allocates roughly width*height*4 bytes; without this gate several 40 MP
|
|
// files can OOM the worker even with a small HTTP concurrency.
|
|
weight := pixels * 4
|
|
if weight > p.budgetBytes {
|
|
return nil, permanent("invalid_image", errors.New("image exceeds decode memory budget"))
|
|
}
|
|
if err = p.decodeBudget.Acquire(ctx, weight); err != nil {
|
|
return nil, transient("processing_timeout", err)
|
|
}
|
|
defer p.decodeBudget.Release(weight)
|
|
|
|
orientation := jpegOrientation(file)
|
|
if _, err = file.Seek(0, 0); err != nil {
|
|
return nil, transient("storage_read_failed", err)
|
|
}
|
|
img, decodeErr := safeDecode(file)
|
|
if decodeErr != nil {
|
|
return nil, permanent("invalid_image", decodeErr)
|
|
}
|
|
bounds := img.Bounds()
|
|
if bounds.Dx() != cfg.Width || bounds.Dy() != cfg.Height {
|
|
return nil, permanent("invalid_image", errors.New("decoded bounds differ from header"))
|
|
}
|
|
return orient(img, orientation), nil
|
|
}
|
|
|
|
func safeDecode(file *os.File) (img image.Image, err error) {
|
|
defer func() {
|
|
if recovered := recover(); recovered != nil {
|
|
img = nil
|
|
err = fmt.Errorf("image decoder panic: %v", recovered)
|
|
}
|
|
}()
|
|
img, _, err = image.Decode(file)
|
|
return img, err
|
|
}
|
|
|
|
// fitOnWhite scales down to fit maxWidth x maxHeight, never up. The source is
|
|
// composited over white, which is the contract for JPEG variants with alpha.
|
|
func fitOnWhite(src image.Image, maxWidth, maxHeight int) *image.RGBA {
|
|
bounds := src.Bounds()
|
|
width, height := fitSize(bounds.Dx(), bounds.Dy(), maxWidth, maxHeight)
|
|
dst := image.NewRGBA(image.Rect(0, 0, width, height))
|
|
draw.Draw(dst, dst.Bounds(), &image.Uniform{C: color.White}, image.Point{}, draw.Src)
|
|
draw.CatmullRom.Scale(dst, dst.Bounds(), src, bounds, draw.Over, nil)
|
|
return dst
|
|
}
|
|
|
|
// centerSquare always produces config.CropSize x config.CropSize. Unlike thumbnails, the
|
|
// contract allows upscaling tiny sources for the crop variant.
|
|
func centerSquare(src image.Image, size int) *image.RGBA {
|
|
bounds := src.Bounds()
|
|
side := bounds.Dx()
|
|
if bounds.Dy() < side {
|
|
side = bounds.Dy()
|
|
}
|
|
x := bounds.Min.X + (bounds.Dx()-side)/2
|
|
y := bounds.Min.Y + (bounds.Dy()-side)/2
|
|
cropRect := image.Rect(x, y, x+side, y+side)
|
|
|
|
dst := image.NewRGBA(image.Rect(0, 0, size, size))
|
|
draw.Draw(dst, dst.Bounds(), &image.Uniform{C: color.White}, image.Point{}, draw.Src)
|
|
draw.CatmullRom.Scale(dst, dst.Bounds(), src, cropRect, draw.Over, nil)
|
|
return dst
|
|
}
|
|
|
|
// fitSize preserves aspect ratio, rounds down and clamps to at least 1 px.
|
|
// It deliberately never enlarges a small source.
|
|
func fitSize(width, height, maxWidth, maxHeight int) (int, int) {
|
|
if width <= maxWidth && height <= maxHeight {
|
|
return width, height
|
|
}
|
|
if width*maxHeight >= height*maxWidth {
|
|
newHeight := height * maxWidth / width
|
|
if newHeight < 1 {
|
|
newHeight = 1
|
|
}
|
|
return maxWidth, newHeight
|
|
}
|
|
newWidth := width * maxHeight / height
|
|
if newWidth < 1 {
|
|
newWidth = 1
|
|
}
|
|
return newWidth, maxHeight
|
|
}
|
|
|
|
func renderJPEG(name string, img image.Image) (Encoded, error) {
|
|
var buf bytes.Buffer
|
|
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: config.JPEGQuality}); err != nil {
|
|
return Encoded{}, err
|
|
}
|
|
bounds := img.Bounds()
|
|
return Encoded{
|
|
Name: name,
|
|
ContentType: "image/jpeg",
|
|
Bytes: buf.Bytes(),
|
|
Width: bounds.Dx(),
|
|
Height: bounds.Dy(),
|
|
SHA256: digest(buf.Bytes()),
|
|
}, nil
|
|
}
|
|
|
|
func renderWebP(img image.Image) (Encoded, error) {
|
|
var buf bytes.Buffer
|
|
if err := webp.Encode(&buf, img, webp.Options{Quality: config.WebPQuality, Method: config.WebPMethod}); err != nil {
|
|
return Encoded{}, err
|
|
}
|
|
bounds := img.Bounds()
|
|
return Encoded{
|
|
Name: "optimized",
|
|
ContentType: "image/webp",
|
|
Bytes: buf.Bytes(),
|
|
Width: bounds.Dx(),
|
|
Height: bounds.Dy(),
|
|
SHA256: digest(buf.Bytes()),
|
|
}, nil
|
|
}
|
|
|
|
func digest(data []byte) string {
|
|
sum := sha256.Sum256(data)
|
|
return hex.EncodeToString(sum[:])
|
|
}
|