feat(worker): add optimized WebP variant
This commit is contained in:
@@ -4,7 +4,9 @@ package studenttests_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
@@ -14,6 +16,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/image/webp"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
)
|
||||
|
||||
@@ -54,7 +58,7 @@ func TestThumbnail(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestThumbnailEXIF(t *testing.T) {
|
||||
func TestVariantsEXIF(t *testing.T) {
|
||||
src := image.NewRGBA(image.Rect(0, 0, 40, 20))
|
||||
|
||||
for y := 0; y < 20; y++ {
|
||||
@@ -86,24 +90,27 @@ func TestThumbnailEXIF(t *testing.T) {
|
||||
data = append(data, segment...)
|
||||
data = append(data, exif...)
|
||||
data = append(data, original.Bytes()[2:]...)
|
||||
result, width, height := processedVariant(t, data, "image/jpeg", "thumbnail")
|
||||
for _, variant := range []string{"thumbnail", "optimized"} {
|
||||
t.Run(variant, func(t *testing.T) {
|
||||
result, width, height := processedVariant(t, data, "image/jpeg", variant)
|
||||
if width != 20 || height != 40 {
|
||||
t.Fatalf("orientation ignored: %dx%d", width, height)
|
||||
}
|
||||
|
||||
if width != 20 || height != 40 {
|
||||
t.Fatalf("orientation ignored: %dx%d", width, height)
|
||||
}
|
||||
decoded, _, err := image.Decode(bytes.NewReader(result))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
decoded, err := jpeg.Decode(bytes.NewReader(result))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r, _, b, _ := decoded.At(10, 5).RGBA()
|
||||
if r <= b {
|
||||
t.Fatal("incorrect EXIF rotation")
|
||||
}
|
||||
|
||||
r, _, b, _ := decoded.At(10, 5).RGBA()
|
||||
if r <= b {
|
||||
t.Fatal("incorrect EXIF rotation")
|
||||
}
|
||||
|
||||
if bytes.Contains(result, []byte("Exif")) {
|
||||
t.Fatal("EXIF retained")
|
||||
if bytes.Contains(result, []byte("Exif")) || bytes.Contains(result, []byte("EXIF")) {
|
||||
t.Fatal("EXIF retained")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +135,12 @@ func processedVariant(t *testing.T, source []byte, contentType, variant string)
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sum := sha256.Sum256(data)
|
||||
if len(data) != v.SizeBytes || hex.EncodeToString(sum[:]) != v.Sha256 ||
|
||||
resp.Header.Get("Content-Type") != string(v.ContentType) {
|
||||
t.Fatal("variant content disagrees with metadata")
|
||||
}
|
||||
|
||||
return data, v.Width, v.Height
|
||||
}
|
||||
t.Fatalf("ready attachment has no %s", variant)
|
||||
@@ -180,3 +193,34 @@ func TestCrop(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOptimized(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
width, height, wantWidth, wantHeight int
|
||||
}{
|
||||
{"landscape", 1600, 1000, 1280, 800},
|
||||
{"portrait", 1000, 1600, 800, 1280},
|
||||
{"small", 12, 7, 12, 7},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var source bytes.Buffer
|
||||
if err := png.Encode(&source, image.NewNRGBA(image.Rect(0, 0, tc.width, tc.height))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, width, height := processedVariant(t, source.Bytes(), "image/png", "optimized")
|
||||
decoded, err := webp.Decode(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if width != tc.wantWidth || height != tc.wantHeight ||
|
||||
decoded.Bounds().Dx() != width || decoded.Bounds().Dy() != height {
|
||||
t.Fatalf("unexpected optimized dimensions: metadata=%dx%d, WebP=%v", width, height, decoded.Bounds())
|
||||
}
|
||||
_, _, _, alpha := decoded.At(0, 0).RGBA()
|
||||
if alpha != 0 {
|
||||
t.Fatal("optimized image lost transparency")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/image/webp"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
)
|
||||
@@ -160,17 +161,23 @@ func TestWorkerPersistenceAndDuplicate(t *testing.T) {
|
||||
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.
|
||||
optimized := workerContent(t, user, att.ID, "optimized")
|
||||
cfg, err = webp.DecodeConfig(bytes.NewReader(optimized))
|
||||
if err != nil || cfg.Width != 320 || cfg.Height != 200 {
|
||||
t.Fatal("invalid optimized WebP")
|
||||
}
|
||||
|
||||
workerSQL(t, "UPDATE outbox SET published_at=NULL WHERE "+where)
|
||||
workerWait(t, "duplicate publication", 15*time.Second, func() bool {
|
||||
return workerSQL(t, "SELECT published_at IS NOT NULL FROM outbox WHERE "+where) == "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) != "2" {
|
||||
if !reflect.DeepEqual(ready, duplicate) || workerSQL(t, "SELECT count(*) FROM attachment_variants WHERE "+where) != "3" {
|
||||
t.Fatal("duplicate job changed metadata or duplicated variants")
|
||||
}
|
||||
if !bytes.Equal(workerContent(t, user, att.ID, "crop"), crop) ||
|
||||
if !bytes.Equal(workerContent(t, user, att.ID, "optimized"), optimized) ||
|
||||
!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")
|
||||
|
||||
Reference in New Issue
Block a user