left4me/l4d2web/services/overlay_creation.py
mwiegand f0230e17d3
feat(l4d2-web): overlay path helpers and creation
Adds workshop_paths.cache_path(steam_id) returning
$LEFT4ME_ROOT/workshop_cache/{steam_id}.vpk with digit-only validation.

Adds overlay_creation.generate_overlay_path(id) and
create_overlay_directory(overlay) with exist_ok=False so a stray dir from a
prior failed delete surfaces loudly instead of shadowing fresh content.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 16:38:39 +02:00

35 lines
1.3 KiB
Python

"""Overlay path generation and on-disk directory bootstrap.
All new overlays (any type) get `path = str(overlay_id)`. The directory is
created with `exist_ok=False` so a stray folder from a prior failed delete
surfaces loudly instead of silently shadowing fresh content. Combined with
SQLite AUTOINCREMENT on `overlays.id`, that catches DB/disk drift.
"""
from __future__ import annotations
import os
from l4d2host.paths import get_left4me_root, validate_overlay_ref
from l4d2web.models import Overlay
def generate_overlay_path(overlay_id: int) -> str:
"""Return the canonical relative path for an overlay row.
Validates the result through l4d2host's overlay-ref guard. Pure numeric IDs
always pass — this is just a belt-and-suspenders check that surfaces
immediately if someone changes the scheme.
"""
candidate = str(overlay_id)
return validate_overlay_ref(candidate)
def create_overlay_directory(overlay: Overlay) -> None:
"""Create `LEFT4ME_ROOT/overlays/{overlay.path}/` with `exist_ok=False`.
Raises `FileExistsError` if the directory already exists, surfacing the
rare DB/disk-drift state where a stray directory matches a fresh ID.
"""
target = get_left4me_root() / "overlays" / overlay.path
os.makedirs(target, exist_ok=False)