INNER CODE UNIT · Python
atomic_write_json
ChenShuo2004/cs-board · webapp/server.py:545
def atomic_write_json(target: Path, value: Any) -> None:
temporary = target.with_suffix(target.suffix + ".tmp")
temporary.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding="utf-8")
temporary.replace(target)
def valid_image_file(path: Path) -> bool:
if not path.exists() or path.stat().st_size < 1024:
return False
try:
from PIL import Image
with Image.open(path) as image:
image.verify()
return True
except Exception:
return False