21 خطوط
1007 B
Python
21 خطوط
1007 B
Python
"""Shared bounded native schematic upload validation."""
|
|
from fastapi import HTTPException
|
|
from .config import settings
|
|
from .parser.record_decoder import ParseError,detect_format
|
|
|
|
async def read_schematic(file):
|
|
try:
|
|
name=(file.filename or '').replace('\\','/').rsplit('/',1)[-1]
|
|
name=''.join(c for c in name if c.isprintable())[:255]
|
|
if not name.lower().endswith('.schdoc'):
|
|
raise HTTPException(415,{'code':'unsupported_file_type','message':'Upload a .SchDoc file'})
|
|
body=bytearray()
|
|
while chunk:=await file.read(1024*1024):
|
|
body.extend(chunk)
|
|
if len(body)>settings().max_upload_bytes:
|
|
raise HTTPException(413,{'code':'upload_too_large','message':'Schematic exceeds configured upload size'})
|
|
try:detect_format(bytes(body))
|
|
except ParseError as exc:raise HTTPException(422,{'code':'invalid_schematic','message':str(exc)}) from exc
|
|
return name,bytes(body)
|
|
finally:await file.close()
|