package worker import ( "bytes" "context" "crypto/sha256" "encoding/hex" "errors" "image" "image/color" "image/draw" "image/jpeg" _ "image/png" "io" "os" "strconv" "github.com/disintegration/imaging" "github.com/gen2brain/webp" "mtusi2026-messenger/internal/db" ) var ( ErrInvalidSource = errors.New("invalid source image") ErrUnsupportedPipelineVersion = errors.New("unsupported pipeline version") ) type imageVariant struct { Name db.AttachmentVariantName Data []byte ContentType string Width, Height int } func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAttachmentVariantParams, error) { if row.StagingKey == nil { return nil, ErrInvalidSource } source, err := w.s3.Get(ctx, *row.StagingKey) if err != nil { return nil, err } defer source.Body.Close() tmp, err := os.CreateTemp("", "attachment-job-*") if err != nil { return nil, err } defer func() { _ = tmp.Close() _ = os.Remove(tmp.Name()) }() hash := sha256.New() size, err := io.Copy(io.MultiWriter(tmp, hash), io.LimitReader(source.Body, int64(w.maxBytes)+1)) if err != nil { return nil, err } if size != int64(row.SizeBytes) || hex.EncodeToString(hash.Sum(nil)) != row.Sha256 { return nil, ErrInvalidSource } if _, err := tmp.Seek(0, io.SeekStart); err != nil { return nil, err } select { case w.decodeSlot <- struct{}{}: case <-ctx.Done(): return nil, ctx.Err() } if err := ctx.Err(); err != nil { <-w.decodeSlot return nil, err } images, err := runImageProcess(ctx, tmp, w.maxPixels, row.PipelineVersion) <-w.decodeSlot if err != nil { return nil, err } if err := ctx.Err(); err != nil { return nil, err } if _, err := tmp.Seek(0, io.SeekStart); err != nil { return nil, err } if err := w.s3.Put(ctx, row.S3Key, tmp, size, string(row.ContentType)); err != nil { return nil, err } variants := make([]db.CreateAttachmentVariantParams, 0, len(images)) for _, img := range images { key := "variants/" + row.ID.String() + "/v" + strconv.Itoa(row.PipelineVersion) + "/" + string(img.Name) if err := w.s3.Put(ctx, key, bytes.NewReader(img.Data), int64(len(img.Data)), img.ContentType); err != nil { return nil, err } sum := sha256.Sum256(img.Data) variants = append(variants, db.CreateAttachmentVariantParams{ AttachmentID: row.ID, PipelineVersion: row.PipelineVersion, Variant: img.Name, S3Key: key, ContentType: img.ContentType, SizeBytes: len(img.Data), Width: img.Width, Height: img.Height, Sha256: hex.EncodeToString(sum[:]), }) } return variants, nil } func imageVariants(r io.ReadSeeker, maxPixels, pipelineVersion int) ([]imageVariant, error) { quality := 80 switch pipelineVersion { case 1: case 2: quality = 75 default: return nil, ErrUnsupportedPipelineVersion } cfg, format, err := image.DecodeConfig(r) if err != nil || (format != "png" && format != "jpeg") || cfg.Width <= 0 || cfg.Height <= 0 || cfg.Width > maxPixels/cfg.Height { return nil, ErrInvalidSource } if _, err := r.Seek(0, io.SeekStart); err != nil { return nil, err } src, err := imaging.Decode(r, imaging.AutoOrientation(true)) if err != nil { return nil, ErrInvalidSource } variants := make([]imageVariant, 0, 3) for _, item := range []struct { name db.AttachmentVariantName image image.Image }{ {db.AttachmentVariantNameThumbnail, fitImage(src, 256)}, {db.AttachmentVariantNameCrop, imaging.Fill(src, 128, 128, imaging.Center, imaging.Lanczos)}, {db.AttachmentVariantNameOptimized, fitImage(src, 1280)}, } { var data []byte contentType := "image/jpeg" if item.name == db.AttachmentVariantNameOptimized { contentType = "image/webp" data, err = encodeWebP(item.image, quality) } else { data, err = encodeJPEG(item.image, 85) } if err != nil { return nil, err } variants = append(variants, imageVariant{ Name: item.name, ContentType: contentType, Data: data, Width: item.image.Bounds().Dx(), Height: item.image.Bounds().Dy(), }) } return variants, nil } func fitImage(src image.Image, maxSize int) image.Image { width, height := src.Bounds().Dx(), src.Bounds().Dy() if width > maxSize || height > maxSize { if width >= height { height = max(1, height*maxSize/width) width = maxSize } else { width = max(1, width*maxSize/height) height = maxSize } return imaging.Resize(src, width, height, imaging.Lanczos) } return src } func encodeJPEG(src image.Image, quality int) ([]byte, error) { dst := image.NewRGBA(image.Rect(0, 0, src.Bounds().Dx(), src.Bounds().Dy())) draw.Draw(dst, dst.Bounds(), image.NewUniform(color.White), image.Point{}, draw.Src) draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Over) var out bytes.Buffer if err := jpeg.Encode(&out, dst, &jpeg.Options{Quality: quality}); err != nil { return nil, err } return out.Bytes(), nil } func encodeWebP(src image.Image, quality int) ([]byte, error) { var out bytes.Buffer if err := webp.Encode(&out, src, webp.Options{Quality: quality, Method: 4}); err != nil { return nil, err } return out.Bytes(), nil }