forked from mtusi2026/mtusi2026lab5
prepared skeleton for lab5
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
"""Проверка имени и запуск ограниченного декодера."""
|
||||
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import unicodedata
|
||||
|
||||
from anyio import CancelScope
|
||||
|
||||
from app.core.s3 import S3Settings
|
||||
from app.errors.exceptions import (
|
||||
image_validation_error,
|
||||
invalid_request,
|
||||
payload_too_large,
|
||||
)
|
||||
from app.errors.handlers import operation_timeout
|
||||
|
||||
|
||||
def validate_filename(filename: str) -> None:
|
||||
if not 1 <= len(filename) <= 255 or any(
|
||||
char in "/\\" or unicodedata.category(char) in {"Cc", "Cf", "Cs"}
|
||||
for char in filename
|
||||
):
|
||||
raise invalid_request("Недопустимое имя файла")
|
||||
|
||||
|
||||
async def validate_image(path: Path, content_type: str, settings: S3Settings) -> None:
|
||||
process = await asyncio.create_subprocess_exec(
|
||||
sys.executable,
|
||||
"-m",
|
||||
"app.core.image_worker",
|
||||
str(path),
|
||||
content_type,
|
||||
str(settings.max_image_pixels),
|
||||
stdout=asyncio.subprocess.DEVNULL,
|
||||
stderr=asyncio.subprocess.DEVNULL,
|
||||
)
|
||||
error = payload_too_large("Превышен бюджет проверки изображения")
|
||||
try:
|
||||
async with operation_timeout(settings.image_timeout_seconds, error):
|
||||
code = await process.wait()
|
||||
finally:
|
||||
if process.returncode is None:
|
||||
process.kill()
|
||||
with CancelScope(shield=True):
|
||||
await process.wait()
|
||||
if code != 0:
|
||||
raise image_validation_error(code)
|
||||
Reference in New Issue
Block a user