forked from mtusi2026/mtusi2026lab6
125 lines
5.1 KiB
Python
125 lines
5.1 KiB
Python
"""Sandboxed-per-task image work. Process is killed after JOB_TIMEOUT_SECONDS.
|
|
|
|
Outputs only JSON variant metadata. Original bytes are copied from durable staging,
|
|
never transformed. Pillow reopens/exif-transposes pixels for derivatives, stripping
|
|
metadata on re-encode. No API tokens or arbitrary user paths enter this command.
|
|
"""
|
|
import hashlib
|
|
import io
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from PIL import Image, ImageOps, UnidentifiedImageError
|
|
from botocore.exceptions import ClientError, BotoCoreError
|
|
|
|
from .attachments import BUCKET, MAX_BYTES, MAX_PIXELS, s3_client
|
|
from . import telemetry as ot
|
|
from opentelemetry.trace import SpanKind
|
|
|
|
Image.MAX_IMAGE_PIXELS = MAX_PIXELS
|
|
|
|
|
|
def rgb_image(src):
|
|
orient = ImageOps.exif_transpose(src)
|
|
orient.load()
|
|
if orient.width * orient.height > MAX_PIXELS:
|
|
raise ValueError("too many decoded pixels")
|
|
# Compose transparency on white, strip EXIF/ICC and do not enlarge.
|
|
rgba = orient.convert("RGBA")
|
|
white = Image.new("RGBA", rgba.size, (255, 255, 255, 255))
|
|
white.alpha_composite(rgba)
|
|
return white.convert("RGB")
|
|
|
|
|
|
def transform(raw):
|
|
with Image.open(io.BytesIO(raw)) as image:
|
|
if image.format not in ("PNG", "JPEG"):
|
|
raise ValueError("invalid encoding")
|
|
rgb = rgb_image(image)
|
|
t = rgb.copy()
|
|
t.thumbnail((256, 256), resample=Image.Resampling.LANCZOS)
|
|
# Center crop fit scales up small images when needed.
|
|
crop = ImageOps.fit(rgb, (128, 128), method=Image.Resampling.LANCZOS, centering=(0.5, 0.5))
|
|
optim = rgb.copy()
|
|
optim.thumbnail((1280, 1280), resample=Image.Resampling.LANCZOS)
|
|
return [
|
|
("thumbnail", t, "JPEG", "image/jpeg", {"quality": 85, "optimize": True}),
|
|
("crop", crop, "JPEG", "image/jpeg", {"quality": 85, "optimize": True}),
|
|
("optimized", optim, "WEBP", "image/webp", {"quality": 78, "method": 4}),
|
|
]
|
|
|
|
|
|
def run(attachment_id, pipeline_version):
|
|
import psycopg
|
|
from psycopg.rows import dict_row
|
|
|
|
with ot.span("db.load_staging_metadata", kind=SpanKind.CLIENT, attrs={"db.system.name":"postgresql"}):
|
|
with psycopg.connect(os.environ["DATABASE_URL"], row_factory=dict_row, connect_timeout=5) as conn:
|
|
row = conn.execute("""
|
|
SELECT * FROM attachments WHERE id=%s AND pipeline_version=%s
|
|
""", (attachment_id, pipeline_version)).fetchone()
|
|
if not row or not row["staging_key"]:
|
|
raise ValueError("staging metadata not found")
|
|
|
|
client = s3_client()
|
|
body = client.get_object(Bucket=BUCKET, Key=row["staging_key"])["Body"]
|
|
try:
|
|
raw = body.read(MAX_BYTES + 1)
|
|
finally:
|
|
body.close()
|
|
if not raw or len(raw) > MAX_BYTES or len(raw) != row["size_bytes"]:
|
|
raise ValueError("staging size mismatch")
|
|
if hashlib.sha256(raw).hexdigest() != row["sha256"]:
|
|
raise ValueError("staging checksum mismatch")
|
|
|
|
# Recompute every result if a prior run was interrupted. Keys are deterministic
|
|
# for (attachment_id,pipeline_version,variant), so duplicates don't multiply objects.
|
|
output = []
|
|
for name, image, fmt, mime, options in transform(raw):
|
|
buf = io.BytesIO()
|
|
image.save(buf, format=fmt, **options)
|
|
encoded = buf.getvalue()
|
|
key = f"variants/{attachment_id}/v{pipeline_version}/{name}"
|
|
client.put_object(Bucket=BUCKET, Key=key, Body=encoded, ContentType=mime)
|
|
output.append({"name": name, "content_type": mime,
|
|
"size_bytes": len(encoded), "sha256": hashlib.sha256(encoded).hexdigest(),
|
|
"width": image.width, "height": image.height, "object_key": key})
|
|
|
|
# Only after derivatives exist, publish unchanged primary and recovery copy.
|
|
client.copy_object(Bucket=BUCKET, Key=row["object_key"],
|
|
CopySource={"Bucket": BUCKET, "Key": row["staging_key"]},
|
|
MetadataDirective="REPLACE", ContentType=row["content_type"])
|
|
client.copy_object(Bucket=BUCKET, Key=row["backup_key"],
|
|
CopySource={"Bucket": BUCKET, "Key": row["object_key"]},
|
|
MetadataDirective="REPLACE", ContentType=row["content_type"])
|
|
return output
|
|
|
|
|
|
def main():
|
|
import uuid
|
|
if len(sys.argv) != 3:
|
|
raise SystemExit(2)
|
|
try:
|
|
ident = str(uuid.UUID(sys.argv[1]))
|
|
version = int(sys.argv[2])
|
|
if version < 1:
|
|
raise ValueError("invalid version")
|
|
ot.init("worker", child=True)
|
|
with ot.span("image.transform", context=ot.extract(json.loads(os.getenv("LAB6_PARENT_CONTEXT", "{}")))):
|
|
out = run(ident, version)
|
|
ot.event("image.transformed")
|
|
print(json.dumps({"variants": out}, ensure_ascii=False))
|
|
except (ValueError, UnidentifiedImageError, Image.DecompressionBombError, Image.DecompressionBombWarning) as exc:
|
|
print("Permanent image processing failure", file=sys.stderr)
|
|
raise SystemExit(20) from None
|
|
except (BotoCoreError, ClientError, OSError):
|
|
print("Transient object storage failure", file=sys.stderr)
|
|
raise SystemExit(30) from None
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
finally:
|
|
ot.flush()
|