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))
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import asyncio
|
||||
import json
|
||||
import re
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
@@ -74,8 +75,13 @@ async def test_job_is_processed_once_and_staging_is_cleaned(client: AsyncClient)
|
||||
assert (done["status"], done["job_status"], done["published_version"]) == ("ready", "done", 1)
|
||||
assert done["original_key"] == f"originals/{attachment_id}"
|
||||
assert done["staging_key"] == f"staging/{attachment_id}"
|
||||
(thumbnail,) = await variants(attachment_id)
|
||||
assert thumbnail["object_key"].startswith(f"variants/{attachment_id}/1/thumbnail-")
|
||||
pattern = rf"variants/{attachment_id}/1/(crop|optimized|thumbnail)-[0-9a-f]{{16}}\.(jpg|webp)"
|
||||
found = [re.fullmatch(pattern, row["object_key"]) for row in await variants(attachment_id)]
|
||||
assert [match.groups() for match in found if match] == [
|
||||
("crop", "jpg"),
|
||||
("optimized", "webp"),
|
||||
("thumbnail", "jpg"),
|
||||
]
|
||||
assert await pipeline.clean() >= 1
|
||||
assert (await state(attachment_id))["staging_key"] is None
|
||||
objects = (await storage(client)).objects
|
||||
@@ -127,10 +133,29 @@ async def test_two_copies_in_flight_give_one_result(client: AsyncClient) -> None
|
||||
job = await job_of(attachment_id)
|
||||
outcomes = await asyncio.gather(pipeline.process(job), pipeline.process(job))
|
||||
assert sorted(outcomes) == sorted([Outcome.DONE, Outcome.DUPLICATE])
|
||||
assert len(await variants(attachment_id)) == 1
|
||||
assert len(await variants(attachment_id)) == 3
|
||||
assert (await state(attachment_id))["claims"] == 1
|
||||
|
||||
|
||||
async def test_partial_variant_set_is_never_published(client: AsyncClient) -> None:
|
||||
_, _, attachment_id = await accepted(client)
|
||||
pipeline = await pipeline_of(client)
|
||||
await pipeline.publish()
|
||||
store = await storage(client)
|
||||
|
||||
async def refuse(key: str) -> None:
|
||||
if "/optimized-" in key:
|
||||
raise Unavailable("S3 недоступно")
|
||||
|
||||
store.after_put = refuse
|
||||
try:
|
||||
assert await pipeline.process(await job_of(attachment_id)) is Outcome.RESCHEDULED
|
||||
finally:
|
||||
store.after_put = None
|
||||
assert (await state(attachment_id))["status"] == "queued"
|
||||
assert await variants(attachment_id) == []
|
||||
|
||||
|
||||
async def test_transient_error_reschedules_with_the_same_job(client: AsyncClient) -> None:
|
||||
_, _, attachment_id = await accepted(client)
|
||||
pipeline = await pipeline_of(client)
|
||||
|
||||
Reference in New Issue
Block a user