Files
mtusi2026lab5/src/image_processor.py
T
2026-11-06 18:45:08 +03:00

117 lines
4.6 KiB
Python

"""One isolated process per attempt; quality: JPEG 85, WebP 80; no EXIF/ICC."""
import hashlib
import io
import os
import sys
from PIL import Image, ImageOps, UnidentifiedImageError
from sqlalchemy.orm import sessionmaker
from .attachments import MAX_BYTES, MAX_PIXELS
from .database import create_db_engine
from .jobs import now
from .models import Attachment, AttachmentJob
from .object_storage import BUCKET, s3_client
def fit_size(size, bound):
width, height = size
if max(size) <= bound:
return size
return max(1, width * bound // max(size)), max(1, height * bound // max(size))
def variants(data):
with Image.open(io.BytesIO(data), formats=['PNG', 'JPEG']) as original:
if original.width * original.height > MAX_PIXELS:
raise ValueError('pixel limit')
original.load()
oriented = ImageOps.exif_transpose(original)
rgba = oriented.convert('RGBA')
rgb = Image.new('RGB', rgba.size, 'white')
rgb.paste(rgba, mask=rgba.getchannel('A'))
rgba.close()
oriented.close()
rgb.info.clear()
try:
for name, image, fmt, mime, quality in (
('thumbnail', rgb.resize(fit_size(rgb.size, 256), Image.Resampling.LANCZOS), 'JPEG', 'image/jpeg', 85),
('crop', ImageOps.fit(rgb, (128, 128), Image.Resampling.LANCZOS), 'JPEG', 'image/jpeg', 85),
('optimized', rgb.resize(fit_size(rgb.size, 1280), Image.Resampling.LANCZOS), 'WEBP', 'image/webp', 80),
):
with image, io.BytesIO() as output:
image.save(output, format=fmt, quality=quality)
blob = output.getvalue()
yield {'name': name, 'content_type': mime, 'width': image.width,
'height': image.height, 'size_bytes': len(blob),
'sha256': hashlib.sha256(blob).hexdigest()}, blob
finally:
rgb.close()
def process(sessions, job_id, token):
with sessions() as session:
job = session.get(AttachmentJob, job_id)
if job.lease_token != token:
return
attachment = session.get(Attachment, job.attachment_id)
staging_key = attachment.staging_key
client = s3_client()
response = client.get_object(Bucket=BUCKET, Key=staging_key)
with response['Body'] as body:
data = body.read(MAX_BYTES + 1)
if len(data) != attachment.size_bytes or hashlib.sha256(data).hexdigest() != attachment.sha256:
raise ValueError('staging checksum mismatch')
try:
rendered = list(variants(data))
except OSError as exc:
raise ValueError('invalid image') from exc
# Job lock fences the final writes, cleanup and publication against lease recovery.
# Work before this transaction is CPU-only. Keys are stable across retries.
with sessions.begin() as session:
job = session.get(AttachmentJob, job_id, with_for_update=True)
if job.lease_token != token or job.lease_until <= now():
return
current = session.get(Attachment, job.attachment_id, with_for_update=True)
if current.status != 'processing':
return
client.put_object(Bucket=BUCKET, Key=current.object_key, Body=data,
ContentType=current.content_type)
saved = []
for meta, blob in rendered:
key = f'variants/{current.id}/v{job.pipeline_version}/{meta["name"]}'
client.put_object(Bucket=BUCKET, Key=key, Body=blob, ContentType=meta['content_type'])
saved.append({**meta, 'object_key': key})
# Test-only crash injection; never enabled by compose.yaml.
if os.getenv('JOB_FAULT') == 'after_objects':
os._exit(91)
current.variants = saved
current.status, current.error_code = 'ready', None
current.updated_at = now()
job.lease_token = job.lease_until = None
if os.getenv('JOB_FAULT') == 'after_commit':
os._exit(92)
def main():
# Linux worker: bounded address space as well as the container memory limit.
import resource
memory = int(os.getenv('JOB_MEMORY_MB', '768')) * 1024 * 1024
resource.setrlimit(resource.RLIMIT_AS, (memory, memory))
engine = create_db_engine()
try:
process(sessionmaker(engine, expire_on_commit=False), sys.argv[1], sys.argv[2])
except (UnidentifiedImageError, Image.DecompressionBombError, ValueError, SyntaxError):
return 20
except MemoryError:
return 21
except Exception:
return 22 # No credentials or decoder/database internals in logs.
finally:
engine.dispose()
return 0
if __name__ == '__main__':
sys.exit(main())