forked from mtusi2026/mtusi2026lab5
level 4
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
"""Offline checks for variant geometry and retry delay. No database and no broker."""
|
||||
import io
|
||||
import random
|
||||
import sys
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from .jobs import REQUIRED_VARIANTS, backoff_seconds
|
||||
from .thumbnail import (
|
||||
CROP_SIDE,
|
||||
OPTIMIZED_WEBP_QUALITY,
|
||||
THUMBNAIL_BOX,
|
||||
ImageRejected,
|
||||
render_variants,
|
||||
)
|
||||
|
||||
|
||||
def _png(width: int, height: int, mode: str = 'RGB') -> bytes:
|
||||
color = (20, 40, 60, 128) if mode == 'RGBA' else (20, 40, 60)
|
||||
image = Image.new(mode, (width, height), color)
|
||||
buffer = io.BytesIO()
|
||||
image.save(buffer, format='PNG')
|
||||
image.close()
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
def _jpeg_oriented(width: int, height: int) -> bytes:
|
||||
image = Image.new('RGB', (width, height), (10, 20, 30))
|
||||
exif = Image.Exif()
|
||||
exif[274] = 6
|
||||
buffer = io.BytesIO()
|
||||
image.save(buffer, format='JPEG', quality=90, exif=exif.tobytes())
|
||||
image.close()
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
def _expect(name: str, item: dict, width: int, height: int, content_type: str) -> None:
|
||||
if item['width'] != width or item['height'] != height or item['content_type'] != content_type:
|
||||
raise SystemExit('%s: got %s %sx%s' % (name, item['content_type'], item['width'], item['height']))
|
||||
opened = Image.open(io.BytesIO(item['data']))
|
||||
try:
|
||||
if opened.size != (width, height):
|
||||
raise SystemExit('%s pixels %s' % (name, opened.size))
|
||||
if content_type == 'image/webp' and list(opened.getexif()):
|
||||
raise SystemExit('optimized still carries EXIF')
|
||||
finally:
|
||||
opened.close()
|
||||
|
||||
|
||||
def _check_image(data: bytes, thumb: tuple, optimized: tuple) -> None:
|
||||
rendered = render_variants(data)
|
||||
if tuple(rendered) != REQUIRED_VARIANTS:
|
||||
raise SystemExit('variant set %s' % (tuple(rendered),))
|
||||
_expect('thumbnail', rendered['thumbnail'], thumb[0], thumb[1], 'image/jpeg')
|
||||
_expect('crop', rendered['crop'], CROP_SIDE, CROP_SIDE, 'image/jpeg')
|
||||
_expect('optimized', rendered['optimized'], optimized[0], optimized[1], 'image/webp')
|
||||
if rendered['optimized']['data'][:4] != b'RIFF' or rendered['optimized']['data'][8:12] != b'WEBP':
|
||||
raise SystemExit('optimized is not WebP')
|
||||
|
||||
|
||||
def _check_backoff() -> None:
|
||||
rng = random.Random(0)
|
||||
for attempt in range(1, 8):
|
||||
ceiling = min(30.0, 0.5 * float(2 ** (attempt - 1)))
|
||||
delay = backoff_seconds(attempt, 0.5, 30.0, rng)
|
||||
if delay < 0 or delay > ceiling:
|
||||
raise SystemExit('backoff attempt %s delay %s ceiling %s' % (attempt, delay, ceiling))
|
||||
if backoff_seconds(1, 0.5, 0.1, random.Random(1)) > 0.1:
|
||||
raise SystemExit('backoff ignored the cap')
|
||||
|
||||
|
||||
def main() -> None:
|
||||
_check_image(_png(320, 200), (THUMBNAIL_BOX, 160), (320, 200))
|
||||
_check_image(_png(64, 40), (64, 40), (64, 40))
|
||||
_check_image(_png(10, 10, 'RGBA'), (10, 10), (10, 10))
|
||||
_check_image(_jpeg_oriented(320, 200), (160, THUMBNAIL_BOX), (200, 320))
|
||||
_check_backoff()
|
||||
try:
|
||||
render_variants(b'not an image')
|
||||
except ImageRejected:
|
||||
pass
|
||||
else:
|
||||
raise SystemExit('invalid image was accepted')
|
||||
try:
|
||||
render_variants(b'\x89PNG\r\n\x1a\n' + b'0' * (10_485_760))
|
||||
except ImageRejected:
|
||||
pass
|
||||
else:
|
||||
raise SystemExit('oversized image was accepted')
|
||||
if OPTIMIZED_WEBP_QUALITY != 80:
|
||||
raise SystemExit('webp quality changed')
|
||||
print('PASS: thumbnail 256, crop 128, webp 1280-box, exif stripped, backoff capped')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except SystemExit as exc:
|
||||
if exc.code not in (0, None):
|
||||
print(exc, file=sys.stderr)
|
||||
raise
|
||||
Reference in New Issue
Block a user