INNER CODE UNIT · Python
upload_image
virajmavani/semi-auto-image-annotation-tool · web/backend/main.py:208
async def upload_image(file: UploadFile = File(...)):
"""Upload an image file"""
try:
filename = Path(file.filename).name # strip any directory components
validate_filename(filename)
if file.size and file.size > MAX_UPLOAD_SIZE:
raise HTTPException(status_code=413, detail="File too large (max 50 MB)")
# Save uploaded file
file_path = UPLOAD_DIR / filename
size_written = 0
with open(file_path, "wb") as buffer:
while chunk := await file.read(1024 * 1024):
size_written += len(chunk)
if size_written > MAX_UPLOAD_SIZE:
file_path.unlink(missing_ok=True)
raise HTTPException(status_code=413, detail="File too large (max 50 MB)")
buffer.write(chunk)