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:
Artyom
2026-10-29 03:00:11 +03:00
co-authored by Claude Opus 5.5
parent 045d631c12
commit 431b82fd43
9 changed files with 222 additions and 82 deletions
@@ -1,8 +1,8 @@
## 1. Варианты crop и optimized
- [ ] 1.1 `render_variants`: проверка байтов и пикселей по заголовку, draft для JPEG, ориентация без копии, компоновка на белый, `thumbnail`, `crop` 128×128 JPEG, `optimized` WebP в 1280×1280 без метаданных; ошибка `ImageTooDemanding` в домене зоны
- [ ] 1.2 Тесты рендера и pipeline: три варианта у fixture, геометрия crop и optimized (вытянутые, маленькие, EXIF 6, альфа), отсутствие EXIF, `ready` только с полным набором, `404` на вариант вложения лабы 4
- [ ] 1.3 Проверка группы: `make lint`, `make student-test`
- [x] 1.1 `render_variants`: проверка байтов и пикселей по заголовку, draft для JPEG, ориентация без копии, компоновка на белый, `thumbnail`, `crop` 128×128 JPEG, `optimized` WebP в 1280×1280 без метаданных; ошибка `ImageTooDemanding` в домене зоны
- [x] 1.2 Тесты рендера и pipeline: три варианта у fixture, геометрия crop и optimized (вытянутые, маленькие, EXIF 6, альфа), отсутствие EXIF, `ready` только с полным набором, `404` на вариант вложения лабы 4
- [x] 1.3 Проверка группы: `make lint`, `make student-test`
## 2. Пределы worker
@@ -35,3 +35,7 @@ class UploadAborted(AttachmentError):
class UnreadableImage(AttachmentError):
pass
class ImageTooDemanding(AttachmentError):
pass
@@ -2,7 +2,11 @@ from dataclasses import dataclass
from uuid import UUID
THUMBNAIL = "thumbnail"
CROP = "crop"
OPTIMIZED = "optimized"
THUMBNAIL_BOX = 256
CROP_SIDE = 128
OPTIMIZED_BOX = 1280
VARIANTS_PREFIX = "variants/"
+7 -2
View File
@@ -39,7 +39,11 @@ from messenger.attachments.application.queries import GetAttachment, OpenAttachm
from messenger.attachments.config import AttachmentsSettings, PipelineSettings
from messenger.attachments.domain.jobs import RetryPolicy
from messenger.attachments.infrastructure.faults import EnvironmentFaults
from messenger.attachments.infrastructure.images import PillowImageInspector, PillowVariantRenderer
from messenger.attachments.infrastructure.images import (
PillowImageInspector,
PillowVariantRenderer,
RenderLimits,
)
from messenger.attachments.infrastructure.postgres import (
PostgresAttachmentDirectory,
PostgresAttachmentsUnitOfWork,
@@ -170,7 +174,8 @@ class PipelineProvider(Provider):
class WorkerProvider(Provider):
@provide(scope=Scope.APP)
def renderer(self, settings: PipelineSettings) -> VariantRenderer:
return PillowVariantRenderer(settings.max_image_pixels, settings.worker_concurrency)
limits = RenderLimits(settings.max_upload_bytes, settings.max_image_pixels)
return PillowVariantRenderer(limits, settings.worker_concurrency)
@provide(scope=Scope.APP)
def faults(self, settings: PipelineSettings, logger: Logger) -> Faults:
@@ -1,20 +1,43 @@
import asyncio
import warnings
from dataclasses import dataclass
from io import BytesIO
from PIL import Image, ImageOps
from messenger.attachments.application.ports import Rendition
from messenger.attachments.domain.errors import PayloadTooLarge, UnreadableImage, UnsupportedImage
from messenger.attachments.domain.errors import (
ImageTooDemanding,
PayloadTooLarge,
UnreadableImage,
UnsupportedImage,
)
from messenger.attachments.domain.values import ImageInfo, ImageKind
from messenger.attachments.domain.variants import THUMBNAIL, THUMBNAIL_BOX, fit
from messenger.attachments.domain.variants import (
CROP,
CROP_SIDE,
OPTIMIZED,
OPTIMIZED_BOX,
THUMBNAIL,
THUMBNAIL_BOX,
fit,
)
KINDS = {"PNG": ImageKind.PNG, "JPEG": ImageKind.JPEG}
BOMBS = (Image.DecompressionBombError, Image.DecompressionBombWarning)
JPEG_SCALE = 8
WIDE_MODES = frozenset({"I", "I;16", "I;16B", "I;16L", "I;16N"})
ALPHA_MODES = frozenset({"LA", "La", "PA", "RGBa"})
JPEG_QUALITY = 85
JPEG = {"quality": 85, "optimize": True}
WEBP = {"quality": 80, "method": 4}
ENCODINGS = {"JPEG": (ImageKind.JPEG.value, "jpg"), "WEBP": ("image/webp", "webp")}
REDUCING_GAP = 3.0
@dataclass(frozen=True, slots=True)
class RenderLimits:
max_bytes: int
max_pixels: int
class PillowImageInspector:
@@ -68,35 +91,56 @@ def flatten(image: Image.Image) -> Image.Image:
return image if image.mode == "RGB" else image.convert("RGB")
def jpeg(name: str, image: Image.Image, box: int) -> Rendition:
size = fit(image.width, image.height, box)
scaled = image if size == image.size else image.resize(size, Image.Resampling.LANCZOS)
def scaled(image: Image.Image, size: tuple[int, int]) -> Image.Image:
if size == image.size:
return image
return image.resize(size, Image.Resampling.LANCZOS, reducing_gap=REDUCING_GAP)
def encoded(name: str, image: Image.Image, kind: str, **options: object) -> Rendition:
buffer = BytesIO()
scaled.save(buffer, "JPEG", quality=JPEG_QUALITY, optimize=True)
return Rendition(name, ImageKind.JPEG.value, "jpg", buffer.getvalue(), *size)
image.save(buffer, kind, **options)
content_type, extension = ENCODINGS[kind]
return Rendition(name, content_type, extension, buffer.getvalue(), *image.size)
def render_variants(data: bytes, max_pixels: int) -> list[Rendition]:
def decoded(data: bytes, max_pixels: int) -> tuple[Image.Image, int, int]:
try:
with Image.open(BytesIO(data), formats=tuple(KINDS)) as image:
if image.width * image.height > max_pixels:
raise UnreadableImage("изображение больше допустимого числа пикселей")
image.load()
upright = flatten(ImageOps.exif_transpose(image))
except UnreadableImage:
image = Image.open(BytesIO(data), formats=tuple(KINDS))
width, height = image.size
if width * height > max_pixels:
raise UnreadableImage("изображение больше допустимого числа пикселей")
image.draft(None, fit(width, height, OPTIMIZED_BOX))
stored = image.size
ImageOps.exif_transpose(image, in_place=True)
if image.size != stored:
width, height = height, width
return flatten(image), width, height
except UnreadableImage, MemoryError:
raise
except Exception as error:
raise UnreadableImage("изображение не декодируется") from error
return [jpeg(THUMBNAIL, upright, THUMBNAIL_BOX)]
def render_variants(data: bytes, limits: RenderLimits) -> list[Rendition]:
if len(data) > limits.max_bytes:
raise ImageTooDemanding("оригинал больше допустимого размера")
upright, width, height = decoded(data, limits.max_pixels)
square = ImageOps.fit(upright, (CROP_SIDE, CROP_SIDE), Image.Resampling.LANCZOS)
return [
encoded(THUMBNAIL, scaled(upright, fit(width, height, THUMBNAIL_BOX)), "JPEG", **JPEG),
encoded(CROP, square, "JPEG", **JPEG),
encoded(OPTIMIZED, scaled(upright, fit(width, height, OPTIMIZED_BOX)), "WEBP", **WEBP),
]
class PillowVariantRenderer:
__slots__ = ("_max_pixels", "_slots")
__slots__ = ("_limits", "_slots")
def __init__(self, max_pixels: int, concurrency: int) -> None:
self._max_pixels = max_pixels
def __init__(self, limits: RenderLimits, concurrency: int) -> None:
self._limits = limits
self._slots = asyncio.Semaphore(concurrency)
async def render(self, data: bytes, pipeline_version: int) -> list[Rendition]:
async with self._slots:
return await asyncio.to_thread(render_variants, data, self._max_pixels)
return await asyncio.to_thread(render_variants, data, self._limits)
+29 -15
View File
@@ -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))
+28 -3
View File
@@ -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)
+11 -4
View File
@@ -1,3 +1,4 @@
import asyncio
from datetime import timedelta
from uuid import UUID
@@ -21,13 +22,15 @@ from messenger.attachments.application.ports import Faults, ObjectStorage, Varia
from messenger.attachments.domain.jobs import Job, Outcome, RetryPolicy
from messenger.attachments.entrypoints.schemas import JobMessage
from messenger.attachments.infrastructure.faults import EnvironmentFaults
from messenger.attachments.infrastructure.images import PillowVariantRenderer
from messenger.attachments.infrastructure.images import PillowVariantRenderer, RenderLimits
from messenger.attachments.infrastructure.postgres import PostgresAttachmentsUnitOfWork
from messenger.shared.application.errors import Unavailable
from messenger.shared.infrastructure.clock import MonotonicUtcClock
from messenger.shared.infrastructure.ids import Uuid7Generator
from messenger.shared.infrastructure.postgres import Database
IDLE_SECONDS = 0.1
LIMITS = RenderLimits(10_485_760, 40_000_000)
FAST = RetryPolicy(max_attempts=5, base_seconds=0.001, cap_seconds=0.001)
@@ -62,7 +65,7 @@ class InlinePipeline:
self.policy = policy
self.lease = lease
self.publisher = CapturingPublisher()
self.renderer: VariantRenderer = PillowVariantRenderer(40_000_000, 2)
self.renderer: VariantRenderer = PillowVariantRenderer(LIMITS, 2)
self.faults: Faults = EnvironmentFaults("", NullLogger())
def uow(self) -> PostgresAttachmentsUnitOfWork:
@@ -88,12 +91,16 @@ class InlinePipeline:
)
return await handler(ProcessJob(job))
async def drain(self, rounds: int = 20) -> list[Outcome]:
async def drain(self, rounds: int = 20, idle: int = 3) -> list[Outcome]:
outcomes: list[Outcome] = []
quiet = 0
for _ in range(rounds):
jobs = await self.publish()
if not jobs:
quiet = 0 if jobs else quiet + 1
if quiet == idle:
return outcomes
if not jobs:
await asyncio.sleep(IDLE_SECONDS)
outcomes.extend([await self.process(job) for job in jobs])
return outcomes
+72 -35
View File
@@ -3,11 +3,17 @@ from io import BytesIO
import pytest
from PIL import Image
from messenger.attachments.domain.errors import UnreadableImage
from messenger.attachments.infrastructure.images import render_variants
from messenger.attachments.application.ports import Rendition
from messenger.attachments.domain.errors import ImageTooDemanding, UnreadableImage
from messenger.attachments.infrastructure.images import RenderLimits, render_variants
from pictures import SAMPLE
LIMIT = 40_000_000
LIMITS = RenderLimits(max_bytes=10_485_760, max_pixels=40_000_000)
FORMATS = {
"thumbnail": ("image/jpeg", "jpg", "JPEG"),
"crop": ("image/jpeg", "jpg", "JPEG"),
"optimized": ("image/webp", "webp", "WEBP"),
}
def encoded(image: Image.Image, kind: str = "PNG", **options: object) -> bytes:
@@ -16,62 +22,88 @@ def encoded(image: Image.Image, kind: str = "PNG", **options: object) -> bytes:
return buffer.getvalue()
def thumbnail(data: bytes) -> Image.Image:
(rendition,) = render_variants(data, LIMIT)
assert (rendition.name, rendition.content_type, rendition.extension) == (
"thumbnail",
"image/jpeg",
"jpg",
)
image = Image.open(BytesIO(rendition.data))
assert image.format == "JPEG"
assert image.size == (rendition.width, rendition.height)
assert not image.getexif()
return image
def rendered(data: bytes) -> dict[str, Image.Image]:
renditions: list[Rendition] = render_variants(data, LIMITS)
assert [rendition.name for rendition in renditions] == ["thumbnail", "crop", "optimized"]
images = {}
for rendition in renditions:
content_type, extension, kind = FORMATS[rendition.name]
assert (rendition.content_type, rendition.extension) == (content_type, extension)
image = Image.open(BytesIO(rendition.data))
assert image.format == kind
assert image.size == (rendition.width, rendition.height)
assert not image.getexif()
assert not {"icc_profile", "exif", "xmp"} & set(image.info)
images[rendition.name] = image
return images
def test_fixture_gives_256_by_160() -> None:
assert thumbnail(SAMPLE).size == (256, 160)
def sizes(data: bytes) -> tuple[tuple[int, int], ...]:
images = rendered(data)
return tuple(images[name].size for name in ("thumbnail", "crop", "optimized"))
def test_fixture_gives_three_variants() -> None:
assert sizes(SAMPLE) == ((256, 160), (128, 128), (320, 200))
@pytest.mark.parametrize(
("mode", "size", "fitted"),
("mode", "size", "thumbnail", "optimized"),
[
("L", (17, 1000), (4, 256)),
("RGB", (64, 64), (64, 64)),
("RGB", (3000, 2), (256, 1)),
("I;16", (300, 200), (256, 170)),
("CMYK", (801, 601), (256, 192)),
("L", (17, 1000), (4, 256), (17, 1000)),
("RGB", (64, 64), (64, 64), (64, 64)),
("RGB", (3000, 2), (256, 1), (1280, 1)),
("I;16", (300, 200), (256, 170), (300, 200)),
("CMYK", (801, 601), (256, 192), (801, 601)),
("RGB", (4000, 3000), (256, 192), (1280, 960)),
],
)
def test_sizes_round_down_and_never_upscale(
mode: str, size: tuple[int, int], fitted: tuple[int, int]
def test_sizes_round_down_never_upscale_and_crop_is_square(
mode: str, size: tuple[int, int], thumbnail: tuple[int, int], optimized: tuple[int, int]
) -> None:
kind = "JPEG" if mode == "CMYK" else "PNG"
assert thumbnail(encoded(Image.new(mode, size), kind)).size == fitted
kind = "JPEG" if mode == "CMYK" or size == (4000, 3000) else "PNG"
assert sizes(encoded(Image.new(mode, size), kind)) == (thumbnail, (128, 128), optimized)
def test_crop_takes_the_centre() -> None:
image = Image.new("RGB", (300, 100), (255, 0, 0))
image.paste((0, 0, 255), (100, 0, 200, 100))
crop = rendered(encoded(image))["crop"].convert("RGB")
pixels = [crop.getpixel((x, 64)) for x in (2, 64, 125)]
assert all(isinstance(p, tuple) and p[0] < 60 and p[2] > 200 for p in pixels)
@pytest.mark.parametrize("mode", ["RGBA", "LA"])
def test_transparency_is_composed_on_white(mode: str) -> None:
transparent = Image.new(mode, (300, 300), (0,) * len(mode))
assert thumbnail(encoded(transparent)).convert("RGB").getpixel((5, 5)) == (255, 255, 255)
for image in rendered(encoded(transparent)).values():
assert image.convert("RGB").getpixel((5, 5)) == (255, 255, 255)
def test_palette_transparency_is_composed_on_white() -> None:
palette = Image.new("P", (200, 100))
image = thumbnail(encoded(palette, transparency=0))
image = rendered(encoded(palette, transparency=0))["thumbnail"]
assert image.size == (200, 100)
assert image.convert("RGB").getpixel((5, 5)) == (255, 255, 255)
@pytest.mark.parametrize(("orientation", "fitted"), [(6, (128, 256)), (3, (256, 128))])
@pytest.mark.parametrize(
("orientation", "thumbnail", "optimized"),
[(6, (128, 256), (200, 400)), (3, (256, 128), (400, 200))],
)
def test_exif_orientation_is_applied_before_sizing(
orientation: int, fitted: tuple[int, int]
orientation: int, thumbnail: tuple[int, int], optimized: tuple[int, int]
) -> None:
exif = Image.Exif()
exif[0x0112] = orientation
data = encoded(Image.new("RGB", (400, 200), (10, 200, 10)), "JPEG", exif=exif)
assert thumbnail(data).size == fitted
data = encoded(
Image.new("RGB", (400, 200), (10, 200, 10)), "JPEG", exif=exif, icc_profile=b"x" * 64
)
assert sizes(data) == (thumbnail, (128, 128), optimized)
def test_same_bytes_give_same_variants() -> None:
assert render_variants(SAMPLE, LIMITS) == render_variants(SAMPLE, LIMITS)
@pytest.mark.parametrize(
@@ -81,9 +113,14 @@ def test_exif_orientation_is_applied_before_sizing(
)
def test_undecodable_data_is_a_permanent_error(data: bytes) -> None:
with pytest.raises(UnreadableImage):
render_variants(data, LIMIT)
render_variants(data, LIMITS)
def test_pixel_limit_is_checked_before_decoding() -> None:
with pytest.raises(UnreadableImage):
render_variants(SAMPLE, 320 * 200 - 1)
render_variants(SAMPLE, RenderLimits(LIMITS.max_bytes, 320 * 200 - 1))
def test_byte_limit_is_checked_before_decoding() -> None:
with pytest.raises(ImageTooDemanding):
render_variants(SAMPLE, RenderLimits(len(SAMPLE) - 1, LIMITS.max_pixels))