forked from mtusi2026/mtusi2026lab4
create skeleton for lab 4
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
"""Синхронная проверка изображения; вызывается только из ограниченного процесса."""
|
||||
|
||||
from pathlib import Path
|
||||
import warnings
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from app.errors.exceptions import payload_too_large, unsupported_media_type
|
||||
|
||||
|
||||
def decode_image(path: Path, content_type: str, max_pixels: int) -> None:
|
||||
Image.MAX_IMAGE_PIXELS = max_pixels
|
||||
warnings.simplefilter('error', Image.DecompressionBombWarning)
|
||||
with Image.open(path, formats=['PNG', 'JPEG']) as image:
|
||||
if image.width * image.height > max_pixels:
|
||||
raise payload_too_large('Превышен лимит пикселей изображения')
|
||||
if Image.MIME.get(image.format) != content_type:
|
||||
raise unsupported_media_type('Содержимое не соответствует Content-Type')
|
||||
image.verify()
|
||||
# Для JPEG одной verify() недостаточно: декодировать весь оригинал.
|
||||
with Image.open(path, formats=['PNG', 'JPEG']) as image:
|
||||
image.load()
|
||||
Reference in New Issue
Block a user