"""Utilities to deal with uploaded files.""" from pathlib import Path from typing import Union from werkzeug.utils import secure_filename from werkzeug.datastructures import FileStorage def save_file(fileobj: FileStorage, upload_dir: Path) -> Union[Path, bool]: """Save the uploaded file and return the path.""" if not bool(fileobj): return False filename = Path(secure_filename(fileobj.filename)) # type: ignore[arg-type] if not upload_dir.exists(): upload_dir.mkdir() filepath = Path(upload_dir, filename) fileobj.save(filepath) return filepath