Render crop and optimized variants next to the thumbnail
The worker decodes the original once (JPEG draft down to the optimized box, EXIF orientation applied in place, alpha composed on white) and encodes three variants: the 256 px thumbnail, a centred 128x128 JPEG crop that may upscale and a WebP fitted into 1280x1280 without upscaling, quality 80, method 4. No EXIF, ICC or XMP is written. The byte limit is checked before decoding. ready is set by the transaction that stores all three rows, so a partial set is never published. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
045d631c12
commit
431b82fd43
@@ -1,11 +1,13 @@
|
||||
from collections.abc import AsyncIterator
|
||||
from hashlib import sha256
|
||||
from io import BytesIO
|
||||
from typing import Any
|
||||
from urllib.parse import quote
|
||||
from uuid import uuid4
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
import pytest
|
||||
from httpx import AsyncClient, Response
|
||||
from PIL import Image
|
||||
|
||||
from conftest import actor, make_chat, register, resolve, scalar
|
||||
from fakes import MemoryObjectStorage
|
||||
@@ -85,7 +87,7 @@ async def test_member_uploads_and_another_member_downloads(client: AsyncClient)
|
||||
None,
|
||||
attachment["sha256"],
|
||||
)
|
||||
assert [variant["name"] for variant in ready["variants"]] == ["thumbnail"]
|
||||
assert [variant["name"] for variant in ready["variants"]] == ["crop", "optimized", "thumbnail"]
|
||||
content = await client.get(
|
||||
f"{path}/content", headers={**actor(bob), "Accept": "application/json"}
|
||||
)
|
||||
@@ -242,28 +244,40 @@ async def test_display_name_is_kept(client: AsyncClient, filename: str) -> None:
|
||||
assert response.json()["filename"] == filename
|
||||
|
||||
|
||||
async def test_content_waits_for_ready_and_serves_level_three_variants(
|
||||
client: AsyncClient,
|
||||
) -> None:
|
||||
async def test_content_waits_for_ready_and_serves_every_variant(client: AsyncClient) -> None:
|
||||
alice, bob, _, chat = await trio(client)
|
||||
attachment = (await upload(client, alice, chat)).json()
|
||||
path = f"/api/v1/attachments/{attachment['id']}/content"
|
||||
for variant in ("original", "thumbnail", "crop"):
|
||||
for variant in ("original", "thumbnail", "crop", "optimized"):
|
||||
early = await client.get(f"{path}?variant={variant}", headers=actor(bob))
|
||||
assert early.status_code == 409, early.text
|
||||
assert early.json()["error"]["code"] == "conflict"
|
||||
await settle(client)
|
||||
assert (await client.get(f"{path}?variant=original", headers=actor(alice))).status_code == 200
|
||||
thumbnail = await client.get(f"{path}?variant=thumbnail", headers=actor(bob))
|
||||
meta = (await client.get(path.removesuffix("/content"), headers=actor(bob))).json()
|
||||
(variant,) = meta["variants"]
|
||||
assert thumbnail.headers["content-type"] == "image/jpeg"
|
||||
assert len(thumbnail.content) == variant["size_bytes"]
|
||||
assert sha256(thumbnail.content).hexdigest() == variant["sha256"]
|
||||
assert (variant["width"], variant["height"]) == (256, 160)
|
||||
for absent in ("crop", "optimized"):
|
||||
missing = await client.get(f"{path}?variant={absent}", headers=actor(alice))
|
||||
assert missing.status_code == 404
|
||||
expected = {
|
||||
"crop": ("image/jpeg", "JPEG", (128, 128)),
|
||||
"optimized": ("image/webp", "WEBP", (320, 200)),
|
||||
"thumbnail": ("image/jpeg", "JPEG", (256, 160)),
|
||||
}
|
||||
for variant in meta["variants"]:
|
||||
served = await client.get(f"{path}?variant={variant['name']}", headers=actor(bob))
|
||||
content_type, kind, size = expected[variant["name"]]
|
||||
assert (served.headers["content-type"], variant["content_type"]) == (content_type,) * 2
|
||||
assert len(served.content) == variant["size_bytes"]
|
||||
assert sha256(served.content).hexdigest() == variant["sha256"]
|
||||
image = Image.open(BytesIO(served.content))
|
||||
assert (image.format, image.size, (variant["width"], variant["height"])) == (
|
||||
kind,
|
||||
size,
|
||||
size,
|
||||
)
|
||||
await scalar(
|
||||
"DELETE FROM attachment_variants WHERE attachment_id = $1 AND name = 'crop'",
|
||||
UUID(attachment["id"]),
|
||||
)
|
||||
missing = await client.get(f"{path}?variant=crop", headers=actor(alice))
|
||||
assert missing.status_code == 404
|
||||
assert (await client.get(f"{path}?variant=preview", headers=actor(alice))).status_code == 400
|
||||
assert (
|
||||
await client.get("/api/v1/attachments/x/content", headers=actor(alice))
|
||||
|
||||
Reference in New Issue
Block a user