Добавить версионированный pipeline и проверку идемпотентности.

Трек 2 фиксирует дубль, устаревшую версию, падение до и после записи объекта и replay без второй копии результата.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-09 01:23:49 +03:00
co-authored by Cursor
parent 5617dca7d2
commit cf9d9a918f
10 changed files with 641 additions and 72 deletions
+16 -10
View File
@@ -69,27 +69,33 @@ def _crop_square(image: Image.Image, side: int) -> Image.Image:
return cropped
def _jpeg(image: Image.Image) -> bytes:
def _jpeg(image: Image.Image, quality: int) -> bytes:
buffer = io.BytesIO()
image.save(buffer, format="JPEG", quality=90, optimize=True)
image.save(buffer, format="JPEG", quality=quality, optimize=True)
return buffer.getvalue()
def _webp(image: Image.Image) -> bytes:
# quality=80, method=6. EXIF is omitted because the saved image is a new RGB buffer.
def _webp(image: Image.Image, quality: int) -> bytes:
# method=6. EXIF is omitted because the saved image is a new RGB buffer.
buffer = io.BytesIO()
image.save(buffer, format="WEBP", quality=80, method=6)
image.save(buffer, format="WEBP", quality=quality, method=6)
return buffer.getvalue()
def render(data: bytes, content_type: str) -> dict:
"""Return thumbnail, crop and optimized payloads. Does not touch storage."""
def render(data: bytes, content_type: str, pipeline_version: int = 1) -> dict:
"""Return thumbnail, crop and optimized payloads. Does not touch storage.
Version 1 is the contract profile (JPEG 90, WebP 80). A later version lowers
quality so a stale writer cannot be confused with the newer bytes.
"""
jpeg_quality = 90 if pipeline_version <= 1 else 60
webp_quality = 80 if pipeline_version <= 1 else 50
image = _open_rgb(data, content_type)
thumb = _fit_down(image, 256)
crop = _crop_square(image, 128)
optimized = _fit_down(image, 1280)
return {
"thumbnail": ( _jpeg(thumb), "image/jpeg", thumb.size[0], thumb.size[1]),
"crop": (_jpeg(crop), "image/jpeg", crop.size[0], crop.size[1]),
"optimized": (_webp(optimized), "image/webp", optimized.size[0], optimized.size[1]),
"thumbnail": (_jpeg(thumb, jpeg_quality), "image/jpeg", thumb.size[0], thumb.size[1]),
"crop": (_jpeg(crop, jpeg_quality), "image/jpeg", crop.size[0], crop.size[1]),
"optimized": (_webp(optimized, webp_quality), "image/webp", optimized.size[0], optimized.size[1]),
}