File size: 1,303 Bytes
26b18fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b8b533
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""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