From 73d57e31a5cab70441fc880a19e4c286fbac01a7 Mon Sep 17 00:00:00 2001 From: kk10 Date: Mon, 16 Nov 2026 13:12:04 +0300 Subject: [PATCH] feat(worker): add centered 128x128 JPEG crop --- internal/worker/image.go | 156 ++++++++++++++++++++------------ student-tests/thumbnail_test.go | 59 ++++++++++-- student-tests/worker_test.go | 10 +- 3 files changed, 160 insertions(+), 65 deletions(-) diff --git a/internal/worker/image.go b/internal/worker/image.go index e7fac0b..a2a9ee3 100644 --- a/internal/worker/image.go +++ b/internal/worker/image.go @@ -22,48 +22,10 @@ import ( var errInvalidSource = errors.New("invalid source image") -func thumbnail(r io.ReadSeeker, maxPixels int) ([]byte, int, int, error) { - 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, 0, 0, errInvalidSource - } - - if _, err := r.Seek(0, io.SeekStart); err != nil { - return nil, 0, 0, err - } - - src, err := imaging.Decode(r, imaging.AutoOrientation(true)) - if err != nil { - return nil, 0, 0, errInvalidSource - } - - width, height := src.Bounds().Dx(), src.Bounds().Dy() - if width > 256 || height > 256 { - if width >= height { - height = max(1, height*256/width) - width = 256 - } else { - width = max(1, width*256/height) - height = 256 - } - - src = imaging.Resize(src, width, height, imaging.Lanczos) - } - - dst := image.NewRGBA(image.Rect(0, 0, width, height)) - 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: 85}); err != nil { - return nil, 0, 0, err - } - - return out.Bytes(), width, height, nil +type imageVariant struct { + name db.AttachmentVariantName + data []byte + width, height int } func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAttachmentVariantParams, error) { @@ -114,7 +76,7 @@ func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAtt return nil, err } - data, width, height, err := thumbnail(tmp, w.maxPixels) + images, err := imageVariants(tmp, w.maxPixels) <-w.decodeSlot if err != nil { return nil, err @@ -132,21 +94,101 @@ func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAtt return nil, err } - key := "variants/" + row.ID.String() + "/v" + strconv.Itoa(row.PipelineVersion) + "/thumbnail" - if err := w.s3.Put(ctx, key, bytes.NewReader(data), int64(len(data)), "image/jpeg"); err != nil { + 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)), "image/jpeg"); 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: "image/jpeg", + SizeBytes: len(img.data), + Width: img.width, + Height: img.height, + Sha256: hex.EncodeToString(sum[:]), + }) + } + + return variants, nil +} + +func imageVariants(r io.ReadSeeker, maxPixels int) ([]imageVariant, error) { + 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 } - sum := sha256.Sum256(data) - return []db.CreateAttachmentVariantParams{{ - AttachmentID: row.ID, - PipelineVersion: row.PipelineVersion, - Variant: db.AttachmentVariantNameThumbnail, - S3Key: key, - ContentType: "image/jpeg", - SizeBytes: len(data), - Width: width, - Height: height, - Sha256: hex.EncodeToString(sum[:]), - }}, nil + src, err := imaging.Decode(r, imaging.AutoOrientation(true)) + if err != nil { + return nil, errInvalidSource + } + + thumb := thumbnail(src) + crop := imaging.Fill(src, 128, 128, imaging.Center, imaging.Lanczos) + variants := make([]imageVariant, 0, 2) + for _, item := range []struct { + name db.AttachmentVariantName + image image.Image + }{ + {db.AttachmentVariantNameThumbnail, thumb}, + {db.AttachmentVariantNameCrop, crop}, + } { + data, err := encodeJPEG(item.image) + if err != nil { + return nil, err + } + + variants = append(variants, imageVariant{ + name: item.name, + data: data, + width: item.image.Bounds().Dx(), + height: item.image.Bounds().Dy(), + }) + } + + return variants, nil +} + +func thumbnail(src image.Image) image.Image { + width, height := src.Bounds().Dx(), src.Bounds().Dy() + if width > 256 || height > 256 { + if width >= height { + height = max(1, height*256/width) + width = 256 + } else { + width = max(1, width*256/height) + height = 256 + } + + return imaging.Resize(src, width, height, imaging.Lanczos) + } + + return src +} + +func encodeJPEG(src image.Image) ([]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: 85}); err != nil { + return nil, err + } + + return out.Bytes(), nil } diff --git a/student-tests/thumbnail_test.go b/student-tests/thumbnail_test.go index 5d5e4df..03582e4 100644 --- a/student-tests/thumbnail_test.go +++ b/student-tests/thumbnail_test.go @@ -31,7 +31,7 @@ func TestThumbnail(t *testing.T) { t.Fatal(err) } - data, width, height := processedThumbnail(t, source.Bytes(), "image/png") + data, width, height := processedVariant(t, source.Bytes(), "image/png", "thumbnail") if width != tc.wantWidth || height != tc.wantHeight { t.Fatalf("got %dx%d", width, height) @@ -86,7 +86,7 @@ func TestThumbnailEXIF(t *testing.T) { data = append(data, segment...) data = append(data, exif...) data = append(data, original.Bytes()[2:]...) - result, width, height := processedThumbnail(t, data, "image/jpeg") + result, width, height := processedVariant(t, data, "image/jpeg", "thumbnail") if width != 20 || height != 40 { t.Fatalf("orientation ignored: %dx%d", width, height) @@ -107,7 +107,7 @@ func TestThumbnailEXIF(t *testing.T) { } } -func processedThumbnail(t *testing.T, source []byte, contentType string) ([]byte, int, int) { +func processedVariant(t *testing.T, source []byte, contentType, variant string) ([]byte, int, int) { t.Helper() user := createUser(t) chat := createChat(t, user, user.ID) @@ -117,10 +117,10 @@ func processedThumbnail(t *testing.T, source []byte, contentType string) ([]byte mustDecode(t, resp, &att) att = waitAttachment(t, user, att.ID, "ready", time.Minute) for _, v := range att.Variants { - if v.Name != "thumbnail" { + if string(v.Name) != variant { continue } - resp = doRequest(t, http.MethodGet, "/api/v1/attachments/"+att.ID.String()+"/content?variant=thumbnail", user.Token, nil) + resp = doRequest(t, http.MethodGet, "/api/v1/attachments/"+att.ID.String()+"/content?variant="+variant, user.Token, nil) requireStatus(t, resp, http.StatusOK) defer resp.Body.Close() data, err := io.ReadAll(resp.Body) @@ -130,6 +130,53 @@ func processedThumbnail(t *testing.T, source []byte, contentType string) ([]byte return data, v.Width, v.Height } - t.Fatal("ready attachment has no thumbnail") + t.Fatalf("ready attachment has no %s", variant) return nil, 0, 0 } + +func TestCrop(t *testing.T) { + for _, tc := range []struct { + name string + width, height int + }{ + {"landscape", 384, 128}, + {"portrait", 128, 384}, + {"small", 24, 8}, + } { + t.Run(tc.name, func(t *testing.T) { + src := image.NewNRGBA(image.Rect(0, 0, tc.width, tc.height)) + // Only the central square is green; a stretched full image retains red edges. + side := min(tc.width, tc.height) + left, top := (tc.width-side)/2, (tc.height-side)/2 + for y := 0; y < tc.height; y++ { + for x := 0; x < tc.width; x++ { + c := color.NRGBA{R: 255, A: 255} + if x >= left && x < left+side && y >= top && y < top+side { + c = color.NRGBA{G: 255, A: 255} + } + src.SetNRGBA(x, y, c) + } + } + + var source bytes.Buffer + if err := png.Encode(&source, src); err != nil { + t.Fatal(err) + } + + data, width, height := processedVariant(t, source.Bytes(), "image/png", "crop") + decoded, err := jpeg.Decode(bytes.NewReader(data)) + if err != nil { + t.Fatal(err) + } + if width != 128 || height != 128 || decoded.Bounds() != image.Rect(0, 0, 128, 128) { + t.Fatalf("expected crop 128x128, metadata=%dx%d, JPEG=%v", width, height, decoded.Bounds()) + } + for _, point := range []image.Point{{8, 8}, {64, 64}, {119, 119}} { + r, g, b, _ := decoded.At(point.X, point.Y).RGBA() + if g < 50000 || r > 15000 || b > 15000 { + t.Fatalf("crop is not centered at %v", point) + } + } + }) + } +} diff --git a/student-tests/worker_test.go b/student-tests/worker_test.go index a1d3220..9bca17c 100644 --- a/student-tests/worker_test.go +++ b/student-tests/worker_test.go @@ -155,6 +155,11 @@ func TestWorkerPersistenceAndDuplicate(t *testing.T) { if err != nil || cfg.Width != 256 || cfg.Height != 160 { t.Fatal("invalid JPEG thumbnail") } + crop := workerContent(t, user, att.ID, "crop") + cfg, err = jpeg.DecodeConfig(bytes.NewReader(crop)) + if err != nil || cfg.Width != 128 || cfg.Height != 128 { + t.Fatal("invalid JPEG crop") + } // Re-publish the same intent, modeling duplicate delivery after a lost publisher confirmation. workerSQL(t, "UPDATE outbox SET published_at=NULL WHERE "+where) workerWait(t, "duplicate publication", 15*time.Second, func() bool { @@ -162,10 +167,11 @@ func TestWorkerPersistenceAndDuplicate(t *testing.T) { }) workerWait(t, "duplicate acknowledgement", 15*time.Second, func() bool { return workerQueue(t) == "0:0" }) duplicate := waitAttachment(t, user, att.ID, "ready", 30*time.Second) - if !reflect.DeepEqual(ready, duplicate) || workerSQL(t, "SELECT count(*) FROM attachment_variants WHERE "+where) != "1" { + if !reflect.DeepEqual(ready, duplicate) || workerSQL(t, "SELECT count(*) FROM attachment_variants WHERE "+where) != "2" { t.Fatal("duplicate job changed metadata or duplicated variants") } - if !bytes.Equal(workerContent(t, user, att.ID, "thumbnail"), thumb) || + if !bytes.Equal(workerContent(t, user, att.ID, "crop"), crop) || + !bytes.Equal(workerContent(t, user, att.ID, "thumbnail"), thumb) || !bytes.Equal(workerContent(t, user, att.ID, "original"), source) { t.Fatal("duplicate changed content") }