282 lines
6.8 KiB
Python
282 lines
6.8 KiB
Python
"""Deterministic image variants for the attachment pipeline."""
|
|
|
|
import hashlib
|
|
import io
|
|
import multiprocessing
|
|
import time
|
|
import warnings
|
|
from dataclasses import dataclass
|
|
|
|
from PIL import Image, ImageOps, UnidentifiedImageError
|
|
|
|
from app.core.config import settings
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ProcessedVariant:
|
|
name: str
|
|
content_type: str
|
|
extension: str
|
|
width: int
|
|
height: int
|
|
data: bytes
|
|
sha256: str
|
|
|
|
|
|
class ImageProcessingTimeout(TimeoutError):
|
|
"""Raised when image processing exceeds its configured deadline."""
|
|
|
|
|
|
def _load_oriented(data: bytes) -> Image.Image:
|
|
if len(data) > settings.max_upload_bytes:
|
|
raise ValueError("image_too_large")
|
|
|
|
Image.MAX_IMAGE_PIXELS = settings.max_image_pixels
|
|
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("error", Image.DecompressionBombWarning)
|
|
|
|
try:
|
|
source = Image.open(io.BytesIO(data))
|
|
|
|
if source.format not in {"JPEG", "PNG"}:
|
|
raise ValueError("unsupported_image")
|
|
|
|
source.verify()
|
|
source = Image.open(io.BytesIO(data))
|
|
image = ImageOps.exif_transpose(source)
|
|
image.load()
|
|
|
|
except (
|
|
UnidentifiedImageError,
|
|
OSError,
|
|
Image.DecompressionBombError,
|
|
Image.DecompressionBombWarning,
|
|
):
|
|
raise ValueError("invalid_image") from None
|
|
|
|
if image.width * image.height > settings.max_image_pixels:
|
|
raise ValueError("image_too_many_pixels")
|
|
|
|
return image
|
|
|
|
|
|
def _rgb_white(image: Image.Image) -> Image.Image:
|
|
if image.mode in ("RGBA", "LA") or (
|
|
image.mode == "P" and "transparency" in image.info
|
|
):
|
|
rgba = image.convert("RGBA")
|
|
background = Image.new(
|
|
"RGBA",
|
|
rgba.size,
|
|
(255, 255, 255, 255),
|
|
)
|
|
return Image.alpha_composite(background, rgba).convert("RGB")
|
|
|
|
return image.convert("RGB")
|
|
|
|
|
|
def _fit(
|
|
image: Image.Image,
|
|
box: tuple[int, int],
|
|
allow_upscale: bool = False,
|
|
) -> Image.Image:
|
|
image = image.copy()
|
|
image.thumbnail(box, Image.Resampling.LANCZOS)
|
|
|
|
if allow_upscale:
|
|
scale = min(
|
|
box[0] / image.width,
|
|
box[1] / image.height,
|
|
)
|
|
|
|
if scale > 1:
|
|
image = image.resize(
|
|
(
|
|
max(1, int(image.width * scale)),
|
|
max(1, int(image.height * scale)),
|
|
),
|
|
Image.Resampling.LANCZOS,
|
|
)
|
|
|
|
return image
|
|
|
|
|
|
def _encode(
|
|
name: str,
|
|
image: Image.Image,
|
|
fmt: str,
|
|
content_type: str,
|
|
extension: str,
|
|
**kwargs,
|
|
) -> ProcessedVariant:
|
|
output = io.BytesIO()
|
|
image.save(output, format=fmt, **kwargs)
|
|
data = output.getvalue()
|
|
|
|
return ProcessedVariant(
|
|
name=name,
|
|
content_type=content_type,
|
|
extension=extension,
|
|
width=image.width,
|
|
height=image.height,
|
|
data=data,
|
|
sha256=hashlib.sha256(data).hexdigest(),
|
|
)
|
|
|
|
|
|
def create_variants(original: bytes) -> list[ProcessedVariant]:
|
|
source = _load_oriented(original)
|
|
rgb = _rgb_white(source)
|
|
|
|
# 256x256 fit; never upscale; dimensions retain aspect ratio.
|
|
thumb = _fit(rgb, (256, 256))
|
|
thumbnail = _encode(
|
|
"thumbnail",
|
|
thumb,
|
|
"JPEG",
|
|
"image/jpeg",
|
|
"jpg",
|
|
quality=85,
|
|
optimize=True,
|
|
progressive=True,
|
|
)
|
|
|
|
# Center crop to square, 128x128. Upscaling is allowed by contract.
|
|
side = min(rgb.width, rgb.height)
|
|
left = (rgb.width - side) // 2
|
|
top = (rgb.height - side) // 2
|
|
crop = rgb.crop((left, top, left + side, top + side))
|
|
crop = crop.resize((128, 128), Image.Resampling.LANCZOS)
|
|
|
|
cropped = _encode(
|
|
"crop",
|
|
crop,
|
|
"JPEG",
|
|
"image/jpeg",
|
|
"jpg",
|
|
quality=85,
|
|
optimize=True,
|
|
progressive=True,
|
|
)
|
|
|
|
# Optimized WebP fit 1280x1280; never upscale.
|
|
webp_image = _fit(rgb, (1280, 1280))
|
|
webp = _encode(
|
|
"optimized",
|
|
webp_image,
|
|
"WEBP",
|
|
"image/webp",
|
|
"webp",
|
|
quality=82,
|
|
method=6,
|
|
)
|
|
|
|
return [thumbnail, cropped, webp]
|
|
|
|
|
|
def _variant_process_entry(connection, original: bytes) -> None:
|
|
"""Run Pillow processing in an isolated child process."""
|
|
|
|
try:
|
|
result = create_variants(original)
|
|
connection.send(("ok", result))
|
|
|
|
except ValueError as exc:
|
|
connection.send(("validation_error", str(exc)[:100]))
|
|
|
|
except Exception:
|
|
connection.send(("processing_error", "processing_failed"))
|
|
|
|
finally:
|
|
connection.close()
|
|
|
|
|
|
def create_variants_with_timeout(
|
|
original: bytes,
|
|
timeout_seconds: float,
|
|
) -> list[ProcessedVariant]:
|
|
"""Run image processing in a child process with a hard deadline."""
|
|
|
|
if timeout_seconds <= 0:
|
|
raise ValueError("invalid_processing_timeout")
|
|
|
|
context = multiprocessing.get_context("spawn")
|
|
receive_connection, send_connection = context.Pipe(duplex=False)
|
|
|
|
process = context.Process(
|
|
target=_variant_process_entry,
|
|
args=(send_connection, original),
|
|
name="image-processor",
|
|
)
|
|
|
|
try:
|
|
process.start()
|
|
send_connection.close()
|
|
|
|
deadline = time.monotonic() + timeout_seconds
|
|
message = None
|
|
|
|
while time.monotonic() < deadline:
|
|
if receive_connection.poll(0.1):
|
|
try:
|
|
message = receive_connection.recv()
|
|
except EOFError:
|
|
message = None
|
|
break
|
|
|
|
if not process.is_alive():
|
|
if receive_connection.poll():
|
|
try:
|
|
message = receive_connection.recv()
|
|
except EOFError:
|
|
message = None
|
|
break
|
|
|
|
if message is None and process.is_alive():
|
|
process.terminate()
|
|
process.join(timeout=2)
|
|
|
|
if process.is_alive():
|
|
process.kill()
|
|
process.join()
|
|
|
|
raise ImageProcessingTimeout(
|
|
"image_processing_timeout"
|
|
)
|
|
|
|
process.join(timeout=2)
|
|
|
|
if process.is_alive():
|
|
process.terminate()
|
|
process.join(timeout=2)
|
|
|
|
if message is None:
|
|
raise RuntimeError("image_processing_failed")
|
|
|
|
status, payload = message
|
|
|
|
if status == "ok":
|
|
return payload
|
|
|
|
if status == "validation_error":
|
|
raise ValueError(payload)
|
|
|
|
raise RuntimeError("image_processing_failed")
|
|
|
|
finally:
|
|
if process.is_alive():
|
|
process.terminate()
|
|
process.join(timeout=2)
|
|
|
|
if process.is_alive():
|
|
process.kill()
|
|
process.join()
|
|
|
|
receive_connection.close()
|
|
|
|
try:
|
|
send_connection.close()
|
|
except OSError:
|
|
pass
|