forked from mtusi2026/mtusi2026lab6
320 lines
9.4 KiB
Go
320 lines
9.4 KiB
Go
package worker
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"image/draw"
|
|
"image/jpeg"
|
|
"io"
|
|
"math"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"mtusi2026lab6/internal/application"
|
|
"mtusi2026lab6/internal/domain"
|
|
|
|
"github.com/disintegration/imaging"
|
|
webp "github.com/mayahiro/go-webp"
|
|
)
|
|
|
|
type pipelineProfile struct {
|
|
thumbnailJPEGQuality int
|
|
cropJPEGQuality int
|
|
optimizedWebPQuality int
|
|
}
|
|
|
|
func profileForPipeline(version int) (pipelineProfile, bool) {
|
|
switch version {
|
|
case 1:
|
|
return pipelineProfile{thumbnailJPEGQuality: 85, cropJPEGQuality: 85, optimizedWebPQuality: 80}, true
|
|
case 2:
|
|
return pipelineProfile{thumbnailJPEGQuality: 90, cropJPEGQuality: 90, optimizedWebPQuality: 85}, true
|
|
default:
|
|
return pipelineProfile{}, false
|
|
}
|
|
}
|
|
|
|
type JobStore interface {
|
|
GetAttachmentForProcessing(ctx context.Context, attachmentID string) (domain.Attachment, error)
|
|
CompleteAttachmentJob(
|
|
ctx context.Context,
|
|
job domain.AttachmentJob,
|
|
variants []domain.AttachmentVariant,
|
|
updatedAt time.Time,
|
|
) (bool, error)
|
|
ClearAttachmentStagingKey(ctx context.Context, attachmentID, stagingKey string) error
|
|
}
|
|
|
|
type FaultHook func(job domain.AttachmentJob) error
|
|
|
|
type ProcessorHooks struct {
|
|
BeforeObjects FaultHook
|
|
AfterObjects FaultHook
|
|
}
|
|
|
|
type Processor struct {
|
|
store JobStore
|
|
objects application.ObjectStore
|
|
now func() time.Time
|
|
beforeObjects FaultHook
|
|
afterObjects FaultHook
|
|
}
|
|
|
|
type PermanentError struct {
|
|
Code string
|
|
Err error
|
|
}
|
|
|
|
func (err *PermanentError) Error() string { return err.Code + ": " + err.Err.Error() }
|
|
func (err *PermanentError) Unwrap() error { return err.Err }
|
|
|
|
func NewProcessor(
|
|
store JobStore,
|
|
objects application.ObjectStore,
|
|
hooks ...ProcessorHooks,
|
|
) (*Processor, error) {
|
|
if store == nil || objects == nil {
|
|
return nil, fmt.Errorf("job store and object store are required")
|
|
}
|
|
var configured ProcessorHooks
|
|
if len(hooks) > 0 {
|
|
configured = hooks[0]
|
|
}
|
|
return &Processor{
|
|
store: store,
|
|
objects: objects,
|
|
now: func() time.Time { return time.Now().UTC() },
|
|
beforeObjects: configured.BeforeObjects,
|
|
afterObjects: configured.AfterObjects,
|
|
}, nil
|
|
}
|
|
|
|
func (processor *Processor) Process(ctx context.Context, job domain.AttachmentJob) error {
|
|
attachment, err := processor.store.GetAttachmentForProcessing(ctx, job.AttachmentID)
|
|
if err != nil {
|
|
return fmt.Errorf("load attachment metadata: %w", err)
|
|
}
|
|
if attachment.PipelineVersion != job.PipelineVersion {
|
|
return &PermanentError{Code: "stale_pipeline", Err: fmt.Errorf("pipeline version mismatch")}
|
|
}
|
|
profile, supported := profileForPipeline(job.PipelineVersion)
|
|
if !supported {
|
|
return &PermanentError{Code: "unsupported_pipeline", Err: fmt.Errorf("pipeline version %d is not supported", job.PipelineVersion)}
|
|
}
|
|
if attachment.StagingKey == "" {
|
|
return &PermanentError{Code: "staging_expired", Err: fmt.Errorf("staging object is no longer retained")}
|
|
}
|
|
|
|
object, err := processor.objects.Get(ctx, attachment.StagingKey)
|
|
if err != nil {
|
|
return fmt.Errorf("read staging object: %w", err)
|
|
}
|
|
defer object.Body.Close()
|
|
|
|
staged, err := os.CreateTemp("", "mtusi-worker-original-*")
|
|
if err != nil {
|
|
return fmt.Errorf("create worker staging file: %w", err)
|
|
}
|
|
stagedPath := staged.Name()
|
|
defer func() {
|
|
_ = staged.Close()
|
|
_ = os.Remove(stagedPath)
|
|
}()
|
|
|
|
hasher := sha256.New()
|
|
copied, err := io.Copy(
|
|
io.MultiWriter(staged, hasher),
|
|
io.LimitReader(object.Body, domain.MaxAttachmentSizeBytes+1),
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("copy staging object: %w", err)
|
|
}
|
|
if copied != attachment.SizeBytes ||
|
|
object.SizeBytes != attachment.SizeBytes ||
|
|
hex.EncodeToString(hasher.Sum(nil)) != attachment.SHA256 {
|
|
return &PermanentError{Code: "invalid_image", Err: fmt.Errorf("staging object integrity mismatch")}
|
|
}
|
|
if err = ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
if _, err = staged.Seek(0, io.SeekStart); err != nil {
|
|
return fmt.Errorf("rewind staged image: %w", err)
|
|
}
|
|
config, _, err := image.DecodeConfig(staged)
|
|
if err != nil {
|
|
return &PermanentError{Code: "invalid_image", Err: fmt.Errorf("decode staged image config: %w", err)}
|
|
}
|
|
if config.Width < 1 || config.Height < 1 ||
|
|
int64(config.Width) > int64(domain.MaxAttachmentPixels)/int64(config.Height) {
|
|
return &PermanentError{Code: "invalid_image", Err: fmt.Errorf("decoded image dimensions are unsafe")}
|
|
}
|
|
if _, err = staged.Seek(0, io.SeekStart); err != nil {
|
|
return fmt.Errorf("rewind staged image: %w", err)
|
|
}
|
|
decoded, err := imaging.Decode(staged, imaging.AutoOrientation(true))
|
|
if err != nil {
|
|
return &PermanentError{Code: "invalid_image", Err: fmt.Errorf("decode staged image: %w", err)}
|
|
}
|
|
if err = ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
bounds := decoded.Bounds()
|
|
width, height := bounds.Dx(), bounds.Dy()
|
|
if width < 1 || height < 1 ||
|
|
int64(width) > int64(domain.MaxAttachmentPixels)/int64(height) {
|
|
return &PermanentError{Code: "invalid_image", Err: fmt.Errorf("oriented image dimensions are unsafe")}
|
|
}
|
|
|
|
opaque := image.NewRGBA(image.Rect(0, 0, width, height))
|
|
draw.Draw(opaque, opaque.Bounds(), &image.Uniform{C: color.White}, image.Point{}, draw.Src)
|
|
draw.Draw(opaque, opaque.Bounds(), decoded, bounds.Min, draw.Over)
|
|
|
|
thumbnailWidth, thumbnailHeight := fitDimensions(width, height, 256)
|
|
thumbnail := imaging.Resize(opaque, thumbnailWidth, thumbnailHeight, imaging.Lanczos)
|
|
thumbnailBytes, err := encodeJPEG(thumbnail, profile.thumbnailJPEGQuality)
|
|
if err != nil {
|
|
return fmt.Errorf("encode thumbnail: %w", err)
|
|
}
|
|
|
|
cropSide := width
|
|
if height < cropSide {
|
|
cropSide = height
|
|
}
|
|
cropped := imaging.CropCenter(opaque, cropSide, cropSide)
|
|
cropped = imaging.Resize(cropped, 128, 128, imaging.Lanczos)
|
|
cropBytes, err := encodeJPEG(cropped, profile.cropJPEGQuality)
|
|
if err != nil {
|
|
return fmt.Errorf("encode crop: %w", err)
|
|
}
|
|
|
|
optimizedWidth, optimizedHeight := fitDimensions(width, height, 1280)
|
|
optimized := decoded
|
|
if optimizedWidth != width || optimizedHeight != height {
|
|
optimized = imaging.Resize(decoded, optimizedWidth, optimizedHeight, imaging.Lanczos)
|
|
}
|
|
var optimizedBuffer bytes.Buffer
|
|
if err = webp.EncodeContext(ctx, &optimizedBuffer, optimized, &webp.Options{
|
|
Compression: webp.CompressionLossy,
|
|
Quality: profile.optimizedWebPQuality,
|
|
}); err != nil {
|
|
return fmt.Errorf("encode optimized WebP: %w", err)
|
|
}
|
|
optimizedBytes := optimizedBuffer.Bytes()
|
|
|
|
baseKey := strings.TrimSuffix(attachment.ObjectKey, "/original") +
|
|
fmt.Sprintf("/v%d/", job.PipelineVersion)
|
|
variants := []domain.AttachmentVariant{
|
|
newVariant("thumbnail", "image/jpeg", baseKey+"thumbnail.jpg",
|
|
thumbnailBytes, thumbnailWidth, thumbnailHeight, job.PipelineVersion),
|
|
newVariant("crop", "image/jpeg", baseKey+"crop.jpg",
|
|
cropBytes, 128, 128, job.PipelineVersion),
|
|
newVariant("optimized", "image/webp", baseKey+"optimized.webp",
|
|
optimizedBytes, optimizedWidth, optimizedHeight, job.PipelineVersion),
|
|
}
|
|
|
|
if processor.beforeObjects != nil {
|
|
if err = processor.beforeObjects(job); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if _, err = staged.Seek(0, io.SeekStart); err != nil {
|
|
return fmt.Errorf("rewind original for publication: %w", err)
|
|
}
|
|
if err = processor.objects.Put(ctx, application.ObjectPut{
|
|
Key: attachment.ObjectKey,
|
|
ContentType: attachment.ContentType,
|
|
SizeBytes: attachment.SizeBytes,
|
|
Body: staged,
|
|
}); err != nil {
|
|
return fmt.Errorf("publish original: %w", err)
|
|
}
|
|
variantBodies := [][]byte{thumbnailBytes, cropBytes, optimizedBytes}
|
|
for index, variant := range variants {
|
|
if err = processor.objects.Put(ctx, application.ObjectPut{
|
|
Key: variant.ObjectKey,
|
|
ContentType: variant.ContentType,
|
|
SizeBytes: variant.SizeBytes,
|
|
Body: bytes.NewReader(variantBodies[index]),
|
|
}); err != nil {
|
|
return fmt.Errorf("publish %s: %w", variant.Name, err)
|
|
}
|
|
}
|
|
if processor.afterObjects != nil {
|
|
if err = processor.afterObjects(job); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
applied, err := processor.store.CompleteAttachmentJob(
|
|
ctx,
|
|
job,
|
|
variants,
|
|
processor.now(),
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("commit attachment result: %w", err)
|
|
}
|
|
if !applied {
|
|
return nil
|
|
}
|
|
|
|
cleanupContext, cancel := context.WithTimeout(context.WithoutCancel(ctx), 3*time.Second)
|
|
defer cancel()
|
|
if err = processor.objects.Delete(cleanupContext, attachment.StagingKey); err == nil {
|
|
_ = processor.store.ClearAttachmentStagingKey(
|
|
cleanupContext,
|
|
attachment.ID,
|
|
attachment.StagingKey,
|
|
)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func fitDimensions(width, height, maximum int) (int, int) {
|
|
scale := math.Min(1, math.Min(float64(maximum)/float64(width), float64(maximum)/float64(height)))
|
|
resultWidth := int(math.Floor(float64(width) * scale))
|
|
resultHeight := int(math.Floor(float64(height) * scale))
|
|
if resultWidth < 1 {
|
|
resultWidth = 1
|
|
}
|
|
if resultHeight < 1 {
|
|
resultHeight = 1
|
|
}
|
|
return resultWidth, resultHeight
|
|
}
|
|
|
|
func encodeJPEG(source image.Image, quality int) ([]byte, error) {
|
|
var encoded bytes.Buffer
|
|
if err := jpeg.Encode(&encoded, source, &jpeg.Options{Quality: quality}); err != nil {
|
|
return nil, err
|
|
}
|
|
return encoded.Bytes(), nil
|
|
}
|
|
|
|
func newVariant(
|
|
name string,
|
|
contentType string,
|
|
objectKey string,
|
|
body []byte,
|
|
width int,
|
|
height int,
|
|
pipelineVersion int,
|
|
) domain.AttachmentVariant {
|
|
digest := sha256.Sum256(body)
|
|
return domain.AttachmentVariant{
|
|
Name: name,
|
|
ContentType: contentType,
|
|
SizeBytes: int64(len(body)),
|
|
Width: width,
|
|
Height: height,
|
|
SHA256: hex.EncodeToString(digest[:]),
|
|
ObjectKey: objectKey,
|
|
PipelineVersion: pipelineVersion,
|
|
}
|
|
}
|