token-visualiser / app_environment.py
jejunepixels's picture
Update app_environment.py
7b8b533 verified
Raw History Blame Contribute Delete
1.3 kB
"""Hosting boundaries and session-owned temporary storage."""
from pathlib import Path
import os
import re
import shutil
import uuid
PROJECT_ROOT = Path(__file__).resolve().parent
UPLOAD_ROOT = PROJECT_ROOT / '.uploads'
PRIVATE_CACHE_ROOT = PROJECT_ROOT / '.model-cache' / 'sessions'
def hosted_mode():
return os.environ.get('TOKEN_VISUALISER_HOSTED') == '1' or bool(os.environ.get('SPACE_ID'))
def new_session_directory(root):
root = Path(root).resolve()
directory = root / uuid.uuid4().hex
directory.mkdir(parents=True, exist_ok=False)
return directory
def remove_session_directory(directory, root):
"""Delete only an opaque session folder strictly within its designated root."""
root = Path(root).resolve()
target = Path(directory).resolve()
if target.parent != root or not re.fullmatch(r'[0-9a-f]{32}', target.name):
raise ValueError('Refusing to remove a folder outside session storage.')
if target.exists():
shutil.rmtree(target)
def hosted_download_limit():
if not hosted_mode():
return None
gigabytes = int(os.environ.get('TOKEN_VISUALISER_MAX_DOWNLOAD_GB', '16'))
if gigabytes < 1:
raise ValueError('TOKEN_VISUALISER_MAX_DOWNLOAD_GB must be a positive integer.')
return gigabytes * 1_000_000_000