feat(worker): add centered 128x128 JPEG crop
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user