feat: process attachments through durable versioned jobs

This commit is contained in:
Vladislav Agapov
2026-10-29 16:56:06 +03:00
parent 276dbdad8a
commit e35c8f0b75
235 changed files with 17028 additions and 44 deletions
+98
View File
@@ -0,0 +1,98 @@
// Package imageprocessor creates bounded contract variants with no client-controlled paths.
package imageprocessor
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"image"
"image/color"
"image/draw"
"image/jpeg"
"image/png"
"os/exec"
"github.com/disintegration/imaging"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/internal/model/dto"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/internal/pipeline"
)
// Processor generates JPEGs in-process and WebP through a cancellable bounded encoder.
type Processor struct {
webp string
}
// New resolves the required encoder before accepting work.
func New() (*Processor, error) {
path, err := exec.LookPath("cwebp")
if err != nil {
return nil, fmt.Errorf("cwebp encoder is required")
}
return &Processor{webp: path}, nil
}
// Process verifies dimensions before decode, applies EXIF orientation and strips metadata.
func (p *Processor) Process(ctx context.Context, source []byte) ([]pipeline.Output, error) {
cfg, format, err := image.DecodeConfig(bytes.NewReader(source))
if err != nil || (format != "png" && format != "jpeg") || len(source) > 10*1024*1024 || int64(cfg.Width)*int64(cfg.Height) > 40_000_000 {
return nil, pipeline.ErrInvalidImage
}
if err := ctx.Err(); err != nil {
return nil, err
}
decoded, err := imaging.Decode(bytes.NewReader(source), imaging.AutoOrientation(true))
if err != nil {
return nil, pipeline.ErrInvalidImage
}
bounds := decoded.Bounds()
white := image.NewNRGBA(image.Rect(0, 0, bounds.Dx(), bounds.Dy()))
draw.Draw(white, white.Bounds(), image.NewUniform(color.White), image.Point{}, draw.Src)
draw.Draw(white, white.Bounds(), decoded, bounds.Min, draw.Over)
thumbnail := fit(white, 256)
crop := imaging.Fill(white, 128, 128, imaging.Center, imaging.Lanczos)
optimized := fit(white, 1280)
outputs := make([]pipeline.Output, 0, 3)
for _, item := range []struct {
name string
picture image.Image
}{{"thumbnail", thumbnail}, {"crop", crop}} {
if err := ctx.Err(); err != nil {
return nil, err
}
var encoded bytes.Buffer
if err := jpeg.Encode(&encoded, item.picture, &jpeg.Options{Quality: 85}); err != nil {
return nil, err
}
outputs = append(outputs, output(item.name, "image/jpeg", item.picture.Bounds(), encoded.Bytes()))
}
var input bytes.Buffer
if err := png.Encode(&input, optimized); err != nil {
return nil, err
}
command := exec.CommandContext(ctx, p.webp, "-quiet", "-q", "80", "-metadata", "none", "-o", "-", "--", "-")
command.Stdin = &input
encoded, err := command.Output()
if err != nil {
return nil, fmt.Errorf("webp encoding failed: %w", err)
}
return append(outputs, output("optimized", "image/webp", optimized.Bounds(), encoded)), nil
}
// fit floors dimensions and never enlarges a small source.
func fit(source image.Image, limit int) image.Image {
width, height := source.Bounds().Dx(), source.Bounds().Dy()
largest := max(width, height)
if largest <= limit {
return source
}
return imaging.Resize(source, max(1, width*limit/largest), max(1, height*limit/largest), imaging.Lanczos)
}
// output derives immutable variant metadata from the actual encoded result.
func output(name, mime string, bounds image.Rectangle, data []byte) pipeline.Output {
sum := sha256.Sum256(data)
return pipeline.Output{Metadata: dto.Variant{Name: name, ContentType: mime, Width: bounds.Dx(), Height: bounds.Dy(), SizeBytes: int64(len(data)), SHA256: hex.EncodeToString(sum[:])}, Bytes: data}
}
+107
View File
@@ -0,0 +1,107 @@
package imageprocessor
import (
"bytes"
"context"
"errors"
"fmt"
"image"
"image/color"
"image/png"
"testing"
_ "golang.org/x/image/webp"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/internal/pipeline"
)
// sourcePNG creates a transparent synthetic source with controlled dimensions.
func sourcePNG(t *testing.T, width, height int) []byte {
t.Helper()
pic := image.NewNRGBA(image.Rect(0, 0, width, height))
pic.Set(0, 0, color.NRGBA{R: 255, A: 128})
var data bytes.Buffer
if err := png.Encode(&data, pic); err != nil {
t.Fatal(err)
}
return data.Bytes()
}
// TestVariantsDimensions verifies real JPEG/WebP output and white transparency composition.
func TestVariantsDimensions(t *testing.T) {
for _, size := range []struct {
width int
height int
thumbWidth int
thumbHeight int
}{{320, 200, 256, 160}, {16, 8, 16, 8}, {200, 320, 160, 256}, {1600, 800, 256, 128}} {
t.Run(fmt.Sprintf("%dx%d", size.width, size.height), func(t *testing.T) {
processor, err := New()
if err != nil {
t.Fatal(err)
}
outputs, err := processor.Process(t.Context(), sourcePNG(t, size.width, size.height))
if err != nil || len(outputs) != 3 {
t.Fatalf("variants: %v", err)
}
for _, out := range outputs {
decoded, format, err := image.Decode(bytes.NewReader(out.Bytes))
if err != nil {
t.Fatal(err)
}
if decoded.Bounds().Dx() != out.Metadata.Width || decoded.Bounds().Dy() != out.Metadata.Height {
t.Fatal("metadata dimensions differ")
}
if out.Metadata.Name == "thumbnail" && (out.Metadata.Width != size.thumbWidth || out.Metadata.Height != size.thumbHeight || format != "jpeg") {
t.Fatal("thumbnail contract")
}
if out.Metadata.Name == "crop" && (out.Metadata.Width != 128 || out.Metadata.Height != 128 || format != "jpeg") {
t.Fatal("crop contract")
}
if out.Metadata.Name == "optimized" && (max(out.Metadata.Width, out.Metadata.Height) > 1280 || format != "webp") {
t.Fatal("optimized contract")
}
r, g, b, _ := decoded.At(decoded.Bounds().Max.X-1, decoded.Bounds().Max.Y-1).RGBA()
if min(r, g, b) < 60000 {
t.Fatal("alpha not composed on white")
}
}
})
}
}
// TestProcessorInvalidImage verifies malformed bytes are a permanent safe error.
func TestProcessorInvalidImage(t *testing.T) {
processor := &Processor{webp: "unused"}
_, err := processor.Process(t.Context(), []byte("not an image"))
if !errors.Is(err, pipeline.ErrInvalidImage) {
t.Fatal(err)
}
}
// TestProcessorCancellation prevents decode after an expired job deadline.
func TestProcessorCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
cancel()
_, err := (&Processor{}).Process(ctx, sourcePNG(t, 10, 10))
if !errors.Is(err, context.Canceled) {
t.Fatal(err)
}
}
// TestProcessorEncoderFailure verifies a missing encoder is not a successful variant set.
func TestProcessorEncoderFailure(t *testing.T) {
_, err := (&Processor{webp: "/missing-encoder"}).Process(t.Context(), sourcePNG(t, 10, 10))
if err == nil {
t.Fatal("missing encoder accepted")
}
}
// TestProcessorRequiresEncoder validates the deployment dependency at startup.
func TestProcessorRequiresEncoder(t *testing.T) {
t.Setenv("PATH", t.TempDir())
if _, err := New(); err == nil {
t.Fatal("missing cwebp accepted")
}
}