Spaces:
Running
Running
Create app_environment.py
Browse files- app_environment.py +36 -0
app_environment.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Hosting boundaries and session-owned temporary storage."""
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import os
|
| 4 |
+
import re
|
| 5 |
+
import shutil
|
| 6 |
+
import uuid
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
PROJECT_ROOT = Path(__file__).resolve().parent
|
| 10 |
+
UPLOAD_ROOT = PROJECT_ROOT / '.uploads'
|
| 11 |
+
PRIVATE_CACHE_ROOT = PROJECT_ROOT / '.model-cache' / 'sessions'
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def hosted_mode():
|
| 15 |
+
return os.environ.get('TOKEN_VISUALISER_HOSTED') == '1' or bool(os.environ.get('SPACE_ID'))
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def new_session_directory(root):
|
| 19 |
+
root = Path(root).resolve()
|
| 20 |
+
directory = root / uuid.uuid4().hex
|
| 21 |
+
directory.mkdir(parents=True, exist_ok=False)
|
| 22 |
+
return directory
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def remove_session_directory(directory, root):
|
| 26 |
+
"""Delete only an opaque session folder strictly within its designated root."""
|
| 27 |
+
root = Path(root).resolve()
|
| 28 |
+
target = Path(directory).resolve()
|
| 29 |
+
if target.parent != root or not re.fullmatch(r'[0-9a-f]{32}', target.name):
|
| 30 |
+
raise ValueError('Refusing to remove a folder outside session storage.')
|
| 31 |
+
if target.exists():
|
| 32 |
+
shutil.rmtree(target)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def hosted_download_limit():
|
| 36 |
+
return 4_000_000_000 if hosted_mode() else None
|